def test_bind_list(self): add_simplads = SM.add_simplads({ '1': ListSimplad(), '2': ListSimplad(), '3': ListSimplad(), '4': ListSimplad(), '5': ListSimplad()}) order = SM.set_simplad_order(['1','2','3','4','5']) sm = (F() >> add_simplads >> order)(SM.make()) val = [[[[[4]]]]] bound = SM.unit(sm)(val) def double_simplad_result(i): return SimpladResult(val=i*2, delta_map={}) bound = SM.bind(double_simplad_result)( (sm, BindArgs(bound=bound, deltas=[]))) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) expect(bound[1].bound).to_equal( Bound(unbound=[Bound(unbound=[Bound(unbound=[Bound(unbound=[Bound( unbound=[512], annotation=None)], annotation=None)], annotation=None)], annotation=None)], annotation=None))
def test_bind_maybe(self): add_simplads = SM.add_simplads({ '1': MS(), '2': MS(), '3': MS(), '4': MS(), '5': MS() }) order = SM.set_simplad_order(['1','2','3','4','5']) sm = (F() >> add_simplads >> order)(SM.make()) val = 4 bound = SM.unit(sm)(val) def double_simplad_result(i): return SimpladResult(val=i*2, delta_map={}) bound = SM.bind(double_simplad_result)( (sm, BindArgs(bound=bound, deltas=[]))) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) bound = SM.bind(double_simplad_result)(bound) expect(bound[1].bound).to_equal( Bound(unbound=Bound(unbound=Bound(unbound=Bound(unbound=Bound( unbound=512, annotation=MaybeType.has_value), annotation=MaybeType.has_value), annotation=MaybeType.has_value), annotation=MaybeType.has_value), annotation=MaybeType.has_value))
def test_bind_two_layers(self): bound_before = Bound(unbound=Bound(unbound=1, annotation=8), annotation=9) after = bind(bind(double))(BindArgs(bound=bound_before, deltas=[])) expect(after).to_equal( BindArgs(bound=Bound(unbound=Bound(unbound=2, annotation=8), annotation=9), deltas=[]))
def test_maybe_simplad_no_value(self): s = Bundle().set_simplads({'a': MaybeSimplad()}).unit(4) result = s.pipe([ lift(lambda i: 2 * i), lambda i: res(val=2 * i), lambda i: res(delta_map={'a': MaybeDeltaMaker.no_value}), lift(lambda i: 2 * i) ]) expect(s.bound).to_equal(Bound(unbound=None, annotation=False)) expect(result).to_equal(Bound(unbound=None, annotation=False))
def test_bind_twice(self): bound_before = Bound(unbound=1, annotation=[]) after_first = bind(double)(BindArgs(bound=bound_before, deltas=[])) after_second = bind(double)(after_first) expect(after_second).to_equal( BindArgs(bound=Bound(unbound=4, annotation=[]), deltas=[]))
def test_merge_deltas(self): result_value = BindArgs(bound=8, deltas=[ 'higher deltas', WrappedDelta(type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.default, delta=None), WrappedDelta(type=DeltaType.configured, delta=False)])]) higher_deltas = ['higher deltas'] bound_before = Bound(unbound=4, annotation=MaybeDeltaMaker.has_value) bound_result, higher_deltas = bind(get_echo(result_value))( BindArgs(bound=bound_before, deltas=higher_deltas)) expect(bound_result).to_equal(Bound(unbound=None, annotation=False)) expect(higher_deltas).to_equal(['higher deltas'])
def test_bind_has_value_to_has_value(self): result_value = BindArgs(bound=8, deltas=[ 'higher deltas', WrappedDelta(type=DeltaType.default, delta=None) ]) higher_deltas = ['higher deltas'] bound_before = Bound(unbound=4, annotation=ErrorType.none) bound_result, higher_deltas = bind(get_echo(result_value))(BindArgs( bound=bound_before, deltas=higher_deltas)) expect(bound_result).to_equal( Bound(unbound=8, annotation=ErrorType.none)) expect(higher_deltas).to_equal(['higher deltas'])
def test_bind_has_no_value_to_has_value(self): result_delta = WrappedDelta(type=DeltaType.configured, delta=ErrorDeltaMaker.error('error text')) higher_deltas_after = ['higher deltas', result_delta] result_value = BindArgs(bound=None, deltas=higher_deltas_after) higher_deltas_before = ['higher deltas'] bound_before = Bound(unbound=4, annotation=ErrorType.none) bound_result, higher_deltas = bind(get_echo(result_value))(BindArgs( bound=bound_before, deltas=higher_deltas_before)) expect(bound_result).to_equal( Bound(unbound=ErrorRes(has_error=True, error_text='error text'), annotation=ErrorType.error)) expect(higher_deltas).to_equal(['higher deltas'])
def test_read(self): bound_before = Bound(unbound=1, annotation=[]) obj = {'key': 'correct val'} echo_this = BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.configured, delta=ReaderDeltaMaker.set_obj( keys=[], root_obj=obj)) ]) after_first = bind(lambda i: echo_this)(BindArgs(bound=bound_before, deltas=[])) echo_this = BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.configured, delta=ReaderDeltaMaker.read( ['key'])) ]) after_second = bind(lambda i: echo_this)(after_first) expect(after_second.bound.unbound).to_equal( ReaderResult(read_val='correct val', val=8))
def test_maybe_simplad(self): s = Bundle().set_simplads({'a': MaybeSimplad()}).unit(4) s.pipe([ lift(lambda i: 2 * i), lambda i: res(val=2 * i), lift(lambda i: 2 * i) ]) expect(s.bound).to_equal(Bound(unbound=32, annotation=True))
def test_read_deep(self): bound_before = Bound(unbound=1, annotation=[]) obj = {'a': {'b': {'c': {'x': {'y': {'z': 'located'}}}}}} echo_this = BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.configured, delta=ReaderDeltaMaker.set_obj( keys=[], root_obj=obj)) ]) after_first = bind(lambda i: echo_this)(BindArgs(bound=bound_before, deltas=[])) echo_this = BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.configured, delta=ReaderDeltaMaker.read([ 'a', 'b', 'c', 'x', 'y', 'z' ])) ]) after_second = bind(lambda i: echo_this)(after_first) expect(after_second.bound.unbound).to_equal( ReaderResult(read_val='located', val=8))
def test_bind_two_layers(self): bound_before = Bound( unbound=Bound(unbound=4, annotation=MaybeDeltaMaker.has_value), annotation=MaybeDeltaMaker.has_value) result = BindArgs(bound=8, deltas=[ 'higher deltas', WrappedDelta(type=DeltaType.default, delta=True), WrappedDelta(type=DeltaType.default, delta=True)]) after = bind(bind(get_echo(result)))( BindArgs(bound=bound_before, deltas=['higher deltas'])) outer_bound, deltas = after inner_bound, outer_annotation = outer_bound value, inner_annotation = inner_bound expect(outer_annotation).to_equal(MaybeDeltaMaker.has_value) expect(inner_annotation).to_equal(MaybeDeltaMaker.has_value) expect(value).to_equal(8) expect(deltas[0]).to_equal('higher deltas')
def test_bind_has_value_to_has_no_value(self): result_value = BindArgs(bound='value', deltas=['higher deltas', WrappedDelta(type=DeltaType.configured, delta=MaybeDeltaMaker.no_value)]) higher_deltas = ['higher deltas'] bound_before = Bound(unbound=4, annotation=MaybeDeltaMaker.has_value) bound_result, higher_deltas = bind(get_echo(result_value))(BindArgs( bound=bound_before, deltas=higher_deltas)) expect(bound_result).to_equal((None, MaybeDeltaMaker.no_value)) expect(higher_deltas).to_equal(['higher deltas'])
def test_merge_deltas(self): result_value = BindArgs( bound=8, deltas=[ 'higher deltas', WrappedDelta(type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.default, delta=None), WrappedDelta(type=DeltaType.configured, delta=ErrorDeltaMaker.finish()) ]) ]) higher_deltas = ['higher deltas'] bound_before = Bound(unbound=4, annotation=ErrorType.none) bound_result, higher_deltas = bind(get_echo(result_value))(BindArgs( bound=bound_before, deltas=higher_deltas)) expect(bound_result).to_equal( Bound(unbound=ErrorRes(has_error=False, result=8), annotation=ErrorType.finish)) expect(higher_deltas).to_equal(['higher deltas'])
def test_write(self): bound_before = Bound(unbound=1, annotation=[]) obj = {'key': 'not written'} result = BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.configured, delta=WriterDeltaMaker.set_obj( new_data='a', keys=['key'], root_obj=obj)) ]) after_first = bind(lambda i: result)(BindArgs( bound=bound_before, deltas=[WrappedDelta(type=DeltaType.configured, delta=None)])) after_second = bind(double)(after_first) expect(obj['key']).to_equal('a')
def test_bind_single_layer(self): bound_before = Bound(unbound=1, annotation=[]) after = bind(double)(BindArgs(bound=bound_before, deltas=[])) expect(after).to_equal( BindArgs(bound=Bound(unbound=2, annotation=[]), deltas=[]))
def test_unit_with_maybe_simplad(self): s = Bundle().set_simplads({'a': MaybeSimplad()}).unit(unbound=4) expect(s.bound).to_equal(Bound(unbound=4, annotation=True))