def test_from_code_array(self): """Test from_code_array method""" self.xls_in.to_code_array() wb = xlwt.Workbook() xls_out = Xls(self.code_array, wb) worksheets = [] xls_out._shape2xls(worksheets) xls_out._code2xls(worksheets) xls_out._row_heights2xls(worksheets) self.write_xls_out(xls_out, wb, "_col_widths2xls", worksheets) new_code_array = CodeArray((1000, 100, 3)) xls_outfile = xlrd.open_workbook(self.xls_outfile_path, formatting_info=True) xls_out = Xls(new_code_array, xls_outfile) xls_out.to_code_array() assert self.code_array.shape == new_code_array.shape assert self.code_array.macros == new_code_array.macros assert self.code_array.dict_grid == new_code_array.dict_grid # There may be additional standard heights in copy --> 1 way test for height in self.code_array.row_heights: assert height in new_code_array.row_heights assert self.code_array.col_widths == new_code_array.col_widths # Clean up the test dir os.remove(self.xls_outfile_path)
def __init__(self, main_window, *args, **kwargs): S = kwargs.pop("S") self.main_window = main_window self._states() self.interfaces = GuiInterfaces(self.main_window) if S is None: dimensions = kwargs.pop("dimensions") else: dimensions = S.shape kwargs.pop("dimensions") wx.grid.Grid.__init__(self, main_window, *args, **kwargs) self.SetDefaultCellBackgroundColour(wx.Colour(255, 255, 255, 255)) # Cursor position on entering selection mode self.sel_mode_cursor = None # Set multi line editor self.SetDefaultEditor(GridCellEditor(main_window)) # Create new grid if S is None: self.code_array = CodeArray(dimensions) post_command_event(self, self.GridActionNewMsg, shape=dimensions) else: self.code_array = S _grid_table = GridTable(self, self.code_array) self.SetTable(_grid_table, True) # Grid renderer draws the grid self.grid_renderer = GridRenderer(self.code_array) self.SetDefaultRenderer(self.grid_renderer) # Context menu for quick access of important functions self.contextmenu = ContextMenu(parent=self) # Handler classes contain event handler methods self.handlers = GridEventHandlers(self) self.cell_handlers = GridCellEventHandlers(self) # Grid actions self.actions = AllGridActions(self) # Layout and bindings self._layout() self._bind() # Update toolbars self.update_entry_line() self.update_attribute_toolbar() # Focus on grid so that typing can start immediately self.SetFocus()
def setup_method(self, method): """Creates Pys class with code_array and temporary test.pys file""" # All data structures are initially empty # The test file pys_file has entries in each category self.code_array = CodeArray((1000, 100, 3)) self.pys_infile = bz2.BZ2File(TESTPATH + "pys_test1.pys") self.pys_outfile_path = TESTPATH + "pys_test2.pys" self.pys_in = Pys(self.code_array, self.pys_infile)
def test_slicing(self): """Unit test for __getitem__ and __setitem__""" #Test for item getting, slicing, basic evaluation correctness shape = self.code_array.shape x_list = [0, shape[0]-1] y_list = [0, shape[1]-1] z_list = [0, shape[2]-1] for x, y, z in zip(x_list, y_list, z_list): assert self.code_array[x, y, z] is None self.code_array[:x, :y, :z] self.code_array[:x:2, :y:2, :z:-1] get_shape = numpy.array(self.code_array[:, :, :]).shape orig_shape = self.code_array.shape assert get_shape == orig_shape gridsize = 100 filled_grid = CodeArray((gridsize, 10, 1)) for i in [-2**99, 2**99, 0]: for j in xrange(gridsize): filled_grid[j, 0, 0] = str(i) filled_grid[j, 1, 0] = str(i) + '+' + str(j) filled_grid[j, 2, 0] = str(i) + '*' + str(j) for j in xrange(gridsize): assert filled_grid[j, 0, 0] == i assert filled_grid[j, 1, 0] == i + j assert filled_grid[j, 2, 0] == i * j for j, funcname in enumerate(['int', 'math.ceil', 'fractions.Fraction']): filled_grid[0, 0, 0] = "fractions = __import__('fractions')" filled_grid[0, 0, 0] filled_grid[1, 0, 0] = "math = __import__('math')" filled_grid[1, 0, 0] filled_grid[j, 3, 0] = funcname + ' (' + str(i) + ')' #res = eval(funcname + "(" + "i" + ")") assert filled_grid[j, 3, 0] == eval(funcname + "(" + "i" + ")") #Test X, Y, Z for i in xrange(10): self.code_array[i, 0, 0] = str(i) assert [self.code_array((i, 0, 0)) for i in xrange(10)] == \ map(str, xrange(10)) assert [self.code_array[i, 0, 0] for i in xrange(10)] == range(10) # Test cycle detection filled_grid[0, 0, 0] = "numpy.arange(0, 10, 0.1)" filled_grid[1, 0, 0] = "sum(S[0,0,0])" assert filled_grid[1, 0, 0] == sum(numpy.arange(0, 10, 0.1))
def setup_method(self, method): """Creates Xls class with code_array and temporary test.xls file""" # All data structures are initially empty # The test file xls_file has entries in each category self.top_window = wx.Frame(None, -1) wx.GetApp().SetTopWindow(self.top_window) self.code_array = CodeArray((1000, 100, 3)) self.xls_infile = xlrd.open_workbook(TESTPATH + "xls_test1.xls", formatting_info=True) self.xls_outfile_path = TESTPATH + "xls_test2.xls" self.xls_in = Xls(self.code_array, self.xls_infile)
def setup_method(self, method): """Creates empty DataArray""" self.code_array = CodeArray((100, 10, 3))
class TestCodeArray(object): """Unit tests for CodeArray""" def setup_method(self, method): """Creates empty DataArray""" self.code_array = CodeArray((100, 10, 3)) param_test_setitem = [ {"data": {(2, 3, 2): "42"}, "items": {(1, 3, 2): "42"}, "res_data": {(1, 3, 2): "42", (2, 3, 2): "42"}, }, ] @params(param_test_setitem) def test_setitem(self, data, items, res_data): """Unit test for __setitem__""" self.code_array.dict_grid.update(data) for key in items: self.code_array[key] = items[key] for key in res_data: assert res_data[key] == self.code_array(key) def test_slicing(self): """Unit test for __getitem__ and __setitem__""" # Test for item getting, slicing, basic evaluation correctness shape = self.code_array.shape x_list = [0, shape[0]-1] y_list = [0, shape[1]-1] z_list = [0, shape[2]-1] for x, y, z in zip(x_list, y_list, z_list): assert self.code_array[x, y, z] is None self.code_array[:x, :y, :z] self.code_array[:x:2, :y:2, :z:-1] get_shape = numpy.array(self.code_array[:, :, :]).shape orig_shape = self.code_array.shape assert get_shape == orig_shape gridsize = 100 filled_grid = CodeArray((gridsize, 10, 1)) for i in [-2**99, 2**99, 0]: for j in xrange(gridsize): filled_grid[j, 0, 0] = str(i) filled_grid[j, 1, 0] = str(i) + '+' + str(j) filled_grid[j, 2, 0] = str(i) + '*' + str(j) for j in xrange(gridsize): assert filled_grid[j, 0, 0] == i assert filled_grid[j, 1, 0] == i + j assert filled_grid[j, 2, 0] == i * j for j, funcname in enumerate(['int', 'math.ceil', 'fractions.Fraction']): filled_grid[0, 0, 0] = "fractions = __import__('fractions')" filled_grid[0, 0, 0] filled_grid[1, 0, 0] = "math = __import__('math')" filled_grid[1, 0, 0] filled_grid[j, 3, 0] = funcname + ' (' + str(i) + ')' assert filled_grid[j, 3, 0] == eval(funcname + "(" + "i" + ")") # Test X, Y, Z for i in xrange(10): self.code_array[i, 0, 0] = str(i) assert [self.code_array((i, 0, 0)) for i in xrange(10)] == \ map(str, xrange(10)) assert [self.code_array[i, 0, 0] for i in xrange(10)] == range(10) # Test cycle detection filled_grid[0, 0, 0] = "numpy.arange(0, 10, 0.1)" filled_grid[1, 0, 0] = "sum(S[0,0,0])" assert filled_grid[1, 0, 0] == sum(numpy.arange(0, 10, 0.1)) def test_make_nested_list(self): """Unit test for _make_nested_list""" def gen(): """Nested generator""" yield (("Test" for _ in xrange(2)) for _ in xrange(2)) res = self.code_array._make_nested_list(gen()) assert res == [[["Test" for _ in xrange(2)] for _ in xrange(2)]] param_get_assignment_target_end = [ {'code': "a=5", 'res': 1}, {'code': "a = 5", 'res': 1}, {'code': "5", 'res': -1}, {'code': "a == 5", 'res': -1}, {'code': "", 'res': -1}, {'code': "fractions = __import__('fractions')", 'res': 9}, {'code': "math = __import__('math')", 'res': 4}, {'code': "a = 3==4", 'res': 1}, {'code': "a == 3 < 44", 'res': -1}, {'code': "a != 3 < 44", 'res': -1}, {'code': "a >= 3 < 44", 'res': -1}, {'code': "a = 3 ; a < 44", 'res': None}, ] @params(param_get_assignment_target_end) def test_get_assignment_target_end(self, code, res): """Unit test for _get_assignment_target_end""" module = ast.parse(code) if res is None: try: self.code_array._get_assignment_target_end(module) raise ValueError("Multiple expressions cell not identified") except ValueError: pass else: assert self.code_array._get_assignment_target_end(module) == res param_eval_cell = [ {'key': (0, 0, 0), 'code': "2 + 4", 'res': 6}, {'key': (1, 0, 0), 'code': "S[0, 0, 0]", 'res': None}, {'key': (43, 2, 1), 'code': "X, Y, Z", 'res': (43, 2, 1)}, ] @params(param_eval_cell) def test_eval_cell(self, key, code, res): """Unit test for _eval_cell""" self.code_array[key] = code assert self.code_array._eval_cell(key, code) == res def test_execute_macros(self): """Unit test for execute_macros""" self.code_array.macros = "a = 5\ndef f(x): return x ** 2" self.code_array.execute_macros() assert self.code_array._eval_cell((0, 0, 0), "a") == 5 assert self.code_array._eval_cell((0, 0, 0), "f(2)") == 4 def test_sorted_keys(self): """Unit test for _sorted_keys""" code_array = self.code_array keys = [(1, 0, 0), (2, 0, 0), (0, 1, 0), (0, 99, 0), (0, 0, 0), (0, 0, 99), (1, 2, 3)] assert list(code_array._sorted_keys(keys, (0, 1, 0))) == \ [(0, 1, 0), (0, 99, 0), (1, 2, 3), (0, 0, 99), (0, 0, 0), (1, 0, 0), (2, 0, 0)] sk = list(code_array._sorted_keys(keys, (0, 3, 0), reverse=True)) assert sk == [(0, 1, 0), (2, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 99), (1, 2, 3), (0, 99, 0)] def test_string_match(self): """Tests creation of string_match""" code_array = self.code_array test_strings = [ "", "Hello", " Hello", "Hello ", " Hello ", "Hello\n", "THelloT", " HelloT", "THello ", "hello", "HELLO", "sd" ] search_string = "Hello" # Normal search flags = [] results = [None, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, flags) assert res == result flags = ["MATCH_CASE"] results = [None, 0, 1, 0, 1, 0, 1, 1, 1, None, None, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, flags) assert res == result flags = ["WHOLE_WORD"] results = [None, 0, 1, 0, 1, 0, None, None, None, 0, 0, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, flags) assert res == result def test_findnextmatch(self): """Find method test""" code_array = self.code_array for i in xrange(100): code_array[i, 0, 0] = str(i) assert code_array[3, 0, 0] == 3 assert code_array.findnextmatch((0, 0, 0), "3", "DOWN") == (3, 0, 0) assert code_array.findnextmatch((0, 0, 0), "99", "DOWN") == (99, 0, 0)
class TestCodeArray(object): """Unit tests for CodeArray""" def setup_method(self, method): """Creates empty DataArray""" self.code_array = CodeArray((100, 10, 3)) def test_slicing(self): """Unit test for __getitem__ and __setitem__""" #Test for item getting, slicing, basic evaluation correctness shape = self.code_array.shape x_list = [0, shape[0]-1] y_list = [0, shape[1]-1] z_list = [0, shape[2]-1] for x, y, z in zip(x_list, y_list, z_list): assert self.code_array[x, y, z] is None self.code_array[:x, :y, :z] self.code_array[:x:2, :y:2, :z:-1] get_shape = numpy.array(self.code_array[:, :, :]).shape orig_shape = self.code_array.shape assert get_shape == orig_shape gridsize = 100 filled_grid = CodeArray((gridsize, 10, 1)) for i in [-2**99, 2**99, 0]: for j in xrange(gridsize): filled_grid[j, 0, 0] = str(i) filled_grid[j, 1, 0] = str(i) + '+' + str(j) filled_grid[j, 2, 0] = str(i) + '*' + str(j) for j in xrange(gridsize): assert filled_grid[j, 0, 0] == i assert filled_grid[j, 1, 0] == i + j assert filled_grid[j, 2, 0] == i * j for j, funcname in enumerate(['int', 'math.ceil', 'fractions.Fraction']): filled_grid[0, 0, 0] = "fractions = __import__('fractions')" filled_grid[0, 0, 0] filled_grid[1, 0, 0] = "math = __import__('math')" filled_grid[1, 0, 0] filled_grid[j, 3, 0] = funcname + ' (' + str(i) + ')' #res = eval(funcname + "(" + "i" + ")") assert filled_grid[j, 3, 0] == eval(funcname + "(" + "i" + ")") #Test X, Y, Z for i in xrange(10): self.code_array[i, 0, 0] = str(i) assert [self.code_array((i, 0, 0)) for i in xrange(10)] == \ map(str, xrange(10)) assert [self.code_array[i, 0, 0] for i in xrange(10)] == range(10) # Test cycle detection filled_grid[0, 0, 0] = "numpy.arange(0, 10, 0.1)" filled_grid[1, 0, 0] = "sum(S[0,0,0])" assert filled_grid[1, 0, 0] == sum(numpy.arange(0, 10, 0.1)) ##filled_grid[0, 0, 0] = "S[5:10, 1, 0]" ##assert filled_grid[0, 0, 0].tolist() == range(7, 12) def test_make_nested_list(self): """Unit test for _make_nested_list""" def gen(): """Nested generator""" yield (("Test" for _ in xrange(2)) for _ in xrange(2)) res = self.code_array._make_nested_list(gen()) assert res == [[["Test" for _ in xrange(2)] for _ in xrange(2)]] param_get_assignment_target_end = [ {'code': "a=5", 'res': 1}, {'code': "a = 5", 'res': 1}, {'code': "5", 'res': -1}, {'code': "a == 5", 'res': -1}, {'code': "", 'res': -1}, {'code': "fractions = __import__('fractions')", 'res': 9}, {'code': "math = __import__('math')", 'res': 4}, {'code': "a = 3==4", 'res': 1}, {'code': "a == 3 < 44", 'res': -1}, {'code': "a != 3 < 44", 'res': -1}, {'code': "a >= 3 < 44", 'res': -1}, {'code': "a = 3 ; a < 44", 'res': None}, ] @params(param_get_assignment_target_end) def test_get_assignment_target_end(self, code, res): """Unit test for _get_assignment_target_end""" module = ast.parse(code) if res is None: try: self.code_array._get_assignment_target_end(module) raise ValueError("Multiple expressions cell not identified") except ValueError: pass else: assert self.code_array._get_assignment_target_end(module) == res param_eval_cell = [ {'key': (0, 0, 0), 'code': "2 + 4", 'res': 6}, {'key': (1, 0, 0), 'code': "S[0, 0, 0]", 'res': None}, {'key': (43, 2, 1), 'code': "X, Y, Z", 'res': (43, 2, 1)}, ] @params(param_eval_cell) def test_eval_cell(self, key, code, res): """Unit test for _eval_cell""" self.code_array[key] = code assert self.code_array._eval_cell(key, code) == res def test_execute_macros(self): """Unit test for execute_macros""" self.code_array.macros = "a = 5\ndef f(x): return x ** 2" self.code_array.execute_macros() assert self.code_array._eval_cell((0, 0, 0), "a") == 5 assert self.code_array._eval_cell((0, 0, 0), "f(2)") == 4 def test_sorted_keys(self): """Unit test for _sorted_keys""" code_array = self.code_array keys = [(1, 0, 0), (2, 0, 0), (0, 1, 0), (0, 99, 0), (0, 0, 0), (0, 0, 99), (1, 2, 3)] assert list(code_array._sorted_keys(keys, (0, 1, 0))) == \ [(0, 1, 0), (0, 99, 0), (1, 2, 3), (0, 0, 99), (0, 0, 0), (1, 0, 0), (2, 0, 0)] sk = list(code_array._sorted_keys(keys, (0, 3, 0), reverse=True)) assert sk == [(0, 1, 0), (2, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 99), (1, 2, 3), (0, 99, 0)] def test_string_match(self): """Tests creation of string_match""" code_array = self.code_array test_strings = [ "", "Hello", " Hello", "Hello ", " Hello ", "Hello\n", "THelloT", " HelloT", "THello ", "hello", "HELLO", "sd" ] search_string = "Hello" # Normal search flags = [] results = [None, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, flags) assert res == result flags = ["MATCH_CASE"] results = [None, 0, 1, 0, 1, 0, 1, 1, 1, None, None, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, flags) assert res == result flags = ["WHOLE_WORD"] results = [None, 0, 1, 0, 1, 0, None, None, None, 0, 0, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, flags) assert res == result def test_findnextmatch(self): """Find method test""" code_array = self.code_array for i in xrange(100): code_array[i, 0, 0] = str(i) assert code_array[3, 0, 0] == 3 assert code_array.findnextmatch((0, 0, 0), "3", "DOWN") == (3, 0, 0) assert code_array.findnextmatch((0, 0, 0), "99", "DOWN") == (99, 0, 0)
class TestCodeArray(object): """Unit tests for CodeArray""" def setup_method(self, method): """Creates empty DataArray""" self.code_array = CodeArray((100, 10, 3), Settings()) param_test_setitem = [ ({ (2, 3, 2): "42" }, { (1, 3, 2): "42" }, { (1, 3, 2): "42", (2, 3, 2): "42" }), ] @pytest.mark.parametrize("data, items, res_data", param_test_setitem) def test_setitem(self, data, items, res_data): """Unit test for __setitem__""" self.code_array.dict_grid.update(data) for key in items: self.code_array[key] = items[key] for key in res_data: assert res_data[key] == self.code_array(key) def test_slicing(self): """Unit test for __getitem__ and __setitem__""" # Test for item getting, slicing, basic evaluation correctness shape = self.code_array.shape x_list = [0, shape[0] - 1] y_list = [0, shape[1] - 1] z_list = [0, shape[2] - 1] for x, y, z in zip(x_list, y_list, z_list): assert self.code_array[x, y, z] is None self.code_array[:x, :y, :z] self.code_array[:x:2, :y:2, :z:-1] get_shape = numpy.array(self.code_array[:, :, :]).shape orig_shape = self.code_array.shape assert get_shape == orig_shape gridsize = 100 filled_grid = CodeArray((gridsize, 10, 1), Settings()) for i in [-2**99, 2**99, 0]: for j in range(gridsize): filled_grid[j, 0, 0] = str(i) filled_grid[j, 1, 0] = str(i) + '+' + str(j) filled_grid[j, 2, 0] = str(i) + '*' + str(j) for j in range(gridsize): assert filled_grid[j, 0, 0] == i assert filled_grid[j, 1, 0] == i + j assert filled_grid[j, 2, 0] == i * j for j, funcname in enumerate( ['int', 'math.ceil', 'fractions.Fraction']): filled_grid[0, 0, 0] = "fractions = __import__('fractions')" filled_grid[0, 0, 0] filled_grid[1, 0, 0] = "math = __import__('math')" filled_grid[1, 0, 0] filled_grid[j, 3, 0] = funcname + ' (' + str(i) + ')' assert filled_grid[j, 3, 0] == eval(funcname + "(" + "i" + ")") # Test X, Y, Z for i in range(10): self.code_array[i, 0, 0] = str(i) assert [self.code_array((i, 0, 0)) for i in range(10)] == \ list(map(str, range(10))) assert [self.code_array[i, 0, 0] for i in range(10)] == list(range(10)) # Test cycle detection filled_grid[0, 0, 0] = "numpy.arange(0, 10, 0.1)" filled_grid[1, 0, 0] = "sum(S[0,0,0])" assert filled_grid[1, 0, 0] == sum(numpy.arange(0, 10, 0.1)) def test_make_nested_list(self): """Unit test for _make_nested_list""" def gen(): """Nested generator""" yield (("Test" for _ in range(2)) for _ in range(2)) res = self.code_array._make_nested_list(gen()) assert res == [[["Test" for _ in range(2)] for _ in range(2)]] data_eval_cell = [ ((0, 0, 0), "2 + 4", 6), ((1, 0, 0), "S[0, 0, 0]", None), ((43, 2, 1), "X, Y, Z", (43, 2, 1)), ] @pytest.mark.parametrize("key, code, res", data_eval_cell) def test_eval_cell(self, key, code, res): """Unit test for _eval_cell""" self.code_array[key] = code assert self.code_array._eval_cell(key, code) == res def test_execute_macros(self): """Unit test for execute_macros""" self.code_array.macros = "a = 5\ndef f(x): return x ** 2" self.code_array.execute_macros() assert self.code_array._eval_cell((0, 0, 0), "a") == 5 assert self.code_array._eval_cell((0, 0, 0), "f(2)") == 4 def test_sorted_keys(self): """Unit test for _sorted_keys""" code_array = self.code_array keys = [(1, 0, 0), (2, 0, 0), (0, 1, 0), (0, 99, 0), (0, 0, 0), (0, 0, 99), (1, 2, 3)] sorted_keys = [(0, 1, 0), (0, 99, 0), (1, 2, 3), (0, 0, 99), (0, 0, 0), (1, 0, 0), (2, 0, 0)] rev_sorted_keys = [(0, 1, 0), (2, 0, 0), (1, 0, 0), (0, 0, 0), (0, 0, 99), (1, 2, 3), (0, 99, 0)] sort_gen = code_array._sorted_keys(keys, (0, 1, 0)) for result, expected_result in zip(sort_gen, sorted_keys): assert result == expected_result rev_sort_gen = code_array._sorted_keys(keys, (0, 3, 0), reverse=True) for result, expected_result in zip(rev_sort_gen, rev_sorted_keys): assert result == expected_result def test_string_match(self): """Tests creation of string_match""" code_array = self.code_array test_strings = [ "", "Hello", " Hello", "Hello ", " Hello ", "Hello\n", "THelloT", " HelloT", "THello ", "hello", "HELLO", "sd" ] search_string = "Hello" # Normal search flags = False, False, False results = [None, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, *flags) assert res == result # Case sensitive flags = False, True, False results = [None, 0, 1, 0, 1, 0, 1, 1, 1, None, None, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, *flags) assert res == result # Word search flags = True, False, False results = [None, 0, 1, 0, 1, 0, None, None, None, 0, 0, None] for test_string, result in zip(test_strings, results): res = code_array.string_match(test_string, search_string, *flags) assert res == result def test_findnextmatch(self): """Find method test""" code_array = self.code_array for i in range(100): code_array[i, 0, 0] = str(i) assert code_array[3, 0, 0] == 3 assert code_array.findnextmatch((0, 0, 0), "3", False) == (3, 0, 0) assert code_array.findnextmatch((0, 0, 0), "99", True) == (99, 0, 0)