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_merge_deltas(self): bound_before = unit(echo)(4) delta = WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(delta=LogDeltaMaker.messages_and_listener(['one' ]), type=DeltaType.configured), WrappedDelta(delta=LogDeltaMaker.messages(['two']), type=DeltaType.configured) ]) args = bind(return_with_delta(delta))(BindArgs(bound=bound_before, deltas=['higher delts' ])) result = BindArgs(bound=8, deltas=[ 'delts', WrappedDelta(type=DeltaType.configured, delta=(['three'], [])) ]) res = bind(get_echo(result))(BindArgs(bound=args.bound, deltas=[])) (value, listeners), deltas = res func, log = listeners['log'] expect(log).to_equal(['one', 'two', 'three'])
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_with_deltas(self): def double(i): return BindArgs(bound=2 * i[0], deltas=[ WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.configured, delta='a'), WrappedDelta(type=DeltaType.configured, delta='b') ]), WrappedDelta(type=DeltaType.default, delta=None) ]) bound_before = Bound(unbound=[1, 2, 3, 4], annotation=None) after = bind(double)(BindArgs(bound=bound_before, deltas=[])) expect(after).to_equal( BindArgs( bound=Bound(unbound=[2, 4, 6, 8], annotation=None), deltas=[ WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.configured, delta='a'), WrappedDelta(type=DeltaType.configured, delta='b') ]), WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.configured, delta='a'), WrappedDelta(type=DeltaType.configured, delta='b') ]), WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.configured, delta='a'), WrappedDelta(type=DeltaType.configured, delta='b') ]), WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.configured, delta='a'), WrappedDelta(type=DeltaType.configured, delta='b') ]) ]) ]))
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 run(func, annotation, unbound, higher_deltas): bounds = [] deltas = [] for elem in unbound: res = func(BindArgs(bound=elem, deltas=higher_deltas)) bounds.append(res.bound) deltas.append(res.deltas) deltas = ListSimplad.rotate(deltas) return BindArgs(bound=bounds, deltas=deltas)
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_bind_two_layers(self): bound_before = Bound(unbound=Bound(unbound=4, annotation={}), annotation={}) delta = WrappedDelta(type=DeltaType.configured, delta=([], [])) result = BindArgs(bound=8, deltas=['delts', delta, delta]) after = bind(bind(get_echo(result)))(BindArgs(bound=bound_before, deltas=['delts'])) expect(after).to_equal((((8, {}), {}), ['delts']))
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=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_single_layer_with_read(self): bound_before = unit(echo)(4) echo_this = BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.configured, delta=ReaderDeltaMaker.read( ['key'])) ]) after = bind(lambda i: echo_this)(BindArgs(bound=bound_before, deltas=[])) expect(after.bound.unbound).to_equal( ReaderResult(read_val='not initialised', val=8))
def test_bind_single_layer(self): bound_before = Bound(unbound=4, annotation={}) delta = (['one'], []) result = BindArgs(bound=8, deltas=[ 'delts', WrappedDelta(type=DeltaType.configured, delta=delta) ]) after = bind(get_echo(result))(BindArgs(bound=bound_before, deltas=['delts'])) expect(after).to_equal(((8, {}), ['delts']))
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_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_with_fail(self): add_simplads = SM.add_simplads({ '1': MS(), '2': MS(), '3': ListSimplad(), '4': ListSimplad(), '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={}) def double_simplad_result_fail_2(i): return SimpladResult(val=i*2, delta_map={ '5': MaybeType.no_value }) 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_fail_2)(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=None, annotation=MaybeType.no_value))
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_single_layer(self): def double(i): return BindArgs( bound=2 * i[0], deltas=[WrappedDelta(type=DeltaType.default, delta=None)]) bound_before = Bound(unbound=[1, 2, 3, 4], annotation=None) after = bind(double)(BindArgs(bound=bound_before, deltas=[])) expect(after).to_equal((([2, 4, 6, 8], None), []))
def test_single_message(self): def add_to_log(log, message): log.append(message) return log bound_before = Bound(unbound=4, annotation={'log': (add_to_log, [])}) delta = (['one'], []) result = BindArgs(bound=8, deltas=[ 'delts', WrappedDelta(type=DeltaType.configured, delta=delta) ]) res = bind(get_echo(result))(BindArgs(bound=bound_before, deltas=['delts'])) (value, listeners), deltas = res func, log = listeners['log'] expect(log).to_equal(['one'])
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_box_four(a): add_simplads = SM.add_simplads({ 's1': ListSimplad(), 's2': ListSimplad(), 's3': ListSimplad(), 's4': ListSimplad()}) order = SM.set_simplad_order(['s4','s3','s2','s1']) sm = (F() >> add_simplads >> order)(SM.make()) boxed = SM.get_box(sm)(lambda x: x)( BindArgs(bound=SimpladResult(val=8, delta_map={'s2': MaybeType.no_value}), deltas=[])) expect(boxed).to_equal( BindArgs(bound=8, deltas=[ WrappedDelta(type=DeltaType.default, delta=None), WrappedDelta(type=DeltaType.configured, delta=MaybeType.no_value), WrappedDelta(type=DeltaType.default, delta=None), WrappedDelta(type=DeltaType.default, delta=None)]))
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 double(i): return BindArgs(bound=2 * i[0], deltas=[ WrappedDelta( type=DeltaType.list, delta=[ WrappedDelta(type=DeltaType.configured, delta='a'), WrappedDelta(type=DeltaType.configured, delta='b') ]), WrappedDelta(type=DeltaType.default, delta=None) ])
def run(func, annotation, unbound, higher_deltas): if annotation is MaybeType.has_value: return func(BindArgs(bound=unbound, deltas=higher_deltas)) return BindArgs(bound=None, deltas=higher_deltas)
def double(i): return BindArgs( bound=2 * i[0], deltas=[WrappedDelta(type=DeltaType.default, delta=None)])
def pipe(self, funcs): bind_args = BindArgs(bound=self.bound, deltas=[]) for func in funcs: self.sm, bind_args = SimpladMonad.bind(func)((self.sm, bind_args)) self.bound = bind_args.bound return self.bound
def run(self, func): bind_args = BindArgs(bound=self.bound, deltas=[]) self.sm, bind_args = SimpladMonad.bind(func)((self.sm, bind_args)) self.bound = bind_args.bound return self.bound
def return_with_delta(delta): return lambda i: BindArgs(bound=i[0], deltas=[delta])
def test_run_when_no_value(self): expect(run(get_echo(4), ErrorType.error, 2, ['higher deltas', True])).to_equal( BindArgs(bound=2, deltas=['higher deltas', True]))
def test_run_when_no_value(self): expect(run(get_echo(4), (False, False), 2, ['higher deltas', True])).to_equal( BindArgs(bound=None, deltas=['higher deltas', True]))