Ejemplo n.º 1
0
    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))
Ejemplo n.º 2
0
    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'])
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    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')
                                ])
                        ])
                ]))
Ejemplo n.º 5
0
 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=[]))
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
    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=[]))
Ejemplo n.º 8
0
    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']))
Ejemplo n.º 9
0
    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'])
Ejemplo n.º 10
0
    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'])
Ejemplo n.º 11
0
 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))
Ejemplo n.º 12
0
    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']))
Ejemplo n.º 13
0
    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'])
Ejemplo n.º 14
0
    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'])
Ejemplo n.º 15
0
    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))
Ejemplo n.º 16
0
    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))
Ejemplo n.º 17
0
    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')
Ejemplo n.º 18
0
    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), []))
Ejemplo n.º 19
0
    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'])
Ejemplo n.º 20
0
    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')
Ejemplo n.º 21
0
    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)]))
Ejemplo n.º 22
0
    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'])
Ejemplo n.º 23
0
 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)
                     ])
Ejemplo n.º 24
0
 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)
Ejemplo n.º 25
0
 def double(i):
     return BindArgs(
         bound=2 * i[0],
         deltas=[WrappedDelta(type=DeltaType.default, delta=None)])
Ejemplo n.º 26
0
 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
Ejemplo n.º 27
0
 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
Ejemplo n.º 28
0
def return_with_delta(delta):
    return lambda i: BindArgs(bound=i[0], deltas=[delta])
Ejemplo n.º 29
0
 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]))
Ejemplo n.º 30
0
 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]))