def it_shows_diff_when_strings_differ(self): def _fails(): expect("foo\nbar") == "foo\nbaz" assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected 'foo\\nbaz' but got 'foo\\nbar'\n" "Diff:\n" "@@ -1,2 +1,2 @@\n" " foo\n" "-baz\n" "+bar" ), fail_msg(_fails)
def they_adjust_failure_message_for_expectation_name(self): def can_do_something(thing): return False def will_do_something(thing): return False for predicate in [can_do_something, will_do_something]: add_expectation(predicate) assert fail_msg(expect('walrus').can_do_something) == ( "Expected that 'walrus' can_do_something, but it can't") assert fail_msg(expect('walrus').will_do_something) == ( "Expected that 'walrus' will_do_something, but it won't")
def it_shows_diff_when_strings_differ(self): def _fails(): expect('foo\nbar') == 'foo\nbaz' assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected 'foo\\nbaz' but got 'foo\\nbar'\n" "Diff:\n" "@@ -1,2 +1,2 @@\n" " foo\n" "-baz\n" "+bar"), fail_msg(_fails)
def it_expects_isinstance(self): expect(1).isinstance(int) def _fails(): expect(1).isinstance(str) assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( 'Expected an instance of str but got an instance of int')
def it_requires_exceptions_to_be_raised(self): def _expects_raise_but_doesnt_get_it(): with expect.raises(KeyError): pass assert_raises(AssertionError, _expects_raise_but_doesnt_get_it) assert fail_msg(_expects_raise_but_doesnt_get_it) == ("Expected an exception of type KeyError but got none")
def they_have_default_failure_message(self): def predicate_with_bad_name(thing): return False add_expectation(predicate_with_bad_name) assert fail_msg(expect('walrus').predicate_with_bad_name) == ( "Expected that 'walrus' predicate_with_bad_name, but got False")
def it_expects_isinstance_for_multiple_types(self): expect('str').isinstance((str, bytes)) def _fails(): expect('str').isinstance((int, tuple)) assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( 'Expected an instance of int or tuple but got an instance of str')
def it_can_require_failure_messages(self): def _fails(): with expect.raises(ValueError, "my message"): raise ValueError("wrong message") assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ("Expected ValueError('my message') but got ValueError('wrong message')")
def it_expects_less_than_or_equal(self): expect(1) <= 1 expect(1) <= 2 def _fails(): expect(2) <= 1 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( 'Expected something less than or equal to 1 but got 2')
def it_shows_diff_for_large_reprs(self): sequence = list(range(1000, 1050)) big_list = sequence[:20] + [1019] + sequence[20:] def _fails(): expect(big_list) == sequence assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ("Expected {0} but got {1}\n" "Diff:\n" "@@ -17,6 +17,7 @@\n" " 1016,\n" " 1017,\n" " 1018,\n" "+ 1019,\n" " 1019,\n" " 1020,\n" " 1021," ).format(repr(sequence), repr(big_list)), fail_msg(_fails)
def it_expects_containment(self): expect([1]).contains(1) def _fails(): expect([2]).contains(1) assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected [2] to contain 1 but it didn't")
def it_expects_non_containment(self): expect([1]).does_not_contain(0) def _fails(): expect([1]).does_not_contain(1) assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected [1] to not contain 1 but it did")
def it_expects_greater_than_or_equal(self): expect(1) >= 1 expect(2) >= 1 def _fails(): expect(1) >= 2 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( 'Expected something greater than or equal to 2 but got 1')
def they_can_fail(self): add_expectation(self.is_a_potato) def _fails(): expect('not a potato').is_a_potato() assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected that 'not a potato' is_a_potato, but it isn't")
def it_requires_exceptions_to_be_raised(self): def _expects_raise_but_doesnt_get_it(): with expect.raises(KeyError): pass assert_raises(AssertionError, _expects_raise_but_doesnt_get_it) assert fail_msg(_expects_raise_but_doesnt_get_it) == ( 'Expected an exception of type KeyError but got none')
def it_expects_equals(self): expect(2) == 1 + 1 def _fails(): expect(1) == 2 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == 'Expected 2 but got 1'
def it_expects_less_than(self): expect(1) < 2 def _fails(): expect(1) < 0 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == 'Expected something less than 0 but got 1'
def it_expects_not_equals(self): expect(1) != 2 def _fails(): expect(1) != 1 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == 'Expected anything except 1 but got it'
def it_expects_non_containment(self): expect([1]).does_not_contain(0) def _fails(): expect([1]).does_not_contain(1) assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ("Expected [1] to not contain 1 but it did")
def they_have_default_failure_message(self): def predicate_with_bad_name(thing): return False add_expectation(predicate_with_bad_name) assert fail_msg(expect("walrus").predicate_with_bad_name) == ( "Expected that 'walrus' predicate_with_bad_name, but got False" )
def it_expects_less_than(self): expect(1) < 2 def _fails(): expect(1) < 0 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == "Expected something less than 0 but got 1"
def it_expects_containment(self): expect([1]).contains(1) def _fails(): expect([2]).contains(1) assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ("Expected [2] to contain 1 but it didn't")
def it_expects_greater_than(self): expect(2) > 1 def _fails(): expect(0) > 1 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ("Expected something greater than 1 but got 0")
def it_expects_not_equals(self): expect(1) != 2 def _fails(): expect(1) != 1 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == "Expected anything except 1 but got it"
def it_expects_equals(self): expect(2) == 1 + 1 def _fails(): expect(1) == 2 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == "Expected 2 but got 1"
def it_expects_greater_than(self): expect(2) > 1 def _fails(): expect(0) > 1 assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( 'Expected something greater than 1 but got 0')
def it_can_require_failure_messages(self): def _fails(): with expect.raises(ValueError, 'my message'): raise ValueError('wrong message') assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ( "Expected ValueError('my message') but got ValueError('wrong message')" )
def it_shows_diff_for_large_reprs(self): sequence = list(range(1000, 1050)) big_list = sequence[:20] + [1019] + sequence[20:] def _fails(): expect(big_list) == sequence assert_raises(AssertionError, _fails) assert fail_msg(_fails) == ("Expected {0} but got {1}\n" "Diff:\n" "@@ -17,6 +17,7 @@\n" " 1016,\n" " 1017,\n" " 1018,\n" "+ 1019,\n" " 1019,\n" " 1020,\n" " 1021,").format( repr(sequence), repr(big_list)), fail_msg(_fails)
def it_shows_diff_when_unicode_strings_differ(self): value = "ueber\ngeek" fixture = "über\ngeek" assert isinstance(value, unicode), "value is a " + repr(type(value)) assert isinstance(fixture, unicode), "fixture is a " + repr(type(fixture)) def _fails(): expect(value) == fixture assert_raises(AssertionError, _fails) msg = ( "Expected 'über\\ngeek' but got 'ueber\\ngeek'\n" "Diff:\n" "@@ -1,2 +1,2 @@\n" "-über\n" "+ueber\n" " geek" ) # normalize real msg for differences in py2 and py3 real = fail_msg(_fails).replace("u'", "'").replace("\\xfc", "\xfc") assert real == msg, "\n" + repr(real) + "\n" + repr(msg)
def it_shows_diff_when_unicode_strings_differ(self): value = 'ueber\ngeek' fixture = 'über\ngeek' assert isinstance(value, unicode), "value is a " + repr(type(value)) assert isinstance(fixture, unicode), "fixture is a " + repr(type(fixture)) def _fails(): expect(value) == fixture assert_raises(AssertionError, _fails) msg = ("Expected 'über\\ngeek' but got 'ueber\\ngeek'\n" "Diff:\n" "@@ -1,2 +1,2 @@\n" "-über\n" "+ueber\n" " geek" ) #normalize real msg for differences in py2 and py3 real = fail_msg(_fails).replace("u'", "'").replace( '\\xfc', '\xfc') assert real == msg, '\n' + repr(real) + '\n' + repr(msg)