Esempio n. 1
0
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}

    animals = [animal("dog"), animal("cat"), animal("cow"), animal("cow"), animal("cow")]

    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"

    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"])

    assert that(animals).in_each("attributes['kind']").matches(["dog", "cat", "cow", "cow", "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"])
        )
Esempio n. 2
0
def test_that_something_iterable_matches_another():
    "that(something_iterable).matches(another_iterable)"

    # types must be unicode in py3, bute bytestrings in py2
    KlassOne = type("KlassOne" if PY3 else b"KlassOne", (object,), {})
    KlassTwo = type("KlassTwo" if PY3 else b"KlassTwo", (object,), {})
    one = [("/1", KlassOne), ("/2", KlassTwo)]

    two = [("/1", KlassOne), ("/2", KlassTwo)]

    assert that(one).matches(two)
    assert that(one).equals(two)

    def fail_1():
        assert that([1]).matches(xrange(2))

    class Fail2(object):
        def __init__(self):
            assert that(xrange(1)).matches([2])

    class Fail3(object):
        def __call__(self):
            assert that(xrange(1)).matches([2])

    xrange_name = xrange.__name__
    assert that(fail_1).raises("X is a list and Y is a {0} instead".format(xrange_name))
    assert that(Fail2).raises("X is a {0} and Y is a list instead".format(xrange_name))
    assert that(Fail3()).raises("X is a {0} and Y is a list instead".format(xrange_name))
Esempio n. 3
0
def test_that_something_iterable_matches_another():
    "that(something_iterable).matches(another_iterable)"

    KlassOne = type('KlassOne', (object,), {})
    KlassTwo = type('KlassTwo', (object,), {})
    one = [
        ("/1", KlassOne),
        ("/2", KlassTwo),
    ]

    two = [
        ("/1", KlassOne),
        ("/2", KlassTwo),
    ]

    assert that(one).matches(two)
    assert that(one).equals(two)

    def fail_1():
        assert that(range(1)).matches(xrange(2))

    class Fail2(object):
        def __init__(self):
            assert that(xrange(1)).matches(range(2))

    class Fail3(object):
        def __call__(self):
            assert that(xrange(1)).matches(range(2))

    assert that(fail_1).raises('X is a list and Y is a xrange instead')
    assert that(Fail2).raises('X is a xrange and Y is a list instead')
    assert that(Fail3()).raises('X is a xrange and Y is a list instead')
Esempio n. 4
0
def test_that_len_greater_than_should_raise_assertion_error():
    "that() len_greater_than(number) raise AssertionError"

    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))
Esempio n. 5
0
def test_that_len_lower_than_or_equals_should_raise_assertion_error():
    "that() len_lower_than_or_equals(number) raise AssertionError"

    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))
Esempio n. 6
0
def test_that_len_is():
    "that() len_is(number)"

    lst = range(1000)

    assert that(lst).len_is(1000)
    assert len(lst) == 1000
    assert that(lst).len_is(lst)
Esempio n. 7
0
def test_that_len_lower_than():
    "that() len_lower_than(number)"

    lst = list(range(100))
    lst2 = list(range(1000))

    assert that(lst).len_lower_than(101)
    assert len(lst) == 100
    assert that(lst).len_lower_than(lst2)
Esempio n. 8
0
def test_that_len_greater_than():
    "that() len_greater_than(number)"

    lst = range(1000)
    lst2 = range(100)

    assert that(lst).len_greater_than(100)
    assert len(lst) == 1000
    assert that(lst).len_greater_than(lst2)
Esempio n. 9
0
def test_get_argv_options_integration(context):
    u"Nose should parse sys.argv and figure out whether to run as integration"
    sys.argv = ['./manage.py', 'test', '--integration']
    runner = Nose()

    opts = runner.get_argv_options()
    assert that(opts['is_unit']).equals(False)
    assert that(opts['is_functional']).equals(False)
    assert that(opts['is_integration']).equals(True)
