def test_set_1(self): a = [1, 2, 3] fn = zz.set(zz.var(a)[0], _1) fn(100) self.assertEqual(a, [100, 2, 3]) fn(None) self.assertEqual(a, [None, 2, 3])
def test_delete_2(self): a = [0, 1, 2, 3, 4, 5] fn = zz.delete(zz.var(a)[_1]) fn(0) self.assertEqual(a, [1, 2, 3, 4, 5]) fn(3) self.assertEqual(a, [1, 2, 3, 5])
def test_var_2(self): # Equivalent to just _1 + 100, but we're explicitly wrapping # the number. fn = _1 + zz.var(100) self.assertEqual(fn(10), 110) self.assertEqual(fn(-1), 99) self.assertEqual(fn(0), 100)
def test_var_1(self): fn = zz.var(200) # Doesn't matter what arguments I pass self.assertEqual(fn(), 200) self.assertEqual(fn(0, 100), 200) self.assertEqual(fn(None, "foobar", default=object(), key=lambda x: x), 200) self.assertEqual(fn(200, object, var={'a': 1}, py="Python"), 200)
def test_var_3(self): obj = ["foo", "bar", "baz"] fn = zz.var(obj)[_1] self.assertEqual(fn(0), "foo") self.assertEqual(fn(1), "bar") self.assertEqual(fn(2), "baz") with self.assertRaises(IndexError): fn(3)
def test_delete_1(self): a = [0, 1, 2] fn = zz.delete(zz.var(a)[0]) fn() self.assertEqual(a, [1, 2]) fn() self.assertEqual(a, [2]) fn() self.assertEqual(a, []) with self.assertRaises(IndexError): fn()
def test_set_3(self): a = [1, 2, 3] fn = zz.set(zz.var(a)[_1], 99) fn(0) self.assertEqual(a, [99, 2, 3]) fn(1) self.assertEqual(a, [99, 99, 3]) fn(2) self.assertEqual(a, [99, 99, 99]) with self.assertRaises(IndexError): fn(3)
def test_set_4(self): a = [[0]] fn = zz.set(zz.var(a)[0][0], _1) fn(100) self.assertEqual(a, [[100]])
def test_var_4(self): # Currying. Please don't do this in production code. fn = _1 - zz.var(_1) self.assertEqual(fn(10)(5), 5) self.assertEqual(fn(10)(3), 7) self.assertEqual(fn(5)(6), -1)