-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathliquidata_test.py
785 lines (591 loc) · 21.6 KB
/
liquidata_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
from operator import itemgetter, attrgetter
from functools import reduce
from argparse import Namespace
from copy import copy
import itertools as it
from pytest import mark, raises
xfail = mark.xfail
TODO = mark.xfail(reason='TODO')
GETITEM_FUNDAMENTALLY_BROKEN = xfail(reason="__getitem__ can't distinguish x[a,b] from x[(a,b)]")
parametrize = mark.parametrize
from hypothesis import given
from hypothesis import assume
from hypothesis.strategies import tuples
from hypothesis.strategies import integers
from hypothesis.strategies import none
from hypothesis.strategies import one_of, sampled_from
from testhelpers import *
###################################################################
def test_map():
from liquidata import pipe
data = list(range(10))
f, = symbolic_functions('f')
assert pipe(f)(data) == list(map(f, data))
def test_source():
from liquidata import pipe, source
data = list(range(10))
f, = symbolic_functions('f')
assert pipe(source(data), f) == list(map(f, data))
def test_source_rshift():
from liquidata import pipe, source
data = list(range(10))
f, = symbolic_functions('f')
assert pipe(data >> source, f) == list(map(f, data))
def test_source_rshift():
from liquidata import pipe, source
data = list(range(10))
f, = symbolic_functions('f')
assert pipe(source << data, f) == list(map(f, data))
def test_filter():
from liquidata import pipe
data = list(range(10))
assert pipe({odd})(data) == list(filter(odd, data))
def test_filter_with_key():
from liquidata import pipe, arg as _
data = list(range(10))
assert pipe({odd : _+1})(data) == list(filter(even, data))
def test_branch():
from liquidata import pipe, sink
data = list(range(10))
branch = []
main = pipe([sink(branch.append)])(data)
assert main == data
assert branch == data
def test_integration_1():
from liquidata import pipe, arg as _, sink
data = range(20)
f, g, h = square, (_ + 1), (_ + 2)
a, b, c = odd , (_ > 50), (_ < 100)
s = []
t = pipe(f,
{a},
[g, {b}, sink(s.append)],
h,
{c})(data)
assert s == list(filter(b, map(g, filter(a, map(f, data)))))
assert t == list(filter(c, map(h, filter(a, map(f, data)))))
def test_fold_and_return():
from liquidata import pipe, out
data = range(3)
assert pipe(out(sym_add))(data) == reduce(sym_add, data)
def test_fold_and_named_return():
from liquidata import pipe, out
data = range(3)
assert pipe(out.total(sym_add))(data).total == reduce(sym_add, data)
def test_fold_with_initial_value():
from liquidata import pipe, out
data = range(3)
assert pipe(out(sym_add, 99))(data) == reduce(sym_add, data, 99)
def test_fold_with_initial_value_named():
from liquidata import pipe, out
data = range(3)
net = pipe(out.total(sym_add, 99))
assert net(data).total == reduce(sym_add, data, 99)
def test_fold_into_set():
from liquidata import pipe, out, into
data = 'abracadabra'
assert pipe(out(into(set)))(data) == set(data)
def test_fold_into_Count():
from collections import Counter
from liquidata import pipe, out, into
data = 'abracadabra'
assert pipe(out(into(Counter)))(data) == Counter(data)
def test_fold_into_named_out():
from liquidata import pipe, out, into
data = 'abracadabra'
assert pipe(out.X(into(set)))(data).X == set(data)
def test_return_value_from_branch():
from liquidata import pipe, out
data = range(3)
result = pipe([out.branch(sym_add)],
out.main (sym_mul))(data)
assert result.main == reduce(sym_mul, data)
assert result.branch == reduce(sym_add, data)
def test_implicit_collect_into_list_named():
from liquidata import pipe, out
data = range(3)
assert pipe(out.everything)(data).everything == list(data)
def test_implicit_collect_into_list_nameless_with_call():
from liquidata import pipe, out
data = range(3)
assert pipe(out())(data) == list(data)
def test_implicit_collect_into_list_nameless_without_call():
from liquidata import pipe, out
data = range(3)
assert pipe(out)(data) == list(data)
def test_more_than_one_implicit_anonymous_out():
from liquidata import pipe
f,g = symbolic_functions('fg')
data = range(3)
res = pipe([f], g)(data)
returns = getattr(res, 'return')
assert returns[0] == list(map(f, data))
assert returns[1] == list(map(g, data))
def test_implicit_anonymous_and_named_outs():
from liquidata import pipe, out
f,g = symbolic_functions('fg')
data = range(3)
res = pipe([f, out.branch], g)(data)
assert res.branch == list(map(f, data))
assert vars(res)['return'][0] == list(map(g, data))
def test_nested_branches():
from liquidata import pipe, out
f,g,h,i = symbolic_functions('fghi')
data = range(3)
res = pipe([[f, out.BB], g, out.BM],
[h, out.MB], i, out.MM )(data)
assert res.BB == list(map(f, data))
assert res.BM == list(map(g, data))
assert res.MB == list(map(h, data))
assert res.MM == list(map(i, data))
def test_join():
from liquidata import pipe, join
assert pipe(join)(('abc', 'd', '', 'efgh')) == list('abcdefgh')
def test_flat():
from liquidata import pipe, flat
data = range(4)
f = range
assert pipe(flat(f))(data) == list(it.chain(*map(f, data)))
def test_pipe_as_function():
from liquidata import pipe
f,g = symbolic_functions('fg')
pipe_fn = pipe(f,g).fn(tuple)
assert pipe_fn(6) == (g(f(6)),)
def test_pipe_as_safe_function():
from liquidata import pipe
f,g = symbolic_functions('fg')
pipe_fn = pipe(f,g).fn()
assert pipe_fn(6) == g(f(6))
def test_pipe_as_multi_arg_function():
from liquidata import pipe
f, = symbolic_functions('f')
pipe_fn = pipe(sym_add, f).fn(tuple)
assert pipe_fn(6,7) == (f(sym_add(6,7)),)
def test_pipe_as_safe_multi_arg_function():
from liquidata import pipe
f, = symbolic_functions('f')
pipe_fn = pipe(sym_add, f).fn()
assert pipe_fn(6,7) == f(sym_add(6,7))
def test_pipe_as_function_on_filter():
from liquidata import pipe
f = odd
pipe_fn = pipe({f}).fn(tuple)
assert pipe_fn(3) == (3,)
assert pipe_fn(4) == ()
def test_pipe_as_safe_function_on_filter():
from liquidata import pipe, Void
f = odd
pipe_fn = pipe({f}).fn()
assert pipe_fn(3) == 3
assert pipe_fn(4) == Void
def test_pipe_as_function_on_flat():
from liquidata import pipe, flat
f = range
pipe_fn = pipe(flat(f)).fn(tuple)
assert pipe_fn(3) == (0,1,2)
assert pipe_fn(5) == (0,1,2,3,4)
def test_pipe_as_safe_function_on_flat():
from liquidata import pipe, flat, Many
f = range
pipe_fn = pipe(flat(f)).fn()
assert pipe_fn(3) == Many((0,1,2))
assert pipe_fn(5) == Many((0,1,2,3,4))
def test_Void_str():
from liquidata import Void
assert str(Void) == 'Void'
def test_Void_repr():
from liquidata import Void
assert repr(Void) == 'Void'
def test_Many_str():
from liquidata import Many
assert str(Many((1,2,3))) == 'Many(1, 2, 3)'
def test_Many_repr():
from liquidata import Many
assert repr(Many((1,2,3))) == 'Many((1, 2, 3))'
def test_pipe_as_component():
from liquidata import pipe, pipe
data = range(3,6)
a,b,f,g = symbolic_functions('abfg')
a_pipe = pipe(f, g)
assert pipe(a, a_pipe, b)(data) == list(map(b, map(g, map(f, map(a, data)))))
def test_pick_item():
from liquidata import pipe, item as pick
names = 'abc'
values = range(3)
f, = symbolic_functions('f')
data = [dict((name, value) for name in names) for value in values]
assert pipe(pick.a, f)(data) == list(map(f, values))
def test_pick_multiple_items():
from liquidata import pipe, item as pick
names = 'abc'
ops = tuple(symbolic_functions(names))
values = range(3)
data = [{name:op(N) for (name, op) in zip(names, ops)} for N in values]
assert pipe(pick.a.b)(data) == list(map(itemgetter('a', 'b'), data))
assert pipe(pick.a )(data) == list(map(itemgetter('a' ), data))
RETHINK_ARGSPUT = xfail(reason='Transitioning to operators')
def test_on():
from liquidata import pipe, on
names = 'abc'
f, = symbolic_functions('f')
values = range(3)
data = [Namespace(**{name:N for name in names}) for N in values]
net = pipe(on.a(f))
expected = [copy(n) for n in data]
for n in expected:
n.a = f(n.a)
assert net(data) == expected
def test_get_single_attr():
from liquidata import get
it = Namespace(a=1, b=2)
assert get.a(it) == attrgetter('a')(it)
def test_get_single_item():
from liquidata import get
it = dict(a=1, b=2)
assert get['a'](it) == itemgetter('a')(it)
def test_item_single():
from liquidata import item
it = dict(a=1, b=2)
assert item.a(it) == itemgetter('a')(it)
def test_get_multilpe_attr():
from liquidata import get
it = Namespace(a=1, b=2, c=9, d=4)
assert get.d.b.c(it) == attrgetter('d', 'b', 'c')(it)
@GETITEM_FUNDAMENTALLY_BROKEN
def test_get_multilpe_get_item():
from liquidata import get
it = dict(a=1, b=2, c=9, d=4)
assert get['d', 'b', 'c'](it) == attrgetter('d', 'b', 'c')(it)
def test_item_multiple():
from liquidata import item
it = dict(a=1, b=2, c=9, d=4)
assert item.d.b.c(it) == itemgetter('d', 'b', 'c')(it)
def test_star_map():
from liquidata import pipe, get, star
data = namespace_source()
expected = list(it.starmap(sym_add, zip(map(attrgetter('a'), data),
map(attrgetter('b'), data))))
assert pipe(get.a.b, star(sym_add))(data) == expected
def test_star_implicit_sink():
from liquidata import pipe, get, star, sink
data = namespace_source()
result = []
def store_sum(x,y):
result.append(sym_add(x,y))
pipe(get.a.b, sink(star(store_sum)))(data)
expected = [sym_add(ns.a, ns.b) for ns in data]
assert result == expected
def test_star_flat():
from liquidata import pipe, get, star, flat
data = namespace_source()
got = pipe(get.a.b, star(flat(lambda a,b: (a,b))))(data)
expected = list(it.chain(*((ns.a, ns.b) for ns in data)))
assert got == expected
def test_get_star_single():
from liquidata import pipe, get
f, = symbolic_functions('f')
data = namespace_source()
expected = [f(n.b) for n in data]
assert pipe(get.b * f)(data) == expected
def test_get_star_multiple():
from liquidata import pipe, get
data = namespace_source()
expected = [sym_add(n.c, n.a) for n in data]
assert pipe(get.c.a * sym_add)(data) == expected
def test_star_filter():
from liquidata import pipe, star
data = [(2,3), (3,2), (3,3), (9,1)]
got = pipe(star({gt}))(data)
assert got == list((a,b) for (a,b) in data if a > b)
def test_get_star_filter():
from liquidata import pipe, get, star
data = [Namespace(a=a, b=b) for (a,b) in ((2,3), (3,2), (3,3), (9,1))]
got = pipe(get.a.b * {gt})(data)
assert got == list((n.a, n.b) for n in data if n.a > n.b)
def test_star_key_filter():
from liquidata import pipe, get, star
data = [Namespace(a=a, b=b) for (a,b) in ((2,3), (3,2), (3,3), (9,1))]
got = pipe({star(gt) : get.a.b})(data)
assert got == list(n for n in data if n.a > n.b)
def test_star_pipe():
from liquidata import pipe, get, star
data = namespace_source()
a,b,f = symbolic_functions('abf')
subpipe = pipe(sym_add, f)
got = pipe(get.a.b, star(subpipe))(data)
assert got == [ f(sym_add(n.a, n.b)) for n in data ]
def test_get_star_pipe():
from liquidata import pipe, get
data = namespace_source()
a,b,f = symbolic_functions('abf')
subpipe = pipe(sym_add, f)
got = pipe(get.a.b * subpipe)(data)
assert got == [ f(sym_add(n.a, n.b)) for n in data ]
def test_get_star_implicit_pipe():
from liquidata import pipe, get
data = namespace_source()
a,b,f = symbolic_functions('abf')
got = pipe(get.a.b * (sym_add, f))(data)
assert got == [ f(sym_add(n.a, n.b)) for n in data ]
def test_star_implicit_pipe():
from liquidata import pipe, get, star
data = namespace_source()
a,b,f = symbolic_functions('abf')
got = pipe(get.a.b, star((sym_add, f)))(data)
assert got == [ f(sym_add(n.a, n.b)) for n in data ]
def test_get_as_args_single():
from liquidata import pipe, get
data = namespace_source()
f, = symbolic_functions('f')
assert pipe(get.c, f)(data) == list(map(f, map(attrgetter('c'), data)))
@parametrize('where', 'before after'.split())
def test_get_star_as_args_many(where):
from liquidata import pipe, get
data = namespace_source()
if where == 'before': net = pipe(get.a.b * sym_add)
else : net = pipe(sym_add * get.a.b)
expected = list(map(sym_add, map(attrgetter('a'), data),
map(attrgetter('b'), data)))
assert net(data) == expected
def test_name_single():
from liquidata import pipe, name
data = range(3)
assert pipe(name.x)(data) == list(Namespace(x=it) for it in data)
def test_name_multiple():
from liquidata import pipe, name
data = ((1,2,3), (4,5,6))
got = pipe(name.a.b.c)(data)
expected = list(Namespace(a=a, b=b, c=c) for (a,b,c) in data)
assert got == expected
def test_chaining_splitting_and_naming():
from liquidata import pipe, name
data = range(3)
f, g, h = symbolic_functions('fgh')
def split(x):
return f(x), g(x), h(x)
got = pipe(split, name.a.b.c)(data)
expected = list(Namespace(a=f(x), b=g(x), c=h(x)) for x in data)
assert got == expected
@parametrize('op', '>> <<'.split())
def test_put_operator_single(op):
from liquidata import pipe, put
data = namespace_source()
f, = symbolic_functions('f')
def bf(ns):
return f(ns.b)
if op == ">>": net = pipe(bf >> put.f_of_b)
else : net = pipe(put.f_of_b << bf )
expected = [copy(n) for n in data]
for n in expected:
n.f_of_b = f(n.b)
assert net(data) == expected
@parametrize('op', '>> <<'.split())
def test_put_operator_single_pipe(op):
from liquidata import pipe, put, get
data = namespace_source()
f, = symbolic_functions('f')
if op == ">>": net = pipe((get.b, f) >> put.f_of_b )
else : net = pipe(put.f_of_b << (get.b, f))
expected = [copy(n) for n in data]
for n in expected:
n.f_of_b = f(n.b)
assert net(data) == expected
@parametrize('op', '>> <<'.split())
def test_put_operator_many(op):
from liquidata import pipe, put
data = namespace_source()
def sum_prod(ns):
a,b = ns.a, ns.b
return sym_add(a,b), sym_mul(a,b)
if op == ">>": net = pipe( sum_prod >> put.sum.prod)
else : net = pipe(put.sum.prod << sum_prod)
expected = [copy(n) for n in data]
for n in expected:
a, b = n.a, n.b
n.sum = sym_add(a,b)
n.prod = sym_mul(a,b)
assert net(data) == expected
def test_get_single_put_single():
from liquidata import pipe, get, put
data = namespace_source()
f, = symbolic_functions('f')
net = pipe((get.b, f) >> put.result)
expected = [copy(n) for n in data]
for n in expected:
n.result = f(n.b)
assert net(data) == expected
def test_get_single_put_many():
from liquidata import pipe, get, put
l,r = symbolic_functions('lr')
def f(x):
return l(x), r(x)
data = namespace_source()
net = pipe((get.c, f) >> put.l.r)
expected = [copy(n) for n in data]
for n in expected:
result = f(n.c)
n.l, n.r = result
assert net(data) == expected
def make_test_permutations(): # limit the scope of names used by parametrize
from liquidata import pipe, get, put
def hard_work(a,b):
return sym_add(a,b), sym_mul(a,b)
data = namespace_source()
expected = [copy(n) for n in data]
for n in expected:
x,y = hard_work(n.a, n.b)
n.x = x
n.y = y
@parametrize('spec',
((get.a.b * hard_work >> put.x.y),
(hard_work * get.a.b >> put.x.y),
(put.x.y << hard_work * get.a.b),
))
def test_start_shift_permutations(spec):
assert pipe(spec)(data) == expected
return test_start_shift_permutations
test_start_shift_permutations = make_test_permutations()
def test_get_single_filter():
from liquidata import pipe, get, arg as _
ds = (dict(a=1, b=2),
dict(a=3, b=3),
dict(a=2, b=1),
dict(a=8, b=9))
data = [Namespace(**d) for d in ds]
net = pipe(get.b, {_ > 2})
expected = list(filter(_ > 2, map(attrgetter('b'), data)))
assert net(data) == expected
@RETHINK_ARGSPUT
def test_get_many_filter():
from liquidata import pipe, get
data = (dict(a=1, b=2),
dict(a=3, b=3),
dict(a=2, b=1),
dict(a=8, b=9))
net = pipe((get.a.b, {lt}))
expected = (dict(a=1, b=2),
dict(a=8, b=9))
assert net(data) == expected
def test_get_single_flat():
from liquidata import pipe, flat, get
ds = (dict(a=1, b=2),
dict(a=0, b=3),
dict(a=3, b=1))
data = [Namespace(**d) for d in ds]
net = pipe(get.a, flat(lambda n: n*[n]))
assert net(data) == [1,3,3,3]
def test_get_many_flat():
from liquidata import pipe, flat, get
ds = (dict(a=1, b=9),
dict(a=0, b=8),
dict(a=3, b=7))
data = [Namespace(**d) for d in ds]
net = pipe(get.a.b * flat(lambda a,b:a*[b]))
assert net(data) == [9,7,7,7]
small_ints = integers(min_value=0, max_value=15)
small_ints_nonzero = integers(min_value=1, max_value=15)
slice_arg = one_of(none(), small_ints)
slice_arg_nonzero = one_of(none(), small_ints_nonzero)
@given(one_of(tuples(small_ints),
tuples(small_ints, small_ints),
tuples(slice_arg, slice_arg, slice_arg_nonzero)))
def test_slice_downstream(spec):
from liquidata import pipe, Slice
data = list('abcdefghij')
result = pipe(Slice(*spec))(data)
specslice = slice(*spec)
assert result == data[specslice]
assert result == data[specslice.start : specslice.stop : specslice.step]
# slice takes an optional argument close_all. If this argument
# is False (default), slice will close the innermost branch in
# which the component is plugged in after the component iterates
# over all its entries. However, when set to True, the behaviour
# is to close the outermost pipeline, resulting in a full stop of
# the data flow.
@parametrize("close_all", (False, True))
def test_slice_close_all(close_all):
from liquidata import Slice, pipe, out
data = list(range(20))
n_elements = 5
the_slice = Slice(n_elements, close_all=close_all)
result = pipe([the_slice, out.branch], out.main)(data)
if close_all:
assert result.branch == data[:n_elements]
assert result.main == data[:n_elements]
else:
assert result.branch == data[:n_elements]
assert result.main == data
@parametrize('args',
(( -1,),
(None, -1),
(-1, None),
(None, None, -1),
(None, None, 0),
))
def test_slice_raises_ValueError(args):
from liquidata import Slice
with raises(ValueError):
Slice(*args)
from operator import eq, ne, lt, gt, le, ge, add, sub, mul, floordiv, truediv
binops = sampled_from((eq, ne, lt, gt, le, ge, add, sub, mul, floordiv, truediv))
@given(binops, integers(), integers())
def test_arg_as_lambda_binary(op, lhs, rhs):
assume(op not in (truediv, floordiv) or rhs != 0)
from liquidata import arg
a = op(arg, rhs)
ar = op(lhs, arg)
b = lambda x: op(x , rhs)
br = lambda x: op(lhs, x)
assert a (lhs) == b (lhs)
assert ar(rhs) == br(rhs)
from operator import neg, pos
unops = sampled_from((neg, pos, abs))
@given(unops, integers())
def test_arg_as_lambda_binary(op, operand):
from liquidata import arg
assert op(arg)(operand) == op(operand)
def test_arg_as_lambda_getitem():
from liquidata import arg
data = 'abracadabra'
assert (arg[3])(data) == (lambda x: x[3])(data)
@GETITEM_FUNDAMENTALLY_BROKEN
def test_arg_as_lambda_get_multilple_items():
from liquidata import arg
data = 'abracadabra'
assert (arg[3,9,4])(data) == (lambda x: (x[3], x[9], x[4]))(data)
def test_arg_as_lambda_getattr():
from liquidata import arg
data = Namespace(a=1, b=2)
assert (arg.a)(data) == (lambda x: x.a)(data)
def test_arg_as_lambda_call_single_arg():
from liquidata import arg
def square(x):
return x * x
assert (arg(3))(square) == (lambda x: x(3))(square)
def test_arg_as_lambda_call_two_args():
from liquidata import arg
assert (arg(2,3))(add) == (lambda x: x(2,3))(add)
def test_arg_as_lambda_call_keyword_args():
from liquidata import arg
assert (arg(a=6, b=7))(dict) == (lambda x: x(a=6, b=7))(dict)
# TODO test close_all for take, drop, until, while_, ...
def test_take():
from liquidata import pipe, take
data = 'abracadabra'
assert ''.join(pipe(take(5))(data)) == ''.join(data[:5])
def test_drop():
from liquidata import pipe, drop
data = 'abracadabra'
assert ''.join(pipe(drop(5))(data)) == ''.join(data[5:])
def test_until():
from liquidata import pipe, until, arg as _
data = 'abcdXefghi'
expected = ''.join(it.takewhile(_ != 'X', data))
got = ''.join(pipe(until (_ == 'X'))(data))
assert got == expected
def test_while():
from liquidata import pipe, while_, arg as _
data = 'abcdXefghi'
expected = ''.join(it.takewhile(_ != 'X', data))
got = ''.join(pipe(while_ (_ != 'X'))(data))
assert got == expected