def test_long_unicode(self): one_line_u = self._long_u.replace("\n", " ") mismatch = _BinaryMismatch(one_line_u, "!~", self._long_u) self.assertEqual( mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ("!~", text_repr(one_line_u), text_repr(self._long_u, multiline=True)))
def test_describe_non_ascii_unicode(self): string = _u("A\xA7") suffix = _u("B\xA7") mismatch = DoesNotEndWith(string, suffix) self.assertEqual( "%s does not end with %s." % (text_repr(string), text_repr(suffix)), mismatch.describe())
def test_long_unicode(self): one_line_u = self._long_u.replace("\n", " ") mismatch = _BinaryMismatch(one_line_u, "!~", self._long_u) self.assertEqual(mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ("!~", text_repr(one_line_u), text_repr(self._long_u, multiline=True)))
def test_long_bytes(self): one_line_b = self._long_b.replace(_b("\n"), _b(" ")) mismatch = _BinaryMismatch(one_line_b, "!~", self._long_b) self.assertEqual( mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ("!~", text_repr(one_line_b), text_repr(self._long_b, multiline=True)))
def test_long_mixed_strings(self): mismatch = _BinaryMismatch(self._long_b, "!~", self._long_u) self.assertEqual( mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ("!~", text_repr(self._long_b, multiline=True), text_repr(self._long_u, multiline=True)), )
def test_describe_non_ascii_unicode(self): string = _u("A\xA7") suffix = _u("B\xA7") mismatch = DoesNotEndWith(string, suffix) self.assertEqual("%s does not end with %s." % ( text_repr(string), text_repr(suffix)), mismatch.describe())
def test_long_bytes(self): one_line_b = self._long_b.replace(_b("\n"), _b(" ")) mismatch = _BinaryMismatch(one_line_b, "!~", self._long_b) self.assertEqual(mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ("!~", text_repr(one_line_b), text_repr(self._long_b, multiline=True)))
def test_long_mixed_strings(self): mismatch = _BinaryMismatch(self._long_b, "!~", self._long_u) self.assertEqual( mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ( "!~", text_repr(self._long_u, multiline=True), text_repr(self._long_b, multiline=True), ))
def test_describe_non_ascii_unicode(self): string = "A\xA7" suffix = "B\xA7" mismatch = DoesNotEndWith(string, suffix) self.assertEqual( "{} does not end with {}.".format(text_repr(string), text_repr(suffix)), mismatch.describe())
def test_long_unicode_and_object(self): obj = object() mismatch = _BinaryMismatch(self._long_u, "!~", obj) self.assertEqual(mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ("!~", text_repr(self._long_u, multiline=True), repr(obj)))
def test_ascii_examples_oneline_bytes(self): for s, expected, _ in self.ascii_examples: b = _b(s) actual = text_repr(b, multiline=False) # Add self.assertIsInstance check? self.assertEqual(actual, self.b_prefix + expected) self.assertEqual(eval(actual), b)
def test_long_unicode_and_object(self): obj = object() mismatch = _BinaryMismatch(self._long_u, "!~", obj) self.assertEqual( mismatch.describe(), "%s:\nreference = %s\nactual = %s\n" % ( "!~", repr(obj), text_repr(self._long_u, multiline=True), ))
def test_long_bytes_and_object(self): obj = object() mismatch = _BinaryMismatch(self._long_b, "!~", obj) self.assertEqual( mismatch.describe(), "{}:\nreference = {}\nactual = {}\n".format( "!~", repr(obj), text_repr(self._long_b, multiline=True), ))
def __str__(self): difference = self.mismatch.describe() if self.verbose: # GZ 2011-08-24: Smelly API? Better to take any object and special # case text inside? if istext(self.matchee) or _isbytes(self.matchee): matchee = text_repr(self.matchee, multiline=False) else: matchee = repr(self.matchee) return "Match failed. Matchee: %s\nMatcher: %s\nDifference: %s\n" % (matchee, self.matcher, difference) else: return difference
def __str__(self): difference = self.mismatch.describe() if self.verbose: # GZ 2011-08-24: Smelly API? Better to take any object and special # case text inside? if istext(self.matchee) or _isbytes(self.matchee): matchee = text_repr(self.matchee, multiline=False) else: matchee = repr(self.matchee) return ( 'Match failed. Matchee: %s\nMatcher: %s\nDifference: %s\n' % (matchee, self.matcher, difference)) else: return difference
def test_verbose_unicode(self): # When assertThat is given matchees or matchers that contain non-ASCII # unicode strings, we can still provide a meaningful error. matchee = '\xa7' matcher = Equals('a') mismatch = matcher.match(matchee) expected = ('Match failed. Matchee: %s\n' 'Matcher: %s\n' 'Difference: %s\n' % ( text_repr(matchee), matcher, mismatch.describe(), )) e = MismatchError(matchee, matcher, mismatch, True) self.assertEqual(expected, str(e))
def test_verbose_unicode(self): # When assertThat is given matchees or matchers that contain non-ASCII # unicode strings, we can still provide a meaningful error. matchee = _u("\xa7") matcher = Equals(_u("a")) mismatch = matcher.match(matchee) expected = ( "Match failed. Matchee: %s\n" "Matcher: %s\n" "Difference: %s\n" % (text_repr(matchee), matcher, mismatch.describe()) ) e = MismatchError(matchee, matcher, mismatch, True) if str_is_unicode: actual = str(e) else: actual = unicode(e) # Using str() should still work, and return ascii only self.assertEqual(expected.replace(matchee, matchee.encode("unicode-escape")), str(e).decode("ascii")) self.assertEqual(expected, actual)
def test_verbose_unicode(self): # When assertThat is given matchees or matchers that contain non-ASCII # unicode strings, we can still provide a meaningful error. matchee = _u('\xa7') matcher = Equals(_u('a')) mismatch = matcher.match(matchee) expected = ('Match failed. Matchee: %s\n' 'Matcher: %s\n' 'Difference: %s\n' % ( text_repr(matchee), matcher, mismatch.describe(), )) e = MismatchError(matchee, matcher, mismatch, True) if str_is_unicode: actual = str(e) else: actual = unicode(e) # Using str() should still work, and return ascii only self.assertEqual( expected.replace(matchee, matchee.encode("unicode-escape")), str(e).decode("ascii")) self.assertEqual(expected, actual)
def describe(self): return "%s does not comply with schema %s. msg: %s" % ( text_repr(str(self.json)), text_repr(str(self.schema)), text_repr(self.err_msg), )
def _format(self, thing): # Blocks of text with newlines are formatted as triple-quote # strings. Everything else is pretty-printed. if istext(thing) or _isbytes(thing): return text_repr(thing) return pformat(thing)
def describe(self): return "%s does not end with %s." % (text_repr( self.matchee), text_repr(self.expected))
def test_ascii_examples_multiline_unicode(self): for s, _, expected in self.ascii_examples: actual = text_repr(s, multiline=True) self.assertEqual(actual, self.u_prefix + expected) self.assertEqual(ast.literal_eval(actual), s)
def test_unicode_examples_multiline(self): for u, _, expected in self.unicode_examples: actual = text_repr(u, multiline=True) self.assertEqual(actual, self.u_prefix + expected) self.assertEqual(eval(actual), u)
def test_bytes_examples_multiline(self): for b, _, expected in self.bytes_examples: actual = text_repr(b, multiline=True) self.assertEqual(actual, self.b_prefix + expected) self.assertEqual(eval(actual), b)
def test_bytes_examples_oneline(self): for b, expected, _ in self.bytes_examples: actual = text_repr(b, multiline=False) self.assertEqual(actual, self.b_prefix + expected) self.assertEqual(eval(actual), b)
def test_ascii_examples_defaultline_unicode(self): for s, one, multi in self.ascii_examples: expected = "\n" in s and multi or one self.assertEqual(text_repr(_u(s)), self.u_prefix + expected)
def test_ascii_examples_multiline_unicode(self): for s, _, expected in self.ascii_examples: u = _u(s) actual = text_repr(u, multiline=True) self.assertEqual(actual, self.u_prefix + expected) self.assertEqual(eval(actual), u)
def test_ascii_examples_multiline_bytes(self): for s, _, expected in self.ascii_examples: b = _b(s) actual = text_repr(b, multiline=True) self.assertEqual(actual, self.b_prefix + expected) self.assertEqual(eval(actual), b)
def test_unicode_examples_oneline(self): for u, expected, _ in self.unicode_examples: actual = text_repr(u, multiline=False) self.assertEqual(actual, self.u_prefix + expected) self.assertEqual(ast.literal_eval(actual), u)
def describe(self): return "%s does not end with %s." % ( text_repr(self.matchee), text_repr(self.expected))