Esempio n. 10
0
def test_get_argv_options_simple(context):
    u"Nose should parse sys.argv"
    sys.argv = ['./manage.py', 'test']
    runner = Nose()

    opts = runner.get_argv_options()
    assert that(opts['is_unit']).equals(False)
    assert that(opts['is_functional']).equals(False)
    assert that(opts['is_integration']).equals(False)
Esempio n. 11
0
def test_that_is_a_matcher_should_absorb_callables_to_be_used_as_matcher():
    u"that.is_a_matcher should absorb callables to be used as matcher"
    @that.is_a_matcher
    def is_truthful(what):
        assert bool(what), '%s is so untrue' % (what)
        return 'foobar'

    assert that('friend').is_truthful()
    assert_equals(that('friend').is_truthful(), 'foobar')
Esempio n. 12
0
 def mock_get_paths_for(names, appending):
     k = expected_kinds.pop(0)
     assert that(names).is_a(list)
     assert that(appending).is_a(list)
     assert that(names).equals(['john', 'doe'])
     assert that(appending).equals(['tests', k])
     return [
         '/apps/john/tests/%s' % k,
         '/apps/doe/tests/%s' % k,
     ]
Esempio n. 13
0
def test_that_is_a_matcher_should_absorb_callables_to_be_used_as_matcher():
    "that.is_a_matcher should absorb callables to be used as matcher"

    @that.is_a_matcher
    def is_truthful(what):
        assert bool(what), "%s is so untrue" % (what)
        return "foobar"

    assert that("friend").is_truthful()
    assert_equals(that("friend").is_truthful(), "foobar")
Esempio n. 14
0
def test_that_len_greater_than_or_equals_should_raise_assertion_error():
    "that() len_greater_than_or_equals(number) raise AssertionError"

    lst = range(1000)
    try:
        that(lst).len_greater_than_or_equals(1001)
    except AssertionError, e:
        assert_equals(
            str(e),
            'the length of %r should be greater then or equals %d, but is %d' \
            % (lst, 1001, 1000))
Esempio n. 15
0
def test_that_len_lower_than_should_raise_assertion_error():
    "that() len_lower_than(number) raise AssertionError"

    lst = range(1000)
    try:
        that(lst).len_lower_than(1000)
    except AssertionError, e:
        assert_equals(
            str(e),
            'the length of %r should be lower then %d, but is %d' % \
            (lst, 1000, 1000))
Esempio n. 16
0
def test_that_len_lower_than_or_equals():
    "that() len_lower_than_or_equals(number)"

    lst = list(range(1000))
    lst2 = list(range(1001))

    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)
Esempio n. 17
0
def test_that_at_key_equals():
    "that().at(object).equals(object)"

    class Class:
        name = "some class"

    Object = Class()
    dictionary = {"name": "John"}

    assert that(Class).at("name").equals("some class")
    assert that(Object).at("name").equals("some class")
    assert that(dictionary).at("name").equals("John")
Esempio n. 18
0
def test_that_at_key_equals():
    "that().at(object).equals(object)"

    class Class:
        name = "some class"
    Object = Class()
    dictionary = {
        'name': 'John',
    }

    assert that(Class).at("name").equals('some class')
    assert that(Object).at("name").equals('some class')
    assert that(dictionary).at("name").equals('John')
