import unittest # Setup required global values _v = 1 _list = [i % 2 for i in range(0,4)] _x = 0 _vec1 = [2, 4, 6] _vec2 = [4, 3, -9] _mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] class _TestComp: def __init__(self, x = 1): self.x = x def match_list(self, x_list): return [x == self.x for x in x_list] def match_list_eval(self, x_list): return eval('[x == self.x for x in x_list]') def _match_list_f(value, x_list): return [x == value for x in x_list] def _match_list_eval_f(value, x_list): return eval('[x == value for x in x_list]') class _TestNoComp: def __init__(self, x = 1): self.x = x def match(self, x): return x == self.x def match_eval(self, x): return eval('x == self.x') def _match_f(value, x): return x == value def _match_eval_f(value, x): return eval('x == value') class EvalWithoutComprehensionTestCase(unittest.TestCase): def setUp(self): self.t = _TestNoComp() self.results = {1: False} def test_const(self): result = 0 == 1 self.assertEquals(result, self.results[1]) result = eval('0 == 1') self.assertEquals(result, self.results[1]) def test_global_var(self): result = _x == _v self.assertEquals(result, self.results[1]) result = eval('_x == _v') self.assertEquals(result, self.results[1]) def test_local_var(self): value = 1 result = _x == value self.assertEquals(result, self.results[1]) result = eval('_x == value') self.assertEquals(result, self.results[1]) def test_function_arg(self): result = _match_f(1, _x) self.assertEquals(result, self.results[1]) result = _match_eval_f(1, _x) self.assertEquals(result, self.results[1]) def test_self_attrib(self): result = self.t.match(_x) self.assertEquals(result, self.results[1]) result = self.t.match_eval(_x) self.assertEquals(result, self.results[1]) class EvalWithComprehensionTestCase(unittest.TestCase): def setUp(self): self.t = _TestComp() self.results = {1: [False, True, False, True]} def test_const(self): result = [x == 1 for x in [0, 1, 0, 1]] self.assertEquals(result, self.results[1]) result = eval('[x == 1 for x in [0, 1, 0, 1]]') self.assertEquals(result, self.results[1]) def test_global_var(self): result = [x == _v for x in _list] self.assertEquals(result, self.results[1]) result = eval('[x == _v for x in _list]') self.assertEquals(result, self.results[1]) def test_local_var(self): value = 1 result = [x == value for x in _list] self.assertEquals(result, self.results[1]) result = eval('[x == value for x in _list]') self.assertEquals(result, self.results[1]) def test_function_arg(self): result = _match_list_f(1, _list) self.assertEquals(result, self.results[1]) result = _match_list_eval_f(1, _list) self.assertEquals(result, self.results[1]) def test_self_attrib(self): result = self.t.match_list(_list) self.assertEquals(result, self.results[1]) result = self.t.match_list_eval(_list) self.assertEquals(result, self.results[1]) def test_local_list(self): list = [0, 1, 0, 1] result = [x == 1 for x in list] self.assertEquals(result, self.results[1]) result = eval('[x == 1 for x in list]') self.assertEquals(result, self.results[1]) class ExampleFromDocsTestCase(unittest.TestCase): def setUp(self): self.result = [8, 12, -54] def test_global_var(self): result = [_vec1[i]*_vec2[i] for i in range(len(_vec1))] self.assertEquals(result, self.result) result = eval('[_vec1[i]*_vec2[i] for i in range(len(_vec1))]') self.assertEquals(result, self.result) def test_local_var(self): vec1 = [2, 4, 6] vec2 = [4, 3, -9] result = [vec1[i]*vec2[i] for i in range(len(vec1))] self.assertEquals(result, self.result) result = eval('[vec1[i]*vec2[i] for i in range(len(vec1))]') self.assertEquals(result, self.result) class NestedComprehensionExampleFromDocsTestCase(unittest.TestCase): def setUp(self): self.result = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] def test_global_var(self): result = [[row[i] for row in _mat] for i in [0, 1, 2]] self.assertEquals(result, self.result) result = eval('[[row[i] for row in _mat] for i in [0, 1, 2]]') self.assertEquals(result, self.result) def test_local_var(self): mat = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] result = [[row[i] for row in mat] for i in [0, 1, 2]] self.assertEquals(result, self.result) result = eval('[[row[i] for row in mat] for i in [0, 1, 2]]') self.assertEquals(result, self.result) if __name__ == '__main__': unittest.main()