コード例 #1
0
 def test_dict(self):
     d = {"one": 1, "two": 2, "three": 3}
     for i in (1, 2, 3):
         self.assertTrue(i in py2.dict_values(d))
         self.assertFalse(i in py2.dict_keys(d))
     self.assertFalse("one" in py2.dict_values(d))
     self.assertTrue("one" in py2.dict_keys(d))
     self.assertTrue(("one", 1) in py2.dict_items(d))
     self.assertFalse((1, "one") in py2.dict_items(d))
     # finally, these functions return iterable objects, not lists
     try:
         py2.dict_keys(d)[0]
         self.fail("dict_keys can be indexed")
     except TypeError:
         pass
     try:
         py2.dict_values(d)[0]
         self.fail("dict_values can be indexed")
     except TypeError:
         pass
     try:
         py2.dict_items(d)[0]
         self.fail("dict_items can be indexed")
     except TypeError:
         pass
コード例 #2
0
ファイル: test_py2.py プロジェクト: rcplay/pyslet
 def test_dict(self):
     d = {"one": 1, "two": 2, "three": 3}
     for i in (1, 2, 3):
         self.assertTrue(i in py2.dict_values(d))
         self.assertFalse(i in py2.dict_keys(d))
     self.assertFalse("one" in py2.dict_values(d))
     self.assertTrue("one" in py2.dict_keys(d))
     self.assertTrue(("one", 1) in py2.dict_items(d))
     self.assertFalse((1, "one") in py2.dict_items(d))
     # finally, these functions return iterable objects, not lists
     try:
         py2.dict_keys(d)[0]
         self.fail("dict_keys can be indexed")
     except TypeError:
         pass
     try:
         py2.dict_values(d)[0]
         self.fail("dict_values can be indexed")
     except TypeError:
         pass
     try:
         py2.dict_items(d)[0]
         self.fail("dict_items can be indexed")
     except TypeError:
         pass
コード例 #3
0
    def test_nss(self):
        """Syntax for URN char::

            <trans> | "%" <hex> <hex>

        Translation is done by encoding each character outside the URN
        character set as a sequence of one to six octets using UTF-8
        encoding [5], and the encoding of each of those octets as "%"
        followed by two characters from the <hex> character set above.

        the character [%] used in a literal sense MUST be encoded

        a character MUST NOT be "%"-encoded if the character is not a
        reserved character

        SHOULD NOT use [other reserved characters] characters in
        unencoded form

        each character outside the URN character set [is encoded] as a
        sequence of one to six octets using UTF-8 encoding

        The presence of an "%" character in an URN MUST be followed by
        two characters from the <hex> character set

        In addition, octet 0 (0 hex) should NEVER be used, in either
        unencoded or %-encoded form."""
        trans_tests = {
            ul('\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10'
               '\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f '
               '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\'
               ']^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f'):
            '%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10'
            '%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20'
            '!%22%23$%25%26\'()*+,-.%2F0123456789:;%3C=%3E%3F@ABCDEFGHIJKLMN'
            'OPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D'
            '%7E%7F',
            u8(b'\xe8\x8b\xb1\xe5\x9b\xbd'): '%E8%8B%B1%E5%9B%BD',
            ul('Caf\xe9'): 'Caf%C3%A9'
            }
        for src, dst in dict_items(trans_tests):
            self.assertTrue(
                urn.translate_to_urnchar(src) == dst,
                "%s -> \n%s, expected \n%s" %
                (repr(src),
                 repr(urn.translate_to_urnchar(src)),
                 repr(dst)))
            self.assertTrue(
                urn.translate_from_urnchar(dst) == src,
                "%s -> \n%s, expected \n%s" %
                (repr(dst),
                 repr(urn.translate_from_urnchar(dst)), repr(src)))
            u = urn.URN(nid='foo', nss=dst)
            self.assertTrue(u.nss == dst)
            u = uri.URI.from_octets('urn:foo:%s' % dst)
            self.assertTrue(u.nss == dst)
        for wrong in ("100% wrong", "Zero%00"):
            try:
                urn.translate_from_urnchar(wrong)
                self.fail("%s test in URN" % repr(wrong))
            except ValueError:
                pass
        try:
            urn.translate_to_urnchar("Zero\x00Byte")
            self.fail("Zero byte test in URN")
        except ValueError:
            pass
        # let's invent a scheme whereby the reserved characters
        # include . which is reserved for special meaning and
        # / is used unencoded as a path separator (even though
        # it is reserved and *SHOULD* be encoded

        def dot(c):
            return c == "."

        src = "urn:path:.steve/file%2Ename/easy_come%2Feasy_go"
        u = uri.URI.from_octets(src)
        path = u.nss.replace('.', 'users/')
        path = [urn.translate_from_urnchar(s) for s in path.split('/')]
        self.assertTrue(path == [
            'users', 'steve', 'file.name', 'easy_come/easy_go'],
            "Parsed: %s" % repr(path))
        path = path[1:]
        # / is always reserved so we don't need to call this out
        path = [urn.translate_to_urnchar(x, dot) for x in path]
        # add the newly reserved characters after translation...
        path = '.' + '/'.join(path)
        u2 = urn.URN(nid='path', nss=path)
        self.assertTrue(u == u2)
        self.assertTrue(str(u) == str(u2))