[core] Testing and fixing new args/kwargs behaviour.

This commit is contained in:
Romain Dorgueil
2017-10-22 18:00:16 +02:00
parent bc01b5d404
commit 28fe41c0bd
3 changed files with 87 additions and 20 deletions

View File

@ -96,7 +96,7 @@ class Bag:
@classmethod
def inherit(cls, *args, **kwargs):
return cls(*args, _flags=(INHERIT_INPUT, ), **kwargs)
return cls(*args, _flags=(INHERIT_INPUT,), **kwargs)
def __eq__(self, other):
# XXX there are overlapping cases, but this is very handy for now. Let's think about it later.
@ -105,23 +105,24 @@ class Bag:
if isinstance(other, Bag) and other.args == self.args and other.kwargs == self.kwargs:
return True
# tuple of (tuple, dict)
if isinstance(other, tuple) and len(other) == 2 and other[0] == self.args and other[1] == self.kwargs:
return True
# tuple
if isinstance(other, tuple):
# self == ()
if not len(other):
return not self.args and not self.kwargs
# tuple (aka args)
if isinstance(other, tuple) and other == self.args:
return True
if isinstance(other[-1], dict):
# self == (*args, {**kwargs}) ?
return other[:-1] == self.args and other[-1] == self.kwargs
# self == (*args) ?
return other == self.args and not self.kwargs
# dict (aka kwargs)
if isinstance(other, dict) and not self.args and other == self.kwargs:
return True
# arg0
if len(self.args) == 1 and not len(self.kwargs) and self.args[0] == other:
return True
return False
return len(self.args) == 1 and not self.kwargs and self.args[0] == other
def __repr__(self):
return '<{} ({})>'.format(
@ -135,7 +136,7 @@ class Bag:
class LoopbackBag(Bag):
default_flags = (LOOPBACK, )
default_flags = (LOOPBACK,)
class ErrorBag(Bag):

View File

@ -12,7 +12,7 @@ def test_node_string():
output = context.get_buffer()
assert len(output) == 1
assert output[0] == (('foo', ), {})
assert output[0] == 'foo'
def g():
yield 'foo'
@ -23,8 +23,8 @@ def test_node_string():
output = context.get_buffer()
assert len(output) == 2
assert output[0] == (('foo', ), {})
assert output[1] == (('bar', ), {})
assert output[0] == 'foo'
assert output[1] == 'bar'
def test_node_bytes():
@ -36,7 +36,7 @@ def test_node_bytes():
output = context.get_buffer()
assert len(output) == 1
assert output[0] == ((b'foo', ), {})
assert output[0] == b'foo'
def g():
yield b'foo'
@ -47,8 +47,8 @@ def test_node_bytes():
output = context.get_buffer()
assert len(output) == 2
assert output[0] == ((b'foo', ), {})
assert output[1] == ((b'bar', ), {})
assert output[0] == b'foo'
assert output[1] == b'bar'
def test_node_dict():
@ -125,3 +125,54 @@ def test_node_tuple():
assert len(output) == 2
assert output[0] == ('foo', 'bar')
assert output[1] == ('foo', 'baz')
def test_node_tuple_chained():
strategy = NaiveStrategy(GraphExecutionContextType=BufferingGraphExecutionContext)
def uppercase(*args):
return tuple(map(str.upper, args))
def f():
return 'foo', 'bar'
graph = Graph(f, uppercase)
context = strategy.execute(graph)
output = context.get_buffer()
assert len(output) == 1
assert output[0] == ('FOO', 'BAR')
def g():
yield 'foo', 'bar'
yield 'foo', 'baz'
graph = Graph(g, uppercase)
context = strategy.execute(graph)
output = context.get_buffer()
assert len(output) == 2
assert output[0] == ('FOO', 'BAR')
assert output[1] == ('FOO', 'BAZ')
def test_node_tuple_dict():
def f():
return 'foo', 'bar', {'id': 1}
with BufferingNodeExecutionContext(f) as context:
context.write_sync(Bag())
output = context.get_buffer()
assert len(output) == 1
assert output[0] == ('foo', 'bar', {'id': 1})
def g():
yield 'foo', 'bar', {'id': 1}
yield 'foo', 'baz', {'id': 2}
with BufferingNodeExecutionContext(g) as context:
context.write_sync(Bag())
output = context.get_buffer()
assert len(output) == 2
assert output[0] == ('foo', 'bar', {'id': 1})
assert output[1] == ('foo', 'baz', {'id': 2})

View File

@ -92,13 +92,28 @@ def test_pickle():
assert unpickled == bag
def test_eq_operator():
def test_eq_operator_bag():
assert Bag('foo') == Bag('foo')
assert Bag('foo') != Bag('bar')
assert Bag('foo') is not Bag('foo')
assert Bag('foo') != Token('foo')
assert Token('foo') != Bag('foo')
def test_eq_operator_tuple_mixed():
assert Bag('foo', bar='baz') == ('foo', {'bar': 'baz'})
assert Bag('foo') == ('foo', {})
assert Bag() == ({}, )
def test_eq_operator_tuple_not_mixed():
assert Bag('foo', 'bar') == ('foo', 'bar')
assert Bag('foo') == ('foo', )
assert Bag() == ()
def test_eq_operator_dict():
assert Bag(foo='bar') == {'foo': 'bar'}
assert Bag(foo='bar', corp='acme') == {'foo': 'bar', 'corp': 'acme', }
assert Bag(foo='bar', corp='acme') == {'corp': 'acme', 'foo': 'bar', }
assert Bag() == {}
def test_repr():
bag = Bag('a', a=1)