def test_reduce_right(self): res = _(["foo", "bar", "baz"]).reduceRight(lambda sum, num, *args: sum + num) self.assertEqual("bazbarfoo", res, "did not reducedRight correctly") # alias res = _(["foo", "bar", "baz"]).foldr(lambda sum, num, *args: sum + num) self.assertEqual("bazbarfoo", res, "did not foldr correctly")
def test_reduce_right(self): res = _(["foo", "bar", "baz"]).reduceRight( lambda sum, num, *args: sum + num) self.assertEqual("bazbarfoo", res, "did not reducedRight correctly") # alias res = _(["foo", "bar", "baz"]).foldr(lambda sum, num, *args: sum + num) self.assertEqual("bazbarfoo", res, "did not foldr correctly")
def test_filter(self): res = _(["foo", "hello", "bar", "world"]).filter(lambda x, *args: len(x) > 3) self.assertEqual(["hello", "world"], res, "filter didn't work") # alias res = _(["foo", "hello", "bar", "world"]).select(lambda x, *args: len(x) > 3) self.assertEqual(["hello", "world"], res, "select didn't work")
def test_map_list(self): def mapTest(val, *args): return val * 2 map = _([1, 2, 3, 4]).map(mapTest) self.assertEqual([2, 4, 6, 8], map, "map for list did not work") # alias map = _([1, 2, 3, 4]).collect(mapTest) self.assertEqual([2, 4, 6, 8], map, "collect for list did not work")
def test_filter(self): res = _(["foo", "hello", "bar", "world"] ).filter(lambda x, *args: len(x) > 3) self.assertEqual(["hello", "world"], res, "filter didn't work") # alias res = _(["foo", "hello", "bar", "world"] ).select(lambda x, *args: len(x) > 3) self.assertEqual(["hello", "world"], res, "select didn't work")
def test_map_dict(self): def mapTest(val, key, *args): return val.upper() map = _({"foo": "bar", "bar": "foo"}).map(mapTest) self.assertEqual(["BAR", "FOO"], map, "map for dicts did not work") # alias map = _({"foo": "bar", "bar": "foo"}).collect(mapTest) self.assertEqual(["BAR", "FOO"], map, "collect for dicts did not work")
def test_times(self): vals = [] _.times(3, lambda i: vals.append(i)) self.assertEqual([0, 1, 2], vals, "is 0 indexed") vals = [] _(3).times(lambda i: vals.append(i)) self.assertEqual([0, 1, 2], vals, "is 0 indexed") pass
def test_reduce(self): res = _([1, 2, 3, 4, 5, 6]).reduce(lambda sum, num, *args: sum + num, 0) self.assertEqual(21, res, "did not reduced correctly") # alias res = _([1, 2, 3, 4, 5, 6]).foldl(lambda sum, num, *args: sum + num, 0) self.assertEqual(21, res, "did not foldl correctly") # alias res = _([1, 2, 3, 4, 5, 6]).inject(lambda sum, num, *args: sum + num, 0) self.assertEqual(21, res, "did not inject correctly")
def test_each_dict(self): def eachTest(val, key, *args): self.eachDict += (key + ":" + val + " ") _({"foo": "bar", "bar": "foo"}).each(eachTest) self.assertEqual("foo:bar bar:foo ", self.eachDict, "each for dicts did not work for all") # alias self.eachDict = "" _({"foo": "bar", "bar": "foo"}).forEach(eachTest) self.assertEqual("foo:bar bar:foo ", self.eachDict, "forEach for dicts did not work for all")
def test_groupby(self): parity = _.groupBy([1, 2, 3, 4, 5, 6], lambda num, *args: num % 2) self.assertTrue(0 in parity and 1 in parity, 'created a group for each value') self.assertEqual(_(parity[0]).join(', '), '2, 4, 6', 'put each even number in the right group') llist = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] grouped = _.groupBy(llist, lambda x, *args: len(x)) self.assertEqual(_(grouped[3]).join(' '), 'one two six ten') self.assertEqual(_(grouped[4]).join(' '), 'four five nine') self.assertEqual(_(grouped[5]).join(' '), 'three seven eight')
def test_each_list(self): def eachTest(val, *args): self.eachList.append(val + 1) _([1, 2, 3, 4]).each(eachTest) self.assertEqual([2, 3, 4, 5], self.eachList, "each for lists did not work for all") # test alias self.eachList = [] _([1, 2, 3, 4]).forEach(eachTest) self.assertEqual([2, 3, 4, 5], self.eachList, "forEach for lists did not work for all")
def test_each_dict(self): def eachTest(val, key, *args): self.eachSet.add(val) self.eachSet.add(key) _({"foo": "bar", "fizz": "buzz"}).each(eachTest) self.assertEqual({"foo", "bar", "fizz", "buzz"}, self.eachSet, "each for dicts did not work for all") # alias self.eachSet = set() _({"foo": "bar", "fizz": "buzz"}).forEach(eachTest) self.assertEqual({"foo", "bar", "fizz", "buzz"}, self.eachSet, "forEach for dicts did" "not work for all")
def test_sortBy(self): res = _([{'age': '59', 'name': 'foo'}, {'age': '39', 'name': 'bar'}, {'age': '49', 'name': 'baz'}]).sortBy('age') self.assertEqual([{'age': '39', 'name': 'bar'}, {'age': '49', 'name': 'baz'}, {'age': '59', 'name': 'foo'}], res, "filter by key did not work") res = _([{'age': '59', 'name': 'foo'}, {'age': '39', 'name': 'bar'}, {'age': '49', 'name': 'baz'}]).sortBy(lambda x, y, *args: cmp(x, y)) self.assertEqual([{'age': '39', 'name': 'bar'}, {'age': '49', 'name': 'baz'}, {'age': '59', 'name': 'foo'}], res, "filter by lambda did not work") res = _([50, 78, 30, 15, 90]).sortBy() self.assertEqual([15, 30, 50, 78, 90], res, "filter list did not work")
def test_mixin(self): _.mixin({ "myUpper": lambda self: self.obj.upper(), }) self.assertEqual('TEST', _.myUpper('test'), "mixed in a function to _") self.assertEqual('TEST', _('test').myUpper(), "mixed in a function to _ OOP")
def test_groupby(self): parity = _.groupBy([1, 2, 3, 4, 5, 6], lambda num, *args: num % 2) self.assertTrue(0 in parity and 1 in parity, 'created a group for each value') self.assertEqual( _(parity[0]).join(', '), '2, 4, 6', 'put each even number in the right group') llist = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ] grouped = _.groupBy(llist, lambda x, *args: len(x)) self.assertEqual(_(grouped[3]).join(' '), 'one two six ten') self.assertEqual(_(grouped[4]).join(' '), 'four five nine') self.assertEqual(_(grouped[5]).join(' '), 'three seven eight')
def test_invert(self): obj = {"first": 'Moe', "second": 'Larry', "third": 'Curly'} r = _(obj).chain().invert().keys().join(' ').value() self.assertEqual(set(r), set('Larry Moe Curly'), 'can invert an object') self.assertEqual(_.invert(_.invert(obj)), obj, "two inverts gets you back where you started")
def test_zip(self): names = ['moe', 'larry', 'curly'] ages = [30, 40, 50] leaders = [True] stooges = _(names).zip(ages, leaders) self.assertEqual( "[('moe', 30, True), ('larry', 40, None), ('curly', 50, None)]", str(stooges), 'zipped together arrays of different lengths')
def test_zip(self): names = ['moe', 'larry', 'curly'] ages = [30, 40, 50] leaders = [True] stooges = list(_(names).zip(ages, leaders)) self.assertEqual("[('moe', 30, True), ('larry', 40, None)," " ('curly', 50, None)]", str( stooges), 'zipped together arrays of different lengths')
def test_intersection(self): stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'] self.assertEqual(['moe'], _.intersection(stooges, leaders), 'can take the set intersection of two arrays') self.assertEqual(['moe'], _(stooges).intersection(leaders), 'can perform an OO-style intersection')
def test_sortBy(self): res = _([{ 'age': '59', 'name': 'foo' }, { 'age': '39', 'name': 'bar' }, { 'age': '49', 'name': 'baz' }]).sortBy('age') self.assertEqual([{ 'age': '39', 'name': 'bar' }, { 'age': '49', 'name': 'baz' }, { 'age': '59', 'name': 'foo' }], res, "filter by key did not work") res = _([{ 'age': '59', 'name': 'foo' }, { 'age': '39', 'name': 'bar' }, { 'age': '49', 'name': 'baz' }]).sortBy(lambda x, y, *args: cmp(x, y)) self.assertEqual([{ 'age': '39', 'name': 'bar' }, { 'age': '49', 'name': 'baz' }, { 'age': '59', 'name': 'foo' }], res, "filter by lambda did not work") res = _([50, 78, 30, 15, 90]).sortBy() self.assertEqual([15, 30, 50, 78, 90], res, "filter list did not work")
def test_intersection(self): stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'] self.assertEqual(['moe'], _.intersection(stooges, leaders), 'can take the set intersection of two string arrays') self.assertEqual( [1, 2], _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]), 'can take the set intersection of two int arrays') self.assertEqual(['moe'], _(stooges).intersection(leaders), 'can perform an OO-style intersection')
def test_intersection(self): stooges = ['moe', 'curly', 'larry'], leaders = ['moe', 'groucho'] self.assertEqual(['moe'], _.intersection(stooges, leaders), 'can take the set intersection of two string arrays') self.assertEqual([1, 2], _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]), 'can take the set intersection of two int arrays') self.assertEqual(['moe'], _(stooges).intersection(leaders), 'can perform an OO-style intersection')
def test_pluck(self): res = _([{ "name": "foo", "age": "29" }, { "name": "bar", "age": "39" }, { "name": "baz", "age": "49" }]).pluck('age') self.assertEqual(["29", "39", "49"], res, "pluck did not work")
def test_tap(self): ns = self.Namespace() ns.intercepted = None def interceptor(obj): ns.intercepted = obj returned = _.tap(1, interceptor) self.assertEqual(ns.intercepted, 1, "passes tapped object to interceptor") self.assertEqual(returned, 1, "returns tapped object") returned = _([1, 2, 3]).chain().map(lambda n, *args: n * 2).max().tap(interceptor).value() self.assertTrue(returned == 6 and ns.intercepted == 6, 'can use tapped objects in a chain')
def test_isEmpty(self): self.assertTrue(not _([1]).isEmpty(), '[1] is not empty') self.assertTrue(_.isEmpty([]), '[] is empty') self.assertTrue(not _.isEmpty({"one": 1}), '{one : 1} is not empty') self.assertTrue(_.isEmpty({}), '{} is empty') self.assertTrue(_.isEmpty(None), 'null is empty') self.assertTrue(_.isEmpty(), 'undefined is empty') self.assertTrue(_.isEmpty(''), 'the empty string is empty') self.assertTrue(not _.isEmpty('moe'), 'but other strings are not') obj = {"one": 1} obj.pop("one") self.assertTrue(_.isEmpty(obj), 'deleting all the keys from an object empties it') pass
def test_max_list(self): res = _([-55, -55, -7, 15, 2, 1, -7, 4]).list_max_index_value() expected = [3] assert res == expected
def test_last(self): res = _([1, 2, 3, 4, 5]).last() self.assertEqual(5, res, "last one item did not work") res = _([1, 2, 3, 4, 5]).last(3) self.assertEqual([3, 4, 5], res, "last multi item did not wok")
def test_include_dict(self): res = _({"foo": "bar", "hello": "world"}).include('bar') self.assertTrue(res, "include was not true") res = _({"foo": "bar", "hello": "world"}).include('notin') self.assertFalse(res, "include was not false")
def test_chaining(self): array = range(1, 11) u = _(array).chain().filter(lambda x: x > 5).min() self.assertTrue(isinstance(u, _.underscore), "object is not an instanse of underscore") self.assertEqual(6, u.value(), "value should have returned")
def test_first(self): res = _([1, 2, 3, 4, 5]).first() self.assertEqual(1, res, "first one item did not work") res = _([1, 2, 3, 4, 5]).first(3) self.assertEqual([1, 2, 3], res, "first multi item did not wok")
def test_rest(self): res = _([1, 2, 3, 4, 5]).rest() self.assertEqual([2, 3, 4, 5], res, "rest one item did not work") res = _([1, 2, 3, 4, 5]).rest(3) self.assertEqual([4, 5], res, "rest multi item did not wok")
def test_all(self): res = _([True, True, True, True]).all() self.assertTrue(res, "all was not true") res = _([True, True, False, True]).all() self.assertFalse(res, "all was not false")
def test_max(self): res = _([5, 10, 15, 4, 8]).max() self.assertEqual(15, res, "max did not work")
def test_shuffle(self): res = _([5, 10, 15, 4, 8]).shuffle() self.assertNotEqual([5, 10, 15, 4, 8], res, "shuffled array was the same")
def test_min(self): res = _([5, 10, 15, 4, 8]).min() self.assertEqual(4, res, "min did not work")
def test_pluck(self): res = _([{"name": "foo", "age": "29"}, {"name": "bar", "age": "39"}, {"name": "baz", "age": "49"}]).pluck('age') self.assertEqual(["29", "39", "49"], res, "pluck did not work")
def test_invoke(self): res = _(["foo", "bar"]).invoke(lambda x, *args: x.upper()) self.assertEqual(["FOO", "BAR"], res, "invoke with lambda did not work") res = _(["foo", "bar"]).invoke("upper") self.assertEqual(["FOO", "BAR"], res, "invoke with name did not work")
def test_max_empty_list(self): res = _([]).list_max_index_value() expected = [] assert res == expected
def test_any(self): res = _([False, False, False, True]).any() self.assertTrue(res, "any was not true") res = _([False, False, False, False]).any() self.assertFalse(res, "any was not false")
def test_max_one_value(self): res = _([2, 2, 2, 2]).list_max_index_value() expected = [0, 1, 2, 3] assert res == expected
def test_find(self): res = _([1, 2, 3, 4, 5]).find(lambda x, *args: x > 2) self.assertEqual(3, res, "find didn't work") # alias res = _([1, 2, 3, 4, 5]).detect(lambda x, *args: x > 2) self.assertEqual(3, res, "detect didn't work")
def test_compact(self): res = _([False, 1, 0, "foo", None, -1]).compact() self.assertEqual([1, "foo", -1], res, "compact did not work")
def test_reject(self): res = _(["foo", "hello", "bar", "world"] ).reject(lambda x, *args: len(x) > 3) self.assertEqual(["foo", "bar"], res, "reject didn't work")
def test_oo(self): min = _([1, 2, 3, 4, 5]).min() self.assertEqual(1, min, "oo did not work")
def test_initial(self): res = _([1, 2, 3, 4, 5]).initial() self.assertEqual([1, 2, 3, 4], res, "initial one item did not work") res = _([1, 2, 3, 4, 5]).initial(3) self.assertEqual([1, 2], res, "initial multi item did not wok")
def test_include(self): res = _(["hello", "world", "foo", "bar"]).include('foo') self.assertTrue(res, "include was not true") res = _(["hello", "world", "foo", "bar"]).include('notin') self.assertFalse(res, "include was not false")