Esempio n. 19
0
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''')
Esempio n. 20
0
def test_that_is_a():
    "that() is_a(object)"

    something = "something"

    assert that(something).is_a(text_type)
    assert isinstance(something, text_type)
Esempio n. 21
0
 def assertions():
     assert that(something).deep_equals({
         'one': 'yeah',
         'another': {
             'two': '$$',
         },
     })
Esempio n. 22
0
def test_that_contains_dictionary_keys():
    "that(dict(name='foobar')).contains('name')"

    data = dict(name="foobar")
    assert "name" in data
    assert "name" in data.keys()
    assert that(data).contains("name")
Esempio n. 23
0
    def the_providers_are_working(Then):
        Then.the_context_has_variables()
        assert hasattr(Then, "var1")
        assert hasattr(Then, "foobar")
        assert hasattr(Then, "__sure_providers_of__")

        providers = Then.__sure_providers_of__
        action = Then.the_context_has_variables.__name__

        providers_of_var1 = [p.__name__ for p in providers["var1"]]
        assert that(providers_of_var1).contains(action)

        providers_of_foobar = [p.__name__ for p in providers["foobar"]]
        assert that(providers_of_foobar).contains(action)

        return True
Esempio n. 24
0
    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 = Then.__sure_providers_of__
        action = Then.the_context_has_variables.__name__

        providers_of_var1 = [p.__name__ for p in providers["var1"]]
        assert that(providers_of_var1).contains(action)

        providers_of_JohnDoe = [p.__name__ for p in providers["JohnDoe"]]
        assert that(providers_of_JohnDoe).contains(action)

        return True
Esempio n. 25
0
    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 = Then.__sure_providers_of__
        action = Then.the_context_has_variables.__name__

        providers_of_var1 = [p.__name__ for p in providers['var1']]
        assert that(providers_of_var1).contains(action)

        providers_of_JohnDoe = [p.__name__ for p in providers['JohnDoe']]
        assert that(providers_of_JohnDoe).contains(action)

        return True
Esempio n. 26
0
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({
            '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 '$$'",
    ))
Esempio n. 27
0
    def the_providers_are_working(Then):
        Then.the_context_has_variables()
        assert hasattr(Then, 'var1')
        assert hasattr(Then, 'foobar')
        assert hasattr(Then, '__sure_providers_of__')

        providers = Then.__sure_providers_of__
        action = Then.the_context_has_variables.__name__

        providers_of_var1 = [p.__name__ for p in providers['var1']]
        assert that(providers_of_var1).contains(action)

        providers_of_foobar = [p.__name__ for p in providers['foobar']]
        assert that(providers_of_foobar).contains(action)

        return True
Esempio n. 28
0
def test_that_differs():
    "that() differs(object)"

    something = "something"

    assert that(something).differs("23123%FYTUGIHOfdf")
    assert something != "23123%FYTUGIHOfdf"
Esempio n. 29
0
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

        def __repr__(self):
            return '<%s:%s>' % (self.kind, self.name)

    shapes = [
        shape('circle'),
        shape('square'),
        shape('square'),
        shape('triangle'),
    ]

    assert shapes[0].name != 'square'
    assert shapes[3].name != 'square'

    assert shapes[1].name == 'square'
    assert shapes[2].name == 'square'

    assert that(shapes, within_range=(1, 2)). \
                           the_attribute("name"). \
                           equals('square')
Esempio n. 30
0
def test_that_equals():
    "that() equals(string)"

    something = "something"

    assert that("something").equals(something)
    assert something == "something"
Esempio n. 31
0
 def assertions():
     assert that({'two': 'yeah'}).deep_equals('two yeah')
Esempio n. 32
0
 def assertions():
     assert that(something).deep_equals({
         'one': 'oops',
     })
Esempio n. 33
0
 def assertions():
     assert that(something).deep_equals({
         'my::all_users': [
         {'name': 'John', 'age': 33, 'bar': 'foo'},
         ],
     })
Esempio n. 34
0
def test_that_raises():
    "that(callable, with_args=[arg1], and_kwargs={'arg2': 'value'}).raises(SomeException)"
    global called

    called = False

    def function(arg1=None, arg2=None):
        global called
        called = True
        if arg1 == 1 and arg2 == 2:
            raise RuntimeError('yeah, it failed')

        return "OK"

    try:
        function(1, 2)
        assert False, 'should not reach here'

    except RuntimeError as e:
        assert text_type(e) == 'yeah, it failed'

    except Exception:
        assert False, 'should not reach here'

    finally:
        assert called
        called = False

    assert_raises(RuntimeError, function, 1, 2)

    called = False
    assert_equals(function(3, 5), 'OK')
    assert called

    called = False
    assert that(function, with_args=[1], and_kwargs={'arg2': 2}). \
           raises(RuntimeError)
    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_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)
    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_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

    called = False
    assert that(function, with_kwargs={'arg1': 1, 'arg2': 2}). \
           raises(RuntimeError, r'it fail')
    assert called
Esempio n. 35
0
 def __call__(self):
     assert that(xrange(1)).matches([2])
Esempio n. 36
0
def test_that_contains_string():
    "that('foobar').contains('foo')"

    assert 'foo' in 'foobar'
    assert that('foobar').contains('foo')
Esempio n. 37
0
def test_that_contains_tuple():
    "that(('foobar', '123')).contains('foobar')"

    data = ('foobar', '123')
    assert 'foobar' in data
    assert that(data).contains('foobar')
Esempio n. 38
0
 def assertions():
     assert that(something).deep_equals(('one', 'yeah'))
Esempio n. 39
0
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'))
Esempio n. 40
0
def test_that_has():
    "that() has(object)"
    class Class:
        name = "some class"
    Object = Class()
    dictionary = {
        'name': 'John',
    }
    name = "john"

    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)

    assert 'name' in dictionary
    assert that(dictionary).has("name")
    assert that(dictionary).like("name")
    assert "name" in that(dictionary)

    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)
Esempio n. 41
0
 def assertions():
     assert that(something).deep_equals({
         'date': tomorrow,
     })
Esempio n. 42
0
 def fail_plural():
     assert that((1, 2)).is_empty
Esempio n. 43
0
 def assertions():
     # We can't use unicode in Py2, otherwise it will try to coerce
     assert that('foobar' if PY3 else b'foobar').contains(None)
Esempio n. 44
0
def test_that_doesnt_contain_string():
    "that('foobar').does_not_contain('123'), .doesnt_contain"

    assert '123' not in 'foobar'
    assert that('foobar').doesnt_contain('123')
    assert that('foobar').does_not_contain('123')
Esempio n. 45
0
def test_that_contains_list():
    "that(['foobar', '123']).contains('foobar')"

    data = ['foobar', '123']
    assert 'foobar' in data
    assert that(data).contains('foobar')
Esempio n. 46
0
 def the_providers_are_working(the):
     assert that(the.bad_action).raises(AssertionError, error)
     return True
Esempio n. 47
0
def test_that_contains_set():
    "that(set(['foobar', '123']).contains('foobar')"

    data = set(['foobar', '123'])
    assert 'foobar' in data
    assert that(data).contains('foobar')
Esempio n. 48
0
def test_that_looks_like():
    "that('String\\n with BREAKLINE').looks_like('string with breakline')"
    assert that('String\n with BREAKLINE').looks_like('string with breakline')
Esempio n. 49
0
 def depends_on_fails(the):
     assert that(the.my_action).raises(AssertionError, error)
     return True
Esempio n. 50
0
 def fail_1():
     assert that([1]).matches(xrange(2))
Esempio n. 51
0
 def i_can_use_actions(context):
     assert that(context.action1()).equals(context)
     assert that(context.action2()).equals(context)
     return True
Esempio n. 52
0
 def assertions():
     assert that(something).deep_equals(['one', 'yeah', 'damn'])
Esempio n. 53
0
 def __init__(self):
     assert that(xrange(1)).matches([2])
Esempio n. 54
0
 def fail():
     assert that('something').equals(something)
Esempio n. 55
0
 def fail():
     assert that(obj).is_empty
     assert False, 'should not reach here'
Esempio n. 56
0
 def assertions():
     assert that(something).deep_equals({'iterable': ['one', 'yeah']})
Esempio n. 57
0
 def assertions():
     assert that(something).deep_equals({
         'two': 'yeah',
     })
Esempio n. 58
0
def test_deep_equals_list_level1_success():
    "that(list) deep_equals(list) succeeding on level 1"

    something = ['one', 'yeah']
    assert that(something).deep_equals(['one', 'yeah'])
Esempio n. 59
0
 def fail_single():
     assert that((1, )).is_empty
Esempio n. 60
0
 def assertions():
     assert that(something).deep_equals({
         'date': None,
     })