[core] Testing and fixing new args/kwargs behaviour.
This commit is contained in:
@ -105,23 +105,24 @@ class Bag:
|
|||||||
if isinstance(other, Bag) and other.args == self.args and other.kwargs == self.kwargs:
|
if isinstance(other, Bag) and other.args == self.args and other.kwargs == self.kwargs:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# tuple of (tuple, dict)
|
# tuple
|
||||||
if isinstance(other, tuple) and len(other) == 2 and other[0] == self.args and other[1] == self.kwargs:
|
if isinstance(other, tuple):
|
||||||
return True
|
# self == ()
|
||||||
|
if not len(other):
|
||||||
|
return not self.args and not self.kwargs
|
||||||
|
|
||||||
# tuple (aka args)
|
if isinstance(other[-1], dict):
|
||||||
if isinstance(other, tuple) and other == self.args:
|
# self == (*args, {**kwargs}) ?
|
||||||
return True
|
return other[:-1] == self.args and other[-1] == self.kwargs
|
||||||
|
|
||||||
|
# self == (*args) ?
|
||||||
|
return other == self.args and not self.kwargs
|
||||||
|
|
||||||
# dict (aka kwargs)
|
# dict (aka kwargs)
|
||||||
if isinstance(other, dict) and not self.args and other == self.kwargs:
|
if isinstance(other, dict) and not self.args and other == self.kwargs:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# arg0
|
return len(self.args) == 1 and not self.kwargs and self.args[0] == other
|
||||||
if len(self.args) == 1 and not len(self.kwargs) and self.args[0] == other:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<{} ({})>'.format(
|
return '<{} ({})>'.format(
|
||||||
|
|||||||
@ -12,7 +12,7 @@ def test_node_string():
|
|||||||
output = context.get_buffer()
|
output = context.get_buffer()
|
||||||
|
|
||||||
assert len(output) == 1
|
assert len(output) == 1
|
||||||
assert output[0] == (('foo', ), {})
|
assert output[0] == 'foo'
|
||||||
|
|
||||||
def g():
|
def g():
|
||||||
yield 'foo'
|
yield 'foo'
|
||||||
@ -23,8 +23,8 @@ def test_node_string():
|
|||||||
output = context.get_buffer()
|
output = context.get_buffer()
|
||||||
|
|
||||||
assert len(output) == 2
|
assert len(output) == 2
|
||||||
assert output[0] == (('foo', ), {})
|
assert output[0] == 'foo'
|
||||||
assert output[1] == (('bar', ), {})
|
assert output[1] == 'bar'
|
||||||
|
|
||||||
|
|
||||||
def test_node_bytes():
|
def test_node_bytes():
|
||||||
@ -36,7 +36,7 @@ def test_node_bytes():
|
|||||||
|
|
||||||
output = context.get_buffer()
|
output = context.get_buffer()
|
||||||
assert len(output) == 1
|
assert len(output) == 1
|
||||||
assert output[0] == ((b'foo', ), {})
|
assert output[0] == b'foo'
|
||||||
|
|
||||||
def g():
|
def g():
|
||||||
yield b'foo'
|
yield b'foo'
|
||||||
@ -47,8 +47,8 @@ def test_node_bytes():
|
|||||||
output = context.get_buffer()
|
output = context.get_buffer()
|
||||||
|
|
||||||
assert len(output) == 2
|
assert len(output) == 2
|
||||||
assert output[0] == ((b'foo', ), {})
|
assert output[0] == b'foo'
|
||||||
assert output[1] == ((b'bar', ), {})
|
assert output[1] == b'bar'
|
||||||
|
|
||||||
|
|
||||||
def test_node_dict():
|
def test_node_dict():
|
||||||
@ -125,3 +125,54 @@ def test_node_tuple():
|
|||||||
assert len(output) == 2
|
assert len(output) == 2
|
||||||
assert output[0] == ('foo', 'bar')
|
assert output[0] == ('foo', 'bar')
|
||||||
assert output[1] == ('foo', 'baz')
|
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})
|
||||||
|
|||||||
@ -92,13 +92,28 @@ def test_pickle():
|
|||||||
assert unpickled == bag
|
assert unpickled == bag
|
||||||
|
|
||||||
|
|
||||||
def test_eq_operator():
|
def test_eq_operator_bag():
|
||||||
assert Bag('foo') == Bag('foo')
|
assert Bag('foo') == Bag('foo')
|
||||||
assert Bag('foo') != Bag('bar')
|
assert Bag('foo') != Bag('bar')
|
||||||
assert Bag('foo') is not Bag('foo')
|
assert Bag('foo') is not Bag('foo')
|
||||||
assert Bag('foo') != Token('foo')
|
assert Bag('foo') != Token('foo')
|
||||||
assert Token('foo') != Bag('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():
|
def test_repr():
|
||||||
bag = Bag('a', a=1)
|
bag = Bag('a', a=1)
|
||||||
|
|||||||
Reference in New Issue
Block a user