Пример #1
0
 def test_accept_language_list(self):
     rq_tokens = AcceptLanguageList.from_str(" da, en-gb;q=0.8, en;q=0.7 ")
     self.assertTrue(
         rq_tokens.select_token(["en-US"]) == "en-US", "match prefix")
     self.assertTrue(is_string(rq_tokens.select_token(["en-US"])),
                     "select_token return type")
     match = rq_tokens.select_language(
         [params.LanguageTag.from_str("en-US")])
     self.assertTrue(match == "en-US", "match prefix (tag version)")
     self.assertTrue(isinstance(match, params.LanguageTag),
                     "select_language return type")
     rq_tokens = AcceptLanguageList.from_str(" da, en-gb;q=0.8, en;q=0.7 ")
     self.assertTrue(
         rq_tokens.select_language([params.LanguageTag.from_str("eng-US")])
         is None, "match prefix only")
     match = rq_tokens.select_language([
         params.LanguageTag.from_str("en-US"),
         params.LanguageTag.from_str("en-gb")
     ])
     self.assertTrue(match == "en-gb",
                     "match preference: found %s" % repr(match))
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-gb;q=0.8, en;q=0.7, *;q=0.75 ")
     self.assertTrue(
         rq_tokens.select_language([
             params.LanguageTag.from_str("en-US"),
             params.LanguageTag.from_str("de"),
             params.LanguageTag.from_str("en-gb")
         ]) == "en-gb", "match preference")
     self.assertTrue(
         rq_tokens.select_language([
             params.LanguageTag.from_str("en-US"),
             params.LanguageTag.from_str("de"),
         ]) == "de", "match preference")
     self.assertTrue(
         rq_tokens.select_language([
             params.LanguageTag.from_str(
                 "en-gb-drawl-berkshire-westreading")
         ]) is not None, "match long prefix only")
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-us;q=0.8, en-sg;q=0.7")
     self.assertTrue(
         rq_tokens.select_language([
             params.LanguageTag.from_str(
                 "en-gb-drawl-berkshire-westreading")
         ]) is None, "no match on long prefix only")
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-us;q=0.8, en-sg;q=0.7, en-gb-drawl-berkshire")
     self.assertTrue(
         rq_tokens.select_language([
             params.LanguageTag.from_str(
                 "en-gb-drawl-berkshire-westreading")
         ]) is not None, "match on long prefix")
Пример #2
0
 def test_accept_language_list(self):
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-gb;q=0.8, en;q=0.7 ")
     self.assertTrue(
         rq_tokens.select_token(["en-US"]) == "en-US", "match prefix")
     self.assertTrue(is_string(rq_tokens.select_token(["en-US"])),
                     "select_token return type")
     match = rq_tokens.select_language(
         [params.LanguageTag.from_str("en-US")])
     self.assertTrue(match == "en-US", "match prefix (tag version)")
     self.assertTrue(
         isinstance(match, params.LanguageTag),
         "select_language return type")
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-gb;q=0.8, en;q=0.7 ")
     self.assertTrue(rq_tokens.select_language(
         [params.LanguageTag.from_str("eng-US")]) is None,
         "match prefix only")
     match = rq_tokens.select_language(
         [params.LanguageTag.from_str("en-US"),
          params.LanguageTag.from_str("en-gb")])
     self.assertTrue(
         match == "en-gb", "match preference: found %s" % repr(match))
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-gb;q=0.8, en;q=0.7, *;q=0.75 ")
     self.assertTrue(rq_tokens.select_language([
         params.LanguageTag.from_str("en-US"),
         params.LanguageTag.from_str("de"),
         params.LanguageTag.from_str("en-gb")
     ]) == "en-gb", "match preference")
     self.assertTrue(rq_tokens.select_language([
         params.LanguageTag.from_str("en-US"),
         params.LanguageTag.from_str("de"),
     ]) == "de", "match preference")
     self.assertTrue(rq_tokens.select_language([params.LanguageTag.from_str(
         "en-gb-drawl-berkshire-westreading")]) is not None,
         "match long prefix only")
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-us;q=0.8, en-sg;q=0.7")
     self.assertTrue(rq_tokens.select_language([params.LanguageTag.from_str(
         "en-gb-drawl-berkshire-westreading")]) is None,
         "no match on long prefix only")
     rq_tokens = AcceptLanguageList.from_str(
         " da, en-us;q=0.8, en-sg;q=0.7, en-gb-drawl-berkshire")
     self.assertTrue(rq_tokens.select_language([params.LanguageTag.from_str(
         "en-gb-drawl-berkshire-westreading")]) is not None,
         "match on long prefix")
