Description: Switch away from nose Author: Thomas Goirand Bug-Debian: https://bugs.debian.org/1018586 Forwarded: no Last-Update: 2024-05-02 Index: python-sure/tests/test_old_api.py =================================================================== --- python-sure.orig/tests/test_old_api.py +++ python-sure/tests/test_old_api.py @@ -24,1598 +24,1580 @@ import sure from sure.deprecated import that from sure.magic import is_cpython from sure import VariablesBag, expect -from nose.tools import assert_equal as assert_equals -from nose.tools import assert_raises +import unittest from sure.compat import compat_repr, safe_repr, text_type_name +class testOldApi(unittest.TestCase): + def test_setup_with_context(self): + "sure.with_context() runs setup before the function itself" + + def setup(context): + context.name = "John Resig" + + @sure.that_with_context(setup) + def john_is_within_context(context): + assert isinstance(context, VariablesBag) + assert hasattr(context, "name") + + john_is_within_context() + self.assertEqual( + john_is_within_context.__name__, + 'john_is_within_context', + ) -def test_setup_with_context(): - "sure.with_context() runs setup before the function itself" - - def setup(context): - context.name = "John Resig" - @sure.that_with_context(setup) - def john_is_within_context(context): - assert isinstance(context, VariablesBag) - assert hasattr(context, "name") + def test_context_is_not_optional(self): + "sure.that_with_context() when no context is given it fails" - john_is_within_context() - assert_equals( - john_is_within_context.__name__, - 'john_is_within_context', - ) + def setup(context): + context.name = "John Resig" + @sure.that_with_context(setup) + def it_crashes(): + assert True + + assert that(it_crashes).raises( + TypeError, ( + "the function it_crashes defined at test_old_api.py line 56, is being " + "decorated by either @that_with_context or @scenario, so it should " + "take at least 1 parameter, which is the test context"), + ) -def test_context_is_not_optional(): - "sure.that_with_context() when no context is given it fails" - def setup(context): - context.name = "John Resig" + def test_setup_with_context_context_failing(self): + "sure.that_with_context() in a failing test" - @sure.that_with_context(setup) - def it_crashes(): - assert True + def setup(context): + context.name = "John Resig" - assert that(it_crashes).raises( - TypeError, ( - "the function it_crashes defined at test_old_api.py line 56, is being " - "decorated by either @that_with_context or @scenario, so it should " - "take at least 1 parameter, which is the test context"), - ) + @sure.that_with_context(setup) + def it_fails(context): + assert False, 'should fail with this exception' + assert that(it_fails).raises('should fail with this exception') -def test_setup_with_context_context_failing(): - "sure.that_with_context() in a failing test" - def setup(context): - context.name = "John Resig" + def test_teardown_with_context(self): + "sure.with_context() runs teardown before the function itself" - @sure.that_with_context(setup) - def it_fails(context): - assert False, 'should fail with this exception' + class something: + pass - assert that(it_fails).raises('should fail with this exception') + def setup(context): + something.modified = True + def teardown(context): + del something.modified -def test_teardown_with_context(): - "sure.with_context() runs teardown before the function itself" + @sure.that_with_context(setup, teardown) + def something_was_modified(context): + assert hasattr(something, "modified") + assert something.modified - class something: - pass + something_was_modified() + assert not hasattr(something, "modified") - def setup(context): - something.modified = True - def teardown(context): - del something.modified + def test_that_is_a(self): + "that() is_a(object)" - @sure.that_with_context(setup, teardown) - def something_was_modified(context): - assert hasattr(something, "modified") - assert something.modified + something = "something" - something_was_modified() - assert not hasattr(something, "modified") + assert that(something).is_a(text_type) + assert isinstance(something, text_type) -def test_that_is_a(): - "that() is_a(object)" + def test_that_equals(self): + "that() equals(string)" - something = "something" + something = "something" - assert that(something).is_a(text_type) - assert isinstance(something, text_type) + assert that('something').equals(something) + assert something == 'something' -def test_that_equals(): - "that() equals(string)" + def test_that_differs(self): + "that() differs(object)" - something = "something" + something = "something" - assert that('something').equals(something) - assert something == 'something' + assert that(something).differs("23123%FYTUGIHOfdf") + assert something != "23123%FYTUGIHOfdf" -def test_that_differs(): - "that() differs(object)" + def test_that_has(self): + "that() has(object)" + class Class: + name = "some class" + Object = Class() + dictionary = { + 'name': 'John', + } + name = "john" - something = "something" + assert hasattr(Class, 'name') + assert that(Class).has("name") + assert that(Class).like("name") + assert "name" in that(Class) - assert that(something).differs("23123%FYTUGIHOfdf") - assert something != "23123%FYTUGIHOfdf" + assert hasattr(Object, 'name') + assert that(Object).has("name") + assert that(Object).like("name") + assert "name" in that(Object) + assert 'name' in dictionary + assert that(dictionary).has("name") + assert that(dictionary).like("name") + assert "name" in that(dictionary) -def test_that_has(): - "that() has(object)" - class Class: - name = "some class" - Object = Class() - dictionary = { - 'name': 'John', - } - name = "john" + assert that(name).has("john") + assert that(name).like("john") + assert "john" in that(name) + assert that(name).has("hn") + assert that(name).like("hn") + assert "hn" in that(name) + assert that(name).has("jo") + assert that(name).like("jo") + assert "jo" in that(name) - assert hasattr(Class, 'name') - assert that(Class).has("name") - assert that(Class).like("name") - assert "name" in that(Class) - assert hasattr(Object, 'name') - assert that(Object).has("name") - assert that(Object).like("name") - assert "name" in that(Object) + def test_that_at_key_equals(self): + "that().at(object).equals(object)" - assert 'name' in dictionary - assert that(dictionary).has("name") - assert that(dictionary).like("name") - assert "name" in that(dictionary) + class Class: + name = "some class" + Object = Class() + dictionary = { + 'name': 'John', + } - assert that(name).has("john") - assert that(name).like("john") - assert "john" in that(name) - assert that(name).has("hn") - assert that(name).like("hn") - assert "hn" in that(name) - assert that(name).has("jo") - assert that(name).like("jo") - assert "jo" in that(name) + assert that(Class).at("name").equals('some class') + assert that(Object).at("name").equals('some class') + assert that(dictionary).at("name").equals('John') -def test_that_at_key_equals(): - "that().at(object).equals(object)" + def test_that_len_is(self): + "that() len_is(number)" - class Class: - name = "some class" - Object = Class() - dictionary = { - 'name': 'John', - } + lst = range(1000) - assert that(Class).at("name").equals('some class') - assert that(Object).at("name").equals('some class') - assert that(dictionary).at("name").equals('John') + assert that(lst).len_is(1000) + assert len(lst) == 1000 + assert that(lst).len_is(lst) -def test_that_len_is(): - "that() len_is(number)" + def test_that_len_greater_than(self): + "that() len_greater_than(number)" - lst = range(1000) + lst = range(1000) + lst2 = range(100) - assert that(lst).len_is(1000) - assert len(lst) == 1000 - assert that(lst).len_is(lst) + assert that(lst).len_greater_than(100) + assert len(lst) == 1000 + assert that(lst).len_greater_than(lst2) -def test_that_len_greater_than(): - "that() len_greater_than(number)" + def test_that_len_greater_than_should_raise_assertion_error(self): + "that() len_greater_than(number) raise AssertionError" - lst = range(1000) - lst2 = range(100) + lst = list(range(1000)) + try: + that(lst).len_greater_than(1000) + except AssertionError as e: + self.assertEqual( + str(e), + 'the length of the list should be greater then %d, but is %d' \ + % (1000, 1000)) - assert that(lst).len_greater_than(100) - assert len(lst) == 1000 - assert that(lst).len_greater_than(lst2) + def test_that_len_greater_than_or_equals(self): + "that() len_greater_than_or_equals(number)" -def test_that_len_greater_than_should_raise_assertion_error(): - "that() len_greater_than(number) raise AssertionError" + lst = list(range(1000)) + lst2 = list(range(100)) - lst = list(range(1000)) - try: - that(lst).len_greater_than(1000) - except AssertionError as e: - assert_equals( - str(e), - 'the length of the list should be greater then %d, but is %d' \ - % (1000, 1000)) + assert that(lst).len_greater_than_or_equals(100) + assert that(lst).len_greater_than_or_equals(1000) + assert len(lst) == 1000 + assert that(lst).len_greater_than_or_equals(lst2) + assert that(lst).len_greater_than_or_equals(lst) -def test_that_len_greater_than_or_equals(): - "that() len_greater_than_or_equals(number)" + def test_that_len_greater_than_or_equals_should_raise_assertion_error(self): + "that() len_greater_than_or_equals(number) raise AssertionError" - lst = list(range(1000)) - lst2 = list(range(100)) + lst = list(range(1000)) + try: + that(lst).len_greater_than_or_equals(1001) + except AssertionError as e: + self.assertEqual( + str(e), + 'the length of %r should be greater then or equals %d, but is %d' \ + % (lst, 1001, 1000)) - assert that(lst).len_greater_than_or_equals(100) - assert that(lst).len_greater_than_or_equals(1000) - assert len(lst) == 1000 - assert that(lst).len_greater_than_or_equals(lst2) - assert that(lst).len_greater_than_or_equals(lst) + def test_that_len_lower_than(self): + "that() len_lower_than(number)" -def test_that_len_greater_than_or_equals_should_raise_assertion_error(): - "that() len_greater_than_or_equals(number) raise AssertionError" + lst = list(range(100)) + lst2 = list(range(1000)) - lst = list(range(1000)) - try: - that(lst).len_greater_than_or_equals(1001) - except AssertionError as e: - assert_equals( - str(e), - 'the length of %r should be greater then or equals %d, but is %d' \ - % (lst, 1001, 1000)) + assert that(lst).len_lower_than(101) + assert len(lst) == 100 + assert that(lst).len_lower_than(lst2) -def test_that_len_lower_than(): - "that() len_lower_than(number)" + def test_that_len_lower_than_should_raise_assertion_error(self): + "that() len_lower_than(number) raise AssertionError" - lst = list(range(100)) - lst2 = list(range(1000)) + lst = list(range(1000)) + try: + that(lst).len_lower_than(1000) + except AssertionError as e: + self.assertEqual( + str(e), + 'the length of %r should be lower then %d, but is %d' % \ + (lst, 1000, 1000)) - assert that(lst).len_lower_than(101) - assert len(lst) == 100 - assert that(lst).len_lower_than(lst2) + def test_that_len_lower_than_or_equals(self): + "that() len_lower_than_or_equals(number)" -def test_that_len_lower_than_should_raise_assertion_error(): - "that() len_lower_than(number) raise AssertionError" + lst = list(range(1000)) + lst2 = list(range(1001)) - lst = list(range(1000)) - try: - that(lst).len_lower_than(1000) - except AssertionError as e: - assert_equals( - str(e), - 'the length of %r should be lower then %d, but is %d' % \ - (lst, 1000, 1000)) + assert that(lst).len_lower_than_or_equals(1001) + assert that(lst).len_lower_than_or_equals(1000) + assert len(lst) == 1000 + assert that(lst).len_lower_than_or_equals(lst2) + assert that(lst).len_lower_than_or_equals(lst) -def test_that_len_lower_than_or_equals(): - "that() len_lower_than_or_equals(number)" + def test_that_len_lower_than_or_equals_should_raise_assertion_error(self): + "that() len_lower_than_or_equals(number) raise AssertionError" - lst = list(range(1000)) - lst2 = list(range(1001)) + lst = list(range(1000)) + try: + that(lst).len_lower_than_or_equals(100) + except AssertionError as e: + self.assertEqual( + str(e), + 'the length of %r should be lower then or equals %d, but is %d' % \ + (lst, 100, 1000)) - assert that(lst).len_lower_than_or_equals(1001) - assert that(lst).len_lower_than_or_equals(1000) - assert len(lst) == 1000 - assert that(lst).len_lower_than_or_equals(lst2) - assert that(lst).len_lower_than_or_equals(lst) + def test_that_checking_all_atributes(self): + "that(iterable).the_attribute('name').equals('value')" + class shape(object): + def __init__(self, name): + self.kind = 'geometrical form' + self.name = name -def test_that_len_lower_than_or_equals_should_raise_assertion_error(): - "that() len_lower_than_or_equals(number) raise AssertionError" + shapes = [ + shape('circle'), + shape('square'), + shape('rectangle'), + shape('triangle'), + ] - lst = list(range(1000)) - try: - that(lst).len_lower_than_or_equals(100) - except AssertionError as e: - assert_equals( - str(e), - 'the length of %r should be lower then or equals %d, but is %d' % \ - (lst, 100, 1000)) + assert that(shapes).the_attribute("kind").equals('geometrical form') -def test_that_checking_all_atributes(): - "that(iterable).the_attribute('name').equals('value')" - class shape(object): - def __init__(self, name): - self.kind = 'geometrical form' - self.name = name + def test_that_checking_all_atributes_of_range(self): + "that(iterable, within_range=(1, 2)).the_attribute('name').equals('value')" + class shape(object): + def __init__(self, name): + self.kind = 'geometrical form' + self.name = name - shapes = [ - shape('circle'), - shape('square'), - shape('rectangle'), - shape('triangle'), - ] + def __repr__(self): + return '<%s:%s>' % (self.kind, self.name) - assert that(shapes).the_attribute("kind").equals('geometrical form') + shapes = [ + shape('circle'), + shape('square'), + shape('square'), + shape('triangle'), + ] + assert shapes[0].name != 'square' + assert shapes[3].name != 'square' -def test_that_checking_all_atributes_of_range(): - "that(iterable, within_range=(1, 2)).the_attribute('name').equals('value')" - class shape(object): - def __init__(self, name): - self.kind = 'geometrical form' - self.name = name + assert shapes[1].name == 'square' + assert shapes[2].name == 'square' - def __repr__(self): - return '<%s:%s>' % (self.kind, self.name) + assert that(shapes, within_range=(1, 2)). \ + the_attribute("name"). \ + equals('square') - shapes = [ - shape('circle'), - shape('square'), - shape('square'), - shape('triangle'), - ] - assert shapes[0].name != 'square' - assert shapes[3].name != 'square' + def test_that_checking_all_elements(self): + "that(iterable).every_one_is('value')" + shapes = [ + 'cube', + 'ball', + 'ball', + 'piramid', + ] - assert shapes[1].name == 'square' - assert shapes[2].name == 'square' + assert shapes[0] != 'ball' + assert shapes[3] != 'ball' - assert that(shapes, within_range=(1, 2)). \ - the_attribute("name"). \ - equals('square') + assert shapes[1] == 'ball' + assert shapes[2] == 'ball' + assert that(shapes, within_range=(1, 2)).every_one_is('ball') -def test_that_checking_all_elements(): - "that(iterable).every_one_is('value')" - shapes = [ - 'cube', - 'ball', - 'ball', - 'piramid', - ] - assert shapes[0] != 'ball' - assert shapes[3] != 'ball' + def test_that_checking_each_matches(self): + "that(iterable).in_each('').equals('value')" + class animal(object): + def __init__(self, kind): + self.attributes = { + 'class': 'mammal', + 'kind': kind, + } - assert shapes[1] == 'ball' - assert shapes[2] == 'ball' + animals = [ + animal('dog'), + animal('cat'), + animal('cow'), + animal('cow'), + animal('cow'), + ] - assert that(shapes, within_range=(1, 2)).every_one_is('ball') + assert animals[0].attributes['kind'] != 'cow' + assert animals[1].attributes['kind'] != 'cow' + assert animals[2].attributes['kind'] == 'cow' + assert animals[3].attributes['kind'] == 'cow' + assert animals[4].attributes['kind'] == 'cow' -def test_that_checking_each_matches(): - "that(iterable).in_each('').equals('value')" - class animal(object): - def __init__(self, kind): - self.attributes = { - 'class': 'mammal', - 'kind': kind, - } + assert animals[0].attributes['class'] == 'mammal' + assert animals[1].attributes['class'] == 'mammal' + assert animals[2].attributes['class'] == 'mammal' + assert animals[3].attributes['class'] == 'mammal' + assert animals[4].attributes['class'] == 'mammal' - animals = [ - animal('dog'), - animal('cat'), - animal('cow'), - animal('cow'), - animal('cow'), - ] + assert that(animals).in_each("attributes['class']").matches('mammal') + assert that(animals).in_each("attributes['class']"). \ + matches(['mammal','mammal','mammal','mammal','mammal']) - assert animals[0].attributes['kind'] != 'cow' - assert animals[1].attributes['kind'] != 'cow' + assert that(animals).in_each("attributes['kind']"). \ + matches(['dog','cat','cow','cow','cow']) - assert animals[2].attributes['kind'] == 'cow' - assert animals[3].attributes['kind'] == 'cow' - assert animals[4].attributes['kind'] == 'cow' + try: + assert that(animals).in_each("attributes['kind']").matches(['dog']) + assert False, 'should not reach here' + except AssertionError as e: + assert that(text_type(e)).equals( + '%r has 5 items, but the matching list has 1: %r' % ( + ['dog','cat','cow','cow','cow'], ['dog'], + ) + ) - assert animals[0].attributes['class'] == 'mammal' - assert animals[1].attributes['class'] == 'mammal' - assert animals[2].attributes['class'] == 'mammal' - assert animals[3].attributes['class'] == 'mammal' - assert animals[4].attributes['class'] == 'mammal' - assert that(animals).in_each("attributes['class']").matches('mammal') - assert that(animals).in_each("attributes['class']"). \ - matches(['mammal','mammal','mammal','mammal','mammal']) + def test_that_raises(self): + "that(callable, with_args=[arg1], and_kwargs={'arg2': 'value'}).raises(SomeException)" + global called - assert that(animals).in_each("attributes['kind']"). \ - matches(['dog','cat','cow','cow','cow']) + called = False - try: - assert that(animals).in_each("attributes['kind']").matches(['dog']) - assert False, 'should not reach here' - except AssertionError as e: - assert that(text_type(e)).equals( - '%r has 5 items, but the matching list has 1: %r' % ( - ['dog','cat','cow','cow','cow'], ['dog'], - ) - ) + def function(arg1=None, arg2=None): + global called + called = True + if arg1 == 1 and arg2 == 2: + raise RuntimeError('yeah, it failed') + return "OK" -def test_that_raises(): - "that(callable, with_args=[arg1], and_kwargs={'arg2': 'value'}).raises(SomeException)" - global called + try: + function(1, 2) + assert False, 'should not reach here' - called = False + except RuntimeError as e: + assert text_type(e) == 'yeah, it failed' - def function(arg1=None, arg2=None): - global called - called = True - if arg1 == 1 and arg2 == 2: - raise RuntimeError('yeah, it failed') + except Exception: + assert False, 'should not reach here' - return "OK" + finally: + assert called + called = False - try: - function(1, 2) - assert False, 'should not reach here' + self.assertRaises(RuntimeError, function, 1, 2) - except RuntimeError as e: - assert text_type(e) == 'yeah, it failed' + called = False + self.assertEqual(function(3, 5), 'OK') + assert called - except Exception: - assert False, 'should not reach here' + called = False + assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ + raises(RuntimeError) + assert called - finally: + called = False + assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ + raises(RuntimeError, 'yeah, it failed') assert called + called = False + assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ + raises('yeah, it failed') + assert called - assert_raises(RuntimeError, function, 1, 2) + called = False + assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ + raises(RuntimeError) + assert called - called = False - assert_equals(function(3, 5), 'OK') - assert called + called = False + assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ + raises(RuntimeError, 'yeah, it failed') + assert called - called = False - assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ - raises(RuntimeError) - assert called + called = False + assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ + raises('yeah, it failed') + assert called - called = False - assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ - raises(RuntimeError, 'yeah, it failed') - assert called + called = False + assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ + raises(r'it fail') + assert called - called = False - assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \ - raises('yeah, it failed') - assert called + called = False + assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ + raises(RuntimeError, r'it fail') + assert called - called = False - assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ - raises(RuntimeError) - assert called - called = False - assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ - raises(RuntimeError, 'yeah, it failed') - assert called + def test_that_looks_like(self): + "that('String\\n with BREAKLINE').looks_like('string with breakline')" + assert that('String\n with BREAKLINE').looks_like('string with breakline') - called = False - assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ - raises('yeah, it failed') - assert called - called = False - assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ - raises(r'it fail') - assert called + def test_that_raises_does_raise_for_exception_type_mismatch(self): + "that(callable(RuntimeError)).raises(TypeError)" - called = False - assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \ - raises(RuntimeError, r'it fail') - assert called + error = r'.*should raise dependency_action at {0}:952'.replace('{0}', fullpath) -def test_depends_on_failing_due_nothing_found(): - "it fails when an action depends on some attribute that is not " \ - "provided by any other previous action" - import os - from sure import action_for, scenario + def with_setup(context): + @action_for(context, provides=['some_attr']) + def dependency_action(): + context.some_attr = True - fullpath = os.path.abspath(__file__).replace('.pyc', '.py') - error = 'the action "lonely_action" defined at %s:926 ' \ - 'depends on the attribute "something" to be available in the' \ - ' context. It turns out that there are no actions providing ' \ - 'that. Please double-check the implementation' % fullpath + @action_for(context, depends_on=['some_attr']) + def my_action(): + pass - def with_setup(context): - @action_for(context, depends_on=['something']) - def lonely_action(): - pass + @scenario(with_setup) + def depends_on_fails(the): + assert that(the.my_action).raises(AssertionError, error) + return True - @scenario(with_setup) - def depends_on_fails(the): - assert that(the.lonely_action).raises(AssertionError, error) - return True - - assert depends_on_fails() - - -def test_depends_on_failing_due_not_calling_a_previous_action(): - "it fails when an action depends on some attribute that is being " \ - "provided by other actions" - - import os - from sure import action_for, scenario - - fullpath = os.path.abspath(__file__).replace('.pyc', '.py') - error = 'the action "my_action" defined at {0}:956 ' \ - 'depends on the attribute "some_attr" to be available in the context.'\ - ' You need to call one of the following actions beforehand:\n' \ - ' -> dependency_action at {0}:952'.replace('{0}', fullpath) - - def with_setup(context): - @action_for(context, provides=['some_attr']) - def dependency_action(): - context.some_attr = True + assert depends_on_fails() - @action_for(context, depends_on=['some_attr']) - def my_action(): - pass - @scenario(with_setup) - def depends_on_fails(the): - assert that(the.my_action).raises(AssertionError, error) - return True + def test_that_contains_dictionary_keys(self): + "that(dict(name='foobar')).contains('name')" - assert depends_on_fails() + data = dict(name='foobar') + assert 'name' in data + assert 'name' in data.keys() + assert that(data).contains('name') -def test_that_contains_dictionary_keys(): - "that(dict(name='foobar')).contains('name')" + def test_that_contains_list(self): + "that(['foobar', '123']).contains('foobar')" - data = dict(name='foobar') - assert 'name' in data - assert 'name' in data.keys() - assert that(data).contains('name') + data = ['foobar', '123'] + assert 'foobar' in data + assert that(data).contains('foobar') -def test_that_contains_list(): - "that(['foobar', '123']).contains('foobar')" + def test_that_contains_set(self): + "that(set(['foobar', '123']).contains('foobar')" - data = ['foobar', '123'] - assert 'foobar' in data - assert that(data).contains('foobar') + data = set(['foobar', '123']) + assert 'foobar' in data + assert that(data).contains('foobar') -def test_that_contains_set(): - "that(set(['foobar', '123']).contains('foobar')" + def test_that_contains_tuple(self): + "that(('foobar', '123')).contains('foobar')" - data = set(['foobar', '123']) - assert 'foobar' in data - assert that(data).contains('foobar') + data = ('foobar', '123') + assert 'foobar' in data + assert that(data).contains('foobar') -def test_that_contains_tuple(): - "that(('foobar', '123')).contains('foobar')" + def test_variables_bag_provides_meaningful_error_on_nonexisting_attribute(self): + "VariablesBag() provides a meaningful error when attr does not exist" - data = ('foobar', '123') - assert 'foobar' in data - assert that(data).contains('foobar') + context = VariablesBag() + context.name = "John" + context.foo = "bar" -def test_variables_bag_provides_meaningful_error_on_nonexisting_attribute(): - "VariablesBag() provides a meaningful error when attr does not exist" + assert that(context.name).equals("John") + assert that(context.foo).equals("bar") - context = VariablesBag() + def access_nonexisting_attr(): + assert context.bleh == 'crash :(' - context.name = "John" - context.foo = "bar" + assert that(access_nonexisting_attr).raises( + AssertionError, + 'you have tried to access the attribute \'bleh\' from the context ' \ + '(aka VariablesBag), but there is no such attribute assigned to it. ' \ + 'Maybe you misspelled it ? Well, here are the options: ' \ + '[\'name\', \'foo\']', + ) - assert that(context.name).equals("John") - assert that(context.foo).equals("bar") - def access_nonexisting_attr(): - assert context.bleh == 'crash :(' + def test_actions_providing_dinamically_named_variables(self): + "the actions should be able to declare the variables they provide" + from sure import action_for, scenario - assert that(access_nonexisting_attr).raises( - AssertionError, - 'you have tried to access the attribute \'bleh\' from the context ' \ - '(aka VariablesBag), but there is no such attribute assigned to it. ' \ - 'Maybe you misspelled it ? Well, here are the options: ' \ - '[\'name\', \'foo\']', - ) + def with_setup(context): + @action_for(context, provides=['var1', '{0}']) + def the_context_has_variables(first_arg): + context.var1 = 123 + context[first_arg] = "qwerty" + @scenario(with_setup) + def the_providers_are_working(Then): + Then.the_context_has_variables('JohnDoe') + assert hasattr(Then, 'var1') + assert 'JohnDoe' in Then + assert hasattr(Then, '__sure_providers_of__') -def test_actions_providing_dinamically_named_variables(): - "the actions should be able to declare the variables they provide" - from sure import action_for, scenario + providers = Then.__sure_providers_of__ + action = Then.the_context_has_variables.__name__ - def with_setup(context): - @action_for(context, provides=['var1', '{0}']) - def the_context_has_variables(first_arg): - context.var1 = 123 - context[first_arg] = "qwerty" + providers_of_var1 = [p.__name__ for p in providers['var1']] + assert that(providers_of_var1).contains(action) - @scenario(with_setup) - def the_providers_are_working(Then): - Then.the_context_has_variables('JohnDoe') - assert hasattr(Then, 'var1') - assert 'JohnDoe' in Then - assert hasattr(Then, '__sure_providers_of__') + providers_of_JohnDoe = [p.__name__ for p in providers['JohnDoe']] + assert that(providers_of_JohnDoe).contains(action) - providers = Then.__sure_providers_of__ - action = Then.the_context_has_variables.__name__ + return True - providers_of_var1 = [p.__name__ for p in providers['var1']] - assert that(providers_of_var1).contains(action) + assert the_providers_are_working() - providers_of_JohnDoe = [p.__name__ for p in providers['JohnDoe']] - assert that(providers_of_JohnDoe).contains(action) - return True + def test_deep_equals_dict_level1_success(self): + "that() deep_equals(dict) succeeding on level 1" - assert the_providers_are_working() + something = { + 'one': 'yeah', + } + assert that(something).deep_equals({ + 'one': 'yeah', + }) -def test_deep_equals_dict_level1_success(): - "that() deep_equals(dict) succeeding on level 1" - something = { - 'one': 'yeah', - } + def test_deep_equals_dict_level1_fail(self): + "that() deep_equals(dict) failing on level 1" - assert that(something).deep_equals({ - 'one': 'yeah', - }) + something = { + 'one': 'yeah', + } + def assertions(): + assert that(something).deep_equals({ + 'one': 'oops', + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'one': 'yeah'}\n" \ + " and\n" \ + "Y = {'one': 'oops'}\n" \ + "X['one'] is 'yeah' whereas Y['one'] is 'oops'", + )) -def test_deep_equals_dict_level1_fail(): - "that() deep_equals(dict) failing on level 1" - something = { - 'one': 'yeah', - } + def test_deep_equals_list_level1_success(self): + "that(list) deep_equals(list) succeeding on level 1" - def assertions(): - assert that(something).deep_equals({ - 'one': 'oops', - }) + something = ['one', 'yeah'] + assert that(something).deep_equals(['one', 'yeah']) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'one': 'yeah'}\n" \ - " and\n" \ - "Y = {'one': 'oops'}\n" \ - "X['one'] is 'yeah' whereas Y['one'] is 'oops'", - )) + def test_deep_equals_list_level1_fail_by_value(self): + "that(list) deep_equals(list) failing on level 1" -def test_deep_equals_list_level1_success(): - "that(list) deep_equals(list) succeeding on level 1" + something = ['one', 'yeahs'] - something = ['one', 'yeah'] - assert that(something).deep_equals(['one', 'yeah']) + def assertions(): + assert that(something).deep_equals(['one', 'yeah']) + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = ['one', 'yeahs']\n" \ + " and\n" \ + "Y = ['one', 'yeah']\n" \ + "X[1] is 'yeahs' whereas Y[1] is 'yeah'", + )) -def test_deep_equals_list_level1_fail_by_value(): - "that(list) deep_equals(list) failing on level 1" - something = ['one', 'yeahs'] + def test_deep_equals_list_level1_fail_by_length_x_gt_y(self): + "that(list) deep_equals(list) failing by length (len(X) > len(Y))" - def assertions(): - assert that(something).deep_equals(['one', 'yeah']) + something = ['one', 'yeah', 'awesome!'] - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = ['one', 'yeahs']\n" \ - " and\n" \ - "Y = ['one', 'yeah']\n" \ - "X[1] is 'yeahs' whereas Y[1] is 'yeah'", - )) + def assertions(): + assert that(something).deep_equals(['one', 'yeah']) + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = ['one', 'yeah', 'awesome!']\n" \ + " and\n" \ + "Y = ['one', 'yeah']\n" \ + "X has 3 items whereas Y has only 2", + )) -def test_deep_equals_list_level1_fail_by_length_x_gt_y(): - "that(list) deep_equals(list) failing by length (len(X) > len(Y))" - something = ['one', 'yeah', 'awesome!'] + def test_deep_equals_list_level1_fail_by_length_y_gt_x(self): + "that(list) deep_equals(list) failing by length (len(Y) > len(X))" - def assertions(): - assert that(something).deep_equals(['one', 'yeah']) + something = ['one', 'yeah'] - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = ['one', 'yeah', 'awesome!']\n" \ - " and\n" \ - "Y = ['one', 'yeah']\n" \ - "X has 3 items whereas Y has only 2", - )) - - -def test_deep_equals_list_level1_fail_by_length_y_gt_x(): - "that(list) deep_equals(list) failing by length (len(Y) > len(X))" + def assertions(): + assert that(something).deep_equals(['one', 'yeah', 'damn']) - something = ['one', 'yeah'] + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" + "X = ['one', 'yeah']\n" + " and\n" + "Y = ['one', 'yeah', 'damn']\n" + "Y has 3 items whereas X has only 2" + ) + ) - def assertions(): - assert that(something).deep_equals(['one', 'yeah', 'damn']) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" - "X = ['one', 'yeah']\n" - " and\n" - "Y = ['one', 'yeah', 'damn']\n" - "Y has 3 items whereas X has only 2" - ) - ) + def test_deep_equals_dict_level1_fails_missing_key_on_y(self): + "that(X) deep_equals(Y) fails when Y is missing a key that X has" + something = { + 'one': 'yeah', + } -def test_deep_equals_dict_level1_fails_missing_key_on_y(): - "that(X) deep_equals(Y) fails when Y is missing a key that X has" + def assertions(): + assert that(something).deep_equals({ + 'two': 'yeah', + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" + "X = {{'one': 'yeah'}}\n" + " and\n" + "Y = {{'two': 'yeah'}}\n" + "X has the key \"{0}\" whereas Y does not" + ).format(safe_repr('one')) + ) - something = { - 'one': 'yeah', - } - def assertions(): - assert that(something).deep_equals({ - 'two': 'yeah', - }) + def test_deep_equals_failing_basic_vs_complex(self): + "that(X) deep_equals(Y) fails with basic vc complex type" - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" - "X = {{'one': 'yeah'}}\n" - " and\n" - "Y = {{'two': 'yeah'}}\n" - "X has the key \"{0}\" whereas Y does not" - ).format(safe_repr('one')) - ) + def assertions(): + assert that('two yeah').deep_equals({ + 'two': 'yeah', + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = 'two yeah'\n" + " and\n" \ + "Y = {'two': 'yeah'}\n" \ + "X is a %s and Y is a dict instead" % text_type_name, + )) + + + def test_deep_equals_failing_complex_vs_basic(self): + "that(X) deep_equals(Y) fails with complex vc basic type" + + def assertions(): + assert that({'two': 'yeah'}).deep_equals('two yeah') + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'two': 'yeah'}\n" \ + " and\n" \ + "Y = 'two yeah'\n" + "X is a dict and Y is a %s instead" % text_type_name, + )) -def test_deep_equals_failing_basic_vs_complex(): - "that(X) deep_equals(Y) fails with basic vc complex type" - - def assertions(): - assert that('two yeah').deep_equals({ - 'two': 'yeah', - }) + def test_deep_equals_tuple_level1_success(self): + "that(tuple) deep_equals(tuple) succeeding on level 1" - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = 'two yeah'\n" - " and\n" \ - "Y = {'two': 'yeah'}\n" \ - "X is a %s and Y is a dict instead" % text_type_name, - )) + something = ('one', 'yeah') + assert that(something).deep_equals(('one', 'yeah')) -def test_deep_equals_failing_complex_vs_basic(): - "that(X) deep_equals(Y) fails with complex vc basic type" + def test_deep_equals_tuple_level1_fail_by_value(self): + "that(tuple) deep_equals(tuple) failing on level 1" - def assertions(): - assert that({'two': 'yeah'}).deep_equals('two yeah') + something = ('one', 'yeahs') - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'two': 'yeah'}\n" \ - " and\n" \ - "Y = 'two yeah'\n" - "X is a dict and Y is a %s instead" % text_type_name, - )) + def assertions(): + assert that(something).deep_equals(('one', 'yeah')) + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = ('one', 'yeahs')\n" \ + " and\n" \ + "Y = ('one', 'yeah')\n" \ + "X[1] is 'yeahs' whereas Y[1] is 'yeah'", + )) -def test_deep_equals_tuple_level1_success(): - "that(tuple) deep_equals(tuple) succeeding on level 1" - something = ('one', 'yeah') - assert that(something).deep_equals(('one', 'yeah')) + def test_deep_equals_tuple_level1_fail_by_length_x_gt_y(self): + "that(tuple) deep_equals(tuple) failing by length (len(X) > len(Y))" + something = ('one', 'yeah', 'awesome!') -def test_deep_equals_tuple_level1_fail_by_value(): - "that(tuple) deep_equals(tuple) failing on level 1" + def assertions(): + assert that(something).deep_equals(('one', 'yeah')) - something = ('one', 'yeahs') + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = ('one', 'yeah', 'awesome!')\n" \ + " and\n" \ + "Y = ('one', 'yeah')\n" \ + "X has 3 items whereas Y has only 2", + )) - def assertions(): - assert that(something).deep_equals(('one', 'yeah')) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = ('one', 'yeahs')\n" \ - " and\n" \ - "Y = ('one', 'yeah')\n" \ - "X[1] is 'yeahs' whereas Y[1] is 'yeah'", - )) + def test_deep_equals_tuple_level1_fail_by_length_y_gt_x(self): + "that(tuple) deep_equals(tuple) failing by length (len(Y) > len(X))" + something = ('one', 'yeah') -def test_deep_equals_tuple_level1_fail_by_length_x_gt_y(): - "that(tuple) deep_equals(tuple) failing by length (len(X) > len(Y))" + def assertions(): + assert that(something).deep_equals(('one', 'yeah', 'damn')) - something = ('one', 'yeah', 'awesome!') + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = ('one', 'yeah')\n" \ + " and\n" \ + "Y = ('one', 'yeah', 'damn')\n" \ + "Y has 3 items whereas X has only 2", + )) - def assertions(): - assert that(something).deep_equals(('one', 'yeah')) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = ('one', 'yeah', 'awesome!')\n" \ - " and\n" \ - "Y = ('one', 'yeah')\n" \ - "X has 3 items whereas Y has only 2", - )) - - -def test_deep_equals_tuple_level1_fail_by_length_y_gt_x(): - "that(tuple) deep_equals(tuple) failing by length (len(Y) > len(X))" - - something = ('one', 'yeah') - - def assertions(): - assert that(something).deep_equals(('one', 'yeah', 'damn')) - - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = ('one', 'yeah')\n" \ - " and\n" \ - "Y = ('one', 'yeah', 'damn')\n" \ - "Y has 3 items whereas X has only 2", - )) - - -def test_deep_equals_fallsback_to_generic_comparator(): - "that() deep_equals(dict) falling back to generic comparator" - from datetime import datetime - now = datetime.now() - something = { - 'one': 'yeah', - 'date': now, - } - - assert that(something).deep_equals({ - 'one': 'yeah', - 'date': now, - }) - - -def test_deep_equals_fallsback_to_generic_comparator_failing(): - "that() deep_equals(dict) with generic comparator failing" - from datetime import datetime - now = datetime(2012, 3, 5) - tomorrow = datetime(2012, 3, 6) - something = { - 'date': now, - } + def test_deep_equals_fallsback_to_generic_comparator(self): + "that() deep_equals(dict) falling back to generic comparator" + from datetime import datetime + now = datetime.now() + something = { + 'one': 'yeah', + 'date': now, + } - def assertions(): assert that(something).deep_equals({ - 'date': tomorrow, + 'one': 'yeah', + 'date': now, }) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'date': datetime.datetime(2012, 3, 5, 0, 0)}\n" \ - " and\n" \ - "Y = {'date': datetime.datetime(2012, 3, 6, 0, 0)}\n" \ - "X['date'] != Y['date']", - )) - - -def test_deep_equals_fallsback_to_generic_comparator_failing_type(): - "that() deep_equals(dict) with generic comparator failing" - from datetime import datetime - now = datetime(2012, 3, 5) - something = { - 'date': now, - } - def assertions(): - assert that(something).deep_equals({ - 'date': None, - }) + def test_deep_equals_fallsback_to_generic_comparator_failing(self): + "that() deep_equals(dict) with generic comparator failing" + from datetime import datetime + now = datetime(2012, 3, 5) + tomorrow = datetime(2012, 3, 6) + something = { + 'date': now, + } + + def assertions(): + assert that(something).deep_equals({ + 'date': tomorrow, + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'date': datetime.datetime(2012, 3, 5, 0, 0)}\n" \ + " and\n" \ + "Y = {'date': datetime.datetime(2012, 3, 6, 0, 0)}\n" \ + "X['date'] != Y['date']", + )) + + + def test_deep_equals_fallsback_to_generic_comparator_failing_type(self): + "that() deep_equals(dict) with generic comparator failing" + from datetime import datetime + now = datetime(2012, 3, 5) + something = { + 'date': now, + } + + def assertions(): + assert that(something).deep_equals({ + 'date': None, + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'date': datetime.datetime(2012, 3, 5, 0, 0)}\n" \ + " and\n" \ + "Y = {'date': None}\n" \ + "X['date'] is a datetime and Y['date'] is a NoneType instead", + )) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'date': datetime.datetime(2012, 3, 5, 0, 0)}\n" \ - " and\n" \ - "Y = {'date': None}\n" \ - "X['date'] is a datetime and Y['date'] is a NoneType instead", - )) - - -def test_deep_equals_dict_level2_success(): - "that() deep_equals(dict) succeeding on level 2" - - something = { - 'one': 'yeah', - 'another': { - 'two': 'cool', - }, - } - - assert that(something).deep_equals({ - 'one': 'yeah', - 'another': { - 'two': 'cool', - }, - }) - - -def test_deep_equals_dict_level2_list_success(): - "that() deep_equals(dict) succeeding on level 2" - - something = { - 'one': 'yeah', - 'another': ['one', 'two', 3], - } - - assert that(something).deep_equals({ - 'one': 'yeah', - 'another': ['one', 'two', 3], - }) - - -def test_deep_equals_dict_level2_fail(): - "that() deep_equals(dict) failing on level 2" - - something = { - 'one': 'yeah', - 'another': { - 'two': '##', - }, - } - def assertions(): - assert that(something).deep_equals({ + def test_deep_equals_dict_level2_success(self): + "that() deep_equals(dict) succeeding on level 2" + + something = { 'one': 'yeah', 'another': { - 'two': '$$', + 'two': 'cool', }, - }) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'another': {'two': '##'}, 'one': 'yeah'}\n" \ - " and\n" \ - "Y = {'another': {'two': '$$'}, 'one': 'yeah'}\n" \ - "X['another']['two'] is '##' whereas Y['another']['two'] is '$$'", - )) - - -def test_deep_equals_dict_level3_fail_values(): - "that() deep_equals(dict) failing on level 3" - - something = { - 'my::all_users': [ - {'name': 'John', 'age': 33}, - ], - } + } - def assertions(): assert that(something).deep_equals({ - 'my::all_users': [ - {'name': 'John', 'age': 30}, - ], + 'one': 'yeah', + 'another': { + 'two': 'cool', + }, }) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'my::all_users': [{'age': 33, 'name': 'John'}]}\n" \ - " and\n" \ - "Y = {'my::all_users': [{'age': 30, 'name': 'John'}]}\n" \ - "X['my::all_users'][0]['age'] is 33 whereas Y['my::all_users'][0]['age'] is 30", - )) - - -def test_deep_equals_dict_level3_fails_missing_key(): - "that() deep_equals(dict) failing on level 3 when missing a key" - - something = { - 'my::all_users': [ - {'name': 'John', 'age': 33}, - ], - } - def assertions(): - assert that(something).deep_equals({ - 'my::all_users': [ - {'name': 'John', 'age': 30, 'foo': 'bar'}, - ], - }) + def test_deep_equals_dict_level2_list_success(self): + "that() deep_equals(dict) succeeding on level 2" - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" - "X = {{'my::all_users': [{{'age': 33, 'name': 'John'}}]}}\n" - " and\n" - "Y = {{'my::all_users': [{{'age': 30, 'foo': 'bar', 'name': 'John'}}]}}\n" - "X['my::all_users'][0] does not have the key \"{0}\" whereas Y['my::all_users'][0] has it" - ).format(safe_repr('foo')) - ) - - -def test_deep_equals_dict_level3_fails_extra_key(): - "that() deep_equals(dict) failing on level 3 when has an extra key" - - something = { - 'my::all_users': [ - {'name': 'John', 'age': 33, 'foo': 'bar'}, - ], - } + something = { + 'one': 'yeah', + 'another': ['one', 'two', 3], + } - def assertions(): assert that(something).deep_equals({ - 'my::all_users': [ - {'name': 'John', 'age': 30}, - ], + 'one': 'yeah', + 'another': ['one', 'two', 3], }) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" - "X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n" - " and\n" - "Y = {{'my::all_users': [{{'age': 30, 'name': 'John'}}]}}\n" - "X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not" - ).format(safe_repr('foo'))) - - -def test_deep_equals_dict_level3_fails_different_key(): - "that() deep_equals(dict) failing on level 3 when has an extra key" - - something = { - 'my::all_users': [ - {'name': 'John', 'age': 33, 'foo': 'bar'}, - ], - } - def assertions(): - assert that(something).deep_equals({ - 'my::all_users': [ - {'name': 'John', 'age': 33, 'bar': 'foo'}, - ], - }) + def test_deep_equals_dict_level2_fail(self): + "that() deep_equals(dict) failing on level 2" - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" - "X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n" - " and\n" - "Y = {{'my::all_users': [{{'age': 33, 'bar': 'foo', 'name': 'John'}}]}}\n" - "X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not" - ).format(safe_repr('foo'))) + something = { + 'one': 'yeah', + 'another': { + 'two': '##', + }, + } + def assertions(): + assert that(something).deep_equals({ + 'one': 'yeah', + 'another': { + 'two': '$$', + }, + }) + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'another': {'two': '##'}, 'one': 'yeah'}\n" \ + " and\n" \ + "Y = {'another': {'two': '$$'}, 'one': 'yeah'}\n" \ + "X['another']['two'] is '##' whereas Y['another']['two'] is '$$'", + )) -def test_deep_equals_list_level2_fail_by_length_x_gt_y(): - "that(list) deep_equals(list) failing by length (len(X) > len(Y))" - something = {'iterable': ['one', 'yeah', 'awesome!']} + def test_deep_equals_dict_level3_fail_values(self): + "that() deep_equals(dict) failing on level 3" - def assertions(): - assert that(something).deep_equals({'iterable': ['one', 'yeah']}) + something = { + 'my::all_users': [ + {'name': 'John', 'age': 33}, + ], + } - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = {'iterable': ['one', 'yeah', 'awesome!']}\n" \ - " and\n" \ - "Y = {'iterable': ['one', 'yeah']}\n" \ - "X has 3 items whereas Y has only 2", - )) + def assertions(): + assert that(something).deep_equals({ + 'my::all_users': [ + {'name': 'John', 'age': 30}, + ], + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'my::all_users': [{'age': 33, 'name': 'John'}]}\n" \ + " and\n" \ + "Y = {'my::all_users': [{'age': 30, 'name': 'John'}]}\n" \ + "X['my::all_users'][0]['age'] is 33 whereas Y['my::all_users'][0]['age'] is 30", + )) -def test_deep_equals_list_level2_fail_by_length_y_gt_x(): - "that(list) deep_equals(list) failing by length (len(Y) > len(X))" + def test_deep_equals_dict_level3_fails_missing_key(self): + "that() deep_equals(dict) failing on level 3 when missing a key" - something = ['one', 'yeah'] + something = { + 'my::all_users': [ + {'name': 'John', 'age': 33}, + ], + } - def assertions(): - assert that(something).deep_equals(['one', 'yeah', 'damn']) + def assertions(): + assert that(something).deep_equals({ + 'my::all_users': [ + {'name': 'John', 'age': 30, 'foo': 'bar'}, + ], + }) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" + "X = {{'my::all_users': [{{'age': 33, 'name': 'John'}}]}}\n" + " and\n" + "Y = {{'my::all_users': [{{'age': 30, 'foo': 'bar', 'name': 'John'}}]}}\n" + "X['my::all_users'][0] does not have the key \"{0}\" whereas Y['my::all_users'][0] has it" + ).format(safe_repr('foo')) + ) - assert that(assertions).raises( - AssertionError, compat_repr( - "given\n" \ - "X = ['one', 'yeah']\n" \ - " and\n" \ - "Y = ['one', 'yeah', 'damn']\n" \ - "Y has 3 items whereas X has only 2", - )) + def test_deep_equals_dict_level3_fails_extra_key(self): + "that() deep_equals(dict) failing on level 3 when has an extra key" -def test_function_decorated_with_wip_should_set_a_flag(): - "@sure.work_in_progress should set an internal flag into `sure`" + something = { + 'my::all_users': [ + {'name': 'John', 'age': 33, 'foo': 'bar'}, + ], + } - @sure.work_in_progress - def this_was_called(): - assert sure.it('is_running') - return True + def assertions(): + assert that(something).deep_equals({ + 'my::all_users': [ + {'name': 'John', 'age': 30}, + ], + }) - assert not sure._registry['is_running'] - assert this_was_called() - assert not sure._registry['is_running'] + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" + "X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n" + " and\n" + "Y = {{'my::all_users': [{{'age': 30, 'name': 'John'}}]}}\n" + "X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not" + ).format(safe_repr('foo'))) -def test_that_equals_fails(): - "that() equals(string) when it's supposed to fail" + def test_deep_equals_dict_level3_fails_different_key(self): + "that() deep_equals(dict) failing on level 3 when has an extra key" - something = "else" + something = { + 'my::all_users': [ + {'name': 'John', 'age': 33, 'foo': 'bar'}, + ], + } - def fail(): - assert that('something').equals(something) + def assertions(): + assert that(something).deep_equals({ + 'my::all_users': [ + {'name': 'John', 'age': 33, 'bar': 'foo'}, + ], + }) - assert that(fail).raises( - AssertionError, compat_repr( - "given\n" \ - "X = 'something'\n" \ - " and\n" \ - "Y = 'else'\n" \ - "X is 'something' whereas Y is 'else'", - )) - - -def test_raises_with_string(): - "that(callable).raises('message') should compare the message" - - def it_fails(): - assert False, 'should fail with this exception' - - try: - that(it_fails).raises('wrong msg') - raise RuntimeError('should not reach here') - except AssertionError as e: - assert that(text_type(e)).contains('''EXPECTED: -wrong msg - -GOT: -should fail with this exception''') - - -def test_deep_equals_weird(): - part1 = [ - ('Bootstraping Redis role', []), - ('Restart scalarizr', []), - ('Rebundle server', ['rebundle']), - ('Use new role', ['rebundle']), - ('Restart scalarizr after bundling', ['rebundle']), - ('Bundling data', []), - ('Modifying data', []), - ('Reboot server', []), - ('Backuping data on Master', []), - ('Setup replication', []), - ('Restart scalarizr in slave', []), - ('Slave force termination', []), - ('Slave delete EBS', ['ec2']), - ('Setup replication for EBS test', ['ec2']), - ('Writing on Master, reading on Slave', []), - ('Slave -> Master promotion', []), - ('Restart farm', ['restart_farm']), - ] - - part2 = [ - ('Bootstraping Redis role', ['rebundle', 'rebundle', 'rebundle']), - ('Restart scalarizr', []), - ('Rebundle server', ['rebundle']), - ('Use new role', ['rebundle']), - ('Restart scalarizr after bundling', ['rebundle']), - ('Bundling data', []), - ('Modifying data', []), - ('Reboot server', []), - ('Backuping data on Master', []), - ('Setup replication', []), - ('Restart scalarizr in slave', []), - ('Slave force termination', []), - ('Slave delete EBS', ['ec2']), - ('Setup replication for EBS test', ['ec2']), - ('Writing on Master, reading on Slave', []), - ('Slave -> Master promotion', []), - ('Restart farm', ['restart_farm']), - ] + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" + "X = {{'my::all_users': [{{'age': 33, 'foo': 'bar', 'name': 'John'}}]}}\n" + " and\n" + "Y = {{'my::all_users': [{{'age': 33, 'bar': 'foo', 'name': 'John'}}]}}\n" + "X['my::all_users'][0] has the key \"{0}\" whereas Y['my::all_users'][0] does not" + ).format(safe_repr('foo'))) + + + def test_deep_equals_list_level2_fail_by_length_x_gt_y(self): + "that(list) deep_equals(list) failing by length (len(X) > len(Y))" + + something = {'iterable': ['one', 'yeah', 'awesome!']} + + def assertions(): + assert that(something).deep_equals({'iterable': ['one', 'yeah']}) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = {'iterable': ['one', 'yeah', 'awesome!']}\n" \ + " and\n" \ + "Y = {'iterable': ['one', 'yeah']}\n" \ + "X has 3 items whereas Y has only 2", + )) + + + def test_deep_equals_list_level2_fail_by_length_y_gt_x(self): + "that(list) deep_equals(list) failing by length (len(Y) > len(X))" + + something = ['one', 'yeah'] + + def assertions(): + assert that(something).deep_equals(['one', 'yeah', 'damn']) + + assert that(assertions).raises( + AssertionError, compat_repr( + "given\n" \ + "X = ['one', 'yeah']\n" \ + " and\n" \ + "Y = ['one', 'yeah', 'damn']\n" \ + "Y has 3 items whereas X has only 2", + )) + + + def test_function_decorated_with_wip_should_set_a_flag(self): + "@sure.work_in_progress should set an internal flag into `sure`" + + @sure.work_in_progress + def this_was_called(): + assert sure.it('is_running') + return True + + assert not sure._registry['is_running'] + assert this_was_called() + assert not sure._registry['is_running'] + + + def test_that_equals_fails(self): + "that() equals(string) when it's supposed to fail" + + something = "else" + + def fail(): + assert that('something').equals(something) + + assert that(fail).raises( + AssertionError, compat_repr( + "given\n" \ + "X = 'something'\n" \ + " and\n" \ + "Y = 'else'\n" \ + "X is 'something' whereas Y is 'else'", + )) + + + def test_deep_equals_weird(self): + part1 = [ + ('Bootstraping Redis role', []), + ('Restart scalarizr', []), + ('Rebundle server', ['rebundle']), + ('Use new role', ['rebundle']), + ('Restart scalarizr after bundling', ['rebundle']), + ('Bundling data', []), + ('Modifying data', []), + ('Reboot server', []), + ('Backuping data on Master', []), + ('Setup replication', []), + ('Restart scalarizr in slave', []), + ('Slave force termination', []), + ('Slave delete EBS', ['ec2']), + ('Setup replication for EBS test', ['ec2']), + ('Writing on Master, reading on Slave', []), + ('Slave -> Master promotion', []), + ('Restart farm', ['restart_farm']), + ] + + part2 = [ + ('Bootstraping Redis role', ['rebundle', 'rebundle', 'rebundle']), + ('Restart scalarizr', []), + ('Rebundle server', ['rebundle']), + ('Use new role', ['rebundle']), + ('Restart scalarizr after bundling', ['rebundle']), + ('Bundling data', []), + ('Modifying data', []), + ('Reboot server', []), + ('Backuping data on Master', []), + ('Setup replication', []), + ('Restart scalarizr in slave', []), + ('Slave force termination', []), + ('Slave delete EBS', ['ec2']), + ('Setup replication for EBS test', ['ec2']), + ('Writing on Master, reading on Slave', []), + ('Slave -> Master promotion', []), + ('Restart farm', ['restart_farm']), + ] - expect(that(part1).equals).when.called_with(part2).should.throw("") + expect(that(part1).equals).when.called_with(part2).should.throw("")