def _check_utf8_encoding(self, bstring): """ Check whether the given sequence of bytes is properly encoded in UTF-8. :param bytes bstring: the byte string to be checked """ if not gf.is_bytes(bstring): self._failed(u"The given string is not a sequence of bytes") return if not gf.is_utf8_encoded(bstring): self._failed(u"The given string is not encoded in UTF-8.")
def test_is_utf8_encoded(self): tests = [ (u"foo".encode("ascii"), True), (u"foo".encode("latin-1"), True), (u"foo".encode("utf-8"), True), (u"foo".encode("utf-16"), False), (u"foo".encode("utf-32"), False), (u"foà".encode("latin-1"), False), (u"foà".encode("utf-8"), True), (u"foà".encode("utf-16"), False), (u"foà".encode("utf-32"), False), ] for test in tests: self.assertEqual(gf.is_utf8_encoded(test[0]), test[1])