Пример #3
0
 def test_text(self):
     data = "hello"
     udata = u"hello"
     bdata = b"hello"
     xdata = ['hello']
     self.assertTrue(py2.is_string(data))
     self.assertTrue(py2.is_string(udata))
     self.assertTrue(py2.is_string(bdata))
     self.assertFalse(py2.is_string(xdata))
     self.assertTrue(py2.is_text(data))
     self.assertTrue(py2.is_text(udata))
     if sys.version_info[0] < 3:
         self.assertTrue(py2.is_text(bdata))
     else:
         self.assertFalse(py2.is_text(bdata))
     self.assertFalse(py2.is_text(xdata))
     if sys.version_info[0] < 3:
         self.assertFalse(py2.is_unicode(data))
     else:
         self.assertTrue(py2.is_unicode(data))
     self.assertTrue(py2.is_unicode(udata))
     self.assertFalse(py2.is_unicode(bdata))
     self.assertFalse(py2.is_unicode(xdata))
     # force text forces strings to be text
     self.assertTrue(data == py2.force_text(data))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(data), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(data), type("")))
     self.assertTrue(data == py2.force_text(udata))
     self.assertTrue(isinstance(py2.force_text(udata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(udata), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(udata), type("")))
     if sys.version_info[0] < 3:
         # force_text will not throw an error in python 2
         pass
     else:
         try:
             py2.force_text(bdata)
             self.fail("force_text(bytes)")
         except TypeError:
             pass
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_text(xdata)
         self.fail("force_text(object)")
     except TypeError:
         pass
     # force ascii forces strings to be ascii text
     self.assertTrue(data == py2.force_ascii(data))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_ascii(data), type(u"")))
     else:
         self.assertTrue(isinstance(py2.force_ascii(data), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(data), type("")))
     self.assertTrue(data == py2.force_ascii(udata))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_ascii(udata), type(u"")))
     else:
         self.assertTrue(isinstance(py2.force_ascii(udata), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(udata), type("")))
     if sys.version_info[0] < 3:
         self.assertTrue(bdata == py2.force_ascii(bdata))
         self.assertFalse(isinstance(py2.force_ascii(bdata), type(u"")))
     else:
         # can't compare different types in Python 3
         self.assertFalse(bdata == py2.force_ascii(bdata))
         self.assertTrue(isinstance(py2.force_ascii(bdata), type(u"")))
     self.assertTrue(isinstance(py2.force_ascii(bdata), type("")))
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_ascii(xdata)
         self.fail("force_ascii(object)")
     except TypeError:
         pass
     # conversion to text
     self.assertTrue(data == py2.to_text(data))
     self.assertTrue(isinstance(py2.to_text(data), type(u"")))
     self.assertTrue(data == py2.to_text(udata))
     self.assertTrue(isinstance(py2.to_text(udata), type(u"")))
     self.assertTrue(data == py2.to_text(bdata))
     self.assertTrue(isinstance(py2.to_text(bdata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertTrue(u"['hello']" == py2.to_text(xdata))
     else:
         self.assertTrue("['hello']" == py2.to_text(xdata))
     # check the empty text constant:
     self.assertTrue(isinstance(py2.uempty, type(u"")))
     self.assertFalse(py2.uempty)
     self.assertTrue(len(py2.uempty) == 0)
Пример #4
0
 def test_text(self):
     data = "hello"
     udata = u"hello"
     bdata = b"hello"
     xdata = ['hello']
     self.assertTrue(py2.is_string(data))
     self.assertTrue(py2.is_string(udata))
     self.assertTrue(py2.is_string(bdata))
     self.assertFalse(py2.is_string(xdata))
     self.assertTrue(py2.is_text(data))
     self.assertTrue(py2.is_text(udata))
     if sys.version_info[0] < 3:
         self.assertTrue(py2.is_text(bdata))
     else:
         self.assertFalse(py2.is_text(bdata))
     self.assertFalse(py2.is_text(xdata))
     if sys.version_info[0] < 3:
         self.assertFalse(py2.is_unicode(data))
     else:
         self.assertTrue(py2.is_unicode(data))
     self.assertTrue(py2.is_unicode(udata))
     self.assertFalse(py2.is_unicode(bdata))
     self.assertFalse(py2.is_unicode(xdata))
     # force text forces strings to be text
     self.assertTrue(data == py2.force_text(data))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     if sys.version_info[0] < 3:
         self.assertFalse(isinstance(py2.force_text(data), type("")))
     else:
         self.assertTrue(isinstance(py2.force_text(data), type("")))
     self.assertTrue(isinstance(py2.force_text(data), type(u"")))
     self.assertTrue(data == py2.force_text(udata))
     self.assertTrue(isinstance(py2.force_text(udata), type(u"")))
     if sys.version_info[0] < 3:
         # force_text will not throw an error in python 2
         pass
     else:
         try:
             py2.force_text(bdata)
             self.fail("force_text(bytes)")
         except TypeError:
             pass
     # this must work in both python 2 and 3 to prevent accidental
     # conversion to string.
     try:
         py2.force_text(xdata)
         self.fail("force_text(object)")
     except TypeError:
         pass
     # conversion to text
     self.assertTrue(data == py2.to_text(data))
     self.assertTrue(isinstance(py2.to_text(data), type(u"")))
     self.assertTrue(data == py2.to_text(udata))
     self.assertTrue(isinstance(py2.to_text(udata), type(u"")))
     self.assertTrue(data == py2.to_text(bdata))
     self.assertTrue(isinstance(py2.to_text(bdata), type(u"")))
     if sys.version_info[0] < 3:
         self.assertTrue(u"['hello']" == py2.to_text(xdata))
     else:
         self.assertTrue("['hello']" == py2.to_text(xdata))
     # check the empty text constant:
     self.assertTrue(isinstance(py2.uempty, type(u"")))
     self.assertFalse(py2.uempty)
     self.assertTrue(len(py2.uempty) == 0)