def test_basic_map(self): # We can upgrade maps to run in parallel banana = "bnn" | concurrent(mmap(a)) | "".join str_concat = operators.concat | node("".join) batman = concurrent(mmap(a, operator=str_concat)) self.assertEqual(banana(), "banana") self.assertEqual(batman("nnnn"), "nananana")
def test_multi_arg_map(self): @node def f(*args): return args m = concurrent(mmap(f)) with self.assertRaises(CompositionError): # Because star returns a simple function, we can't upgrade it. starmap = concurrent(star(mmap(f))) # we have to wrap concurrent in star instead. starmap = star(concurrent(mmap(f))) mapstar = concurrent(mmap(star(f))) self.assertEqual(m([1, 2, 3], [4, 5, 6]), ((1, 4), (2, 5), (3, 6))) self.assertEqual(m([1, 2, 3]), ((1, ), (2, ), (3, ))) with self.assertRaises(TypeError): starmap([1, 2, 3]) self.assertEqual(starmap([[1, 2, 3]]), m([1, 2, 3])) cmp = ([1, 2, 3], [4, 5, 6]) | starmap self.assertEqual(cmp(), ((1, 4), (2, 5), (3, 6))) cmp = ([1, 2, 3], [4, 5, 6]) | mapstar self.assertEqual(cmp(), ((1, 2, 3), (4, 5, 6)))
def test_str_repr(self): cab = ConcurrentMerge(a + b) cmap = concurrent(mmap(a)) self.assertEqual( repr(cab), "ConcurrentMerge({0}, ({1!r}, {2!r}))".format(operator.add, a, b)) self.assertEqual(str(cab), "concurrent(a + b)") self.assertEqual(str(cmap), "concurrent(mmap(a))")
def test_multi_arg(self): @node def f(*args): return args m = mmap(f) starmap = star(mmap(f)) mapstar = mmap(star(f)) self.assertEqual(m([1, 2, 3], [4, 5, 6]), ((1, 4), (2, 5), (3, 6))) self.assertEqual(m([1, 2, 3]), ((1, ), (2, ), (3, ))) with self.assertRaises(TypeError): starmap([1, 2, 3]) self.assertEqual(starmap([[1, 2, 3]]), m([1, 2, 3])) cmp = ([1, 2, 3], [4, 5, 6]) | starmap self.assertEqual(cmp(), ((1, 4), (2, 5), (3, 6))) cmp = ([1, 2, 3], [4, 5, 6]) | mapstar self.assertEqual(cmp(), ((1, 2, 3), (4, 5, 6)))
def test_highlight_with_map(self): @node def no_fail(x): return x @node def fail(*args): 1 / 0 cmp = a + b | (c & no_fail & fail) mapper = locate_error(("aaaaa", "BBBBB") | mmap(cmp), use_color=False) with self.assertRaises(ZeroDivisionError) as e: mapper() self.assertEqual( str(e.exception), "division by zero \n\nOccured in the following function: " "(('aaaaa', 'BBBBB') | mmap(((a + b) | (c & no_fail & ->fail<-))))", )
def test_loop_with_non_meta(self): cmp = (b & c & "stoke") | mmap(len) self.assertEqual(cmp("_"), (2, 2, 5))
def test_loop(self): cmp = (b & c & "stoke") | mmap(a) self.assertEqual(cmp("_"), ("_ba", "_ca", "stokea"))
def test_auto_meta(self): mapsum = mmap(sum) self.assertEqual(mapsum([[1, 2], [3, 4]]), (3, 7)) self.assertEqual(str(mapsum), "mmap(sum)")