示例#1
0
    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)
示例#2
0
    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")
示例#3
0
    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)
示例#4
0
    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")
示例#5
0
 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')
示例#6
0
    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")
示例#7
0
    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")
示例#8
0
 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')
示例#9
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')")
示例#10
0
 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')
示例#11
0
 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)
示例#12
0
 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")
示例#13
0
 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")
示例#14
0
 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')
示例#15
0
 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")
示例#16
0
    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')
示例#17
0
    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'
示例#18
0
    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'
示例#19
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'
示例#20
0
    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")
示例#21
0
    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"
        )
示例#22
0
    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"
示例#23
0
    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")
示例#24
0
    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")
示例#25
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"
示例#26
0
    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"
示例#27
0
    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')
示例#28
0
    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')
示例#29
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')"
        )
示例#30
0
    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")
示例#31
0
    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')
示例#32
0
    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')
示例#33
0
    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')
示例#34
0
    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)
示例#35
0
    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)
示例#36
0
 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)