def ss2(vars_2): """Create a subspectrum for our first test case. :return: Subspectrum, with .toolbar FirstorderBar, with .vars VARS_2 """ bar2 = FirstOrderBar(callback=fake_callback) bar2.reset(vars_2) ss2 = create_ss(bar2) return ss2
def ss1(vars_1): """Create a subspectrum for our first test case. :return: Subspectrum, with .toolbar FirstorderBar, with .vars VARS_1 """ bar1 = FirstOrderBar(callback=fake_callback) bar1.reset(vars_1) ss1 = create_ss(bar1) return ss1
def test_back_stops_at_beginning(): """Test that if at beginning of history, don't keep moving backwards.""" # GIVEN a history that is currently on the last of three subspectra history = History() testbar = FirstOrderBar() history.change_toolbar(testbar) subspectra = [history.current_subspectrum()] for i in range(2): history.add_subspectrum() history.save() assert history.current == i + 1 subspectra.append(history.current_subspectrum()) assert history.current_subspectrum() is not history._subspectra[i] # WHILE the history is told to move backwards action = history.back() # THEN the current history points to the previous subspectrum assert history.current == 1 assert history.current_subspectrum() is subspectra[1] assert action action = history.back() assert history.current == 0 assert history.current_subspectrum() is subspectra[0] assert action # UNTIL it reaches the beginning of the history, in which case there # is no change. action = history.back() assert history.current == 0 assert history.current_subspectrum() is subspectra[0] assert not action
def test_forward_stops_at_end(): """Test that if at end of history, don't keep moving forwards.""" # GIVEN a history containing three subspectra, set at the first subspectrum history = History() testbar = FirstOrderBar() history.change_toolbar(testbar) subspectra = [history.current_subspectrum()] for i in range(2): history.add_subspectrum() history.save() subspectra.append(history.current_subspectrum()) for i in range(2): history.back() assert history.current_subspectrum() is subspectra[0] # WHILE the history is told to advance for i in range(2): action = history.forward() # THEN the history points to the next subspectrum assert history.current_subspectrum() is subspectra[i + 1] assert action assert history.current == 2 # UNTIL it reaches the end of the history, in which case there is no change ss_2 = history.current_subspectrum() action = history.forward() assert history.current == 2 assert history.current_subspectrum() is ss_2 assert not action
def _initialize_first_order_bar(self): """Instantiate the toolbar for first-order model.""" bar_kwargs = { 'parent': self._top_frame, 'callback': self.update_current_plot } self._first_order_bar = FirstOrderBar(**bar_kwargs)
def test_vars_default_matches_FirstOrderBar_default(vars_default): """Check that the test's default toolbar data matches FirstOrderBar's default data. """ # GIVEN an instance of FirstOrderBar testbar = FirstOrderBar() # THEN its default data matches test_history.vars_default assert testbar.model == 'first_order' # assumed in tests below assert testbar.vars == vars_default
def test_change_toolbar(): """Test that history.toolbar can be changed via method call. This may not be necessary, unless at some point a setter method is actually required instead of direct access to the attribute. """ # Given a history instance and a toolbar instance history = History() toolbar = FirstOrderBar(callback=fake_callback) # WHEN given a toolbar and told to change to it history.change_toolbar(toolbar) # THEN history._toolbar now points to that _toolbar assert history._toolbar is toolbar
def test_add_subspectrum(): """Test that a new, different subspectrum is added to the history._subspectra list.""" # TODO need multiple tests # GIVEN a newly instantiated History object history = History() history._toolbar = FirstOrderBar(callback=fake_callback) # WHEN a new subspectrum is added to it initial_counter = history.current history.add_subspectrum() final_counter = history.current assert history._subspectra[final_counter] is not history._subspectra[ initial_counter] # THEN its counter is incremented by 1 assert final_counter - initial_counter == 1
def test_save(vars_default): """Test that a current subspectrum input state is recorded to the current subspectrum. """ # GIVEN a history with a toolbar reference and a blank subspectrum history = History() history._toolbar = FirstOrderBar(callback=fake_callback) # WHEN told to save state history.save() # THEN the subspectrum's model, vars and toolbar are updated ss = history.current_subspectrum() assert ss.model == 'first_order' assert ss.vars == vars_default assert ss.vars is not history._toolbar.vars # must be a copy to save state assert ss.toolbar is history._toolbar
def test_restore(ss1): """Test that history.toolbar can be restored from the current subspectrum. """ # GIVEN a history instance with a toolbar attribute, # and with a subspectrum containing a toolbar attribute history = History() history._toolbar = FirstOrderBar(callback=fake_callback) history._subspectra[0] = ss1 assert ss1.toolbar is not history._toolbar assert isinstance(ss1.toolbar, FirstOrderBar) # WHEN history restored history.restore() # THEN history._toolbar is updated to point to the current subspectrum # toolbar assert history._toolbar is ss1.toolbar
def test_delete(ss1, ss2): """Test that, when a non-last subspectrum is deleted, it is removed from history._subspectra.""" # GIVEN a history instance with two complete subspectra objects and # pointing to the first subspectrum toolbar = FirstOrderBar(callback=fake_callback) history = History() history._toolbar = toolbar history._subspectra[history.current] = ss1 history._subspectra.append(ss2) assert history.current_subspectrum() is ss1 # WHEN history is told to delete the subspectrum action = history.delete() # THEN current subspectrum set to next subspectrum assert history._subspectra == [ss2] assert history.current_subspectrum() is ss2 assert action
def test_update_all_spectra(ss1, ss2, ss3, x1, x2, y1, y2, y3, y_total): """Test that subspectra and total spectrum are correctly replaced.""" # GIVEN a history object with three subspectra, the second of which is # inactive history = History() ss1.active = True ss2.active = True history._subspectra = [ss1, ss3, ss2] history._toolbar = FirstOrderBar(callback=fake_callback) # WHEN told to update all spectra using a blank spectrum and a list of # lineshape data blank_spectrum = (x1, [0] * 10) lineshapes = [(x1, y1), (x1, y3), (x2, y2)] history.update_all_spectra(blank_spectrum, lineshapes) # THEN all stored lineshapes are updated appropriately new_lineshapes = [(ss1.x, ss1.y), (ss3.x, ss3.y), (ss2.x, ss2.y)] assert np.allclose(lineshapes, new_lineshapes) assert np.allclose(history.total_y, y_total)
def test_forward_updates_history_toolbar(ss1, ss2): """Test that, after going forward one subspectrum, the history's toolbar reference is updated.""" # GIVEN a history instance with two complete subspectra objects and # pointing to the first subspectrum toolbar = FirstOrderBar(callback=fake_callback) history = History() history._toolbar = toolbar history._subspectra[history.current] = ss1 # history.current = 1 history._subspectra.append(ss2) next_ss = history._subspectra[history.current + 1] assert next_ss.toolbar is not history._toolbar # WHEN history is told to go forward history.forward() # THEN history._toolbar was updated assert history._toolbar is history.current_subspectrum().toolbar
def test_forward(): """Test to see if history.forward moves forward 1 subspectrum in _subspectra list. """ # GIVEN a history with two subspectra, set to the first subspectrum history = History() testbar = FirstOrderBar() history.change_toolbar(testbar) history.add_subspectrum() history.save() ss_1 = history.current_subspectrum() history.back() # WHEN the history is advanced forward action = history.forward() # THEN the history points to the second subspectrum assert history.current_subspectrum() is ss_1 assert history.current == 1 assert action
def test_back_updates_history_toolbar(ss1, ss2): """Test that, after going back one subspectrum, the history's toolbar reference is updated.""" # GIVEN a history instance with two complete subspectra objects and # pointing to the more recent subspectrum toolbar = FirstOrderBar(callback=fake_callback) history = History() history._toolbar = toolbar history._subspectra[history.current] = ss1 history.current = 1 history._subspectra.append(ss2) previous_ss = history._subspectra[history.current - 1] assert previous_ss.toolbar is not history._toolbar # WHEN history is told to go back history.back() # THEN history._toolbar was updated assert history._toolbar is history.current_subspectrum().toolbar
def test_back_restores_toolbar_state(ss1, vars_1, ss2): """Test that the previous subspectrum is restored.""" # GIVEN a history instance with two complete subspectra objects and # pointing to the more recent subspectrum toolbar = FirstOrderBar(callback=fake_callback) history = History() history._toolbar = toolbar history._subspectra[history.current] = ss1 history.current = 1 history._subspectra.append(ss2) assert history.current_subspectrum() is ss2 assert history._subspectra[history.current - 1] is not ss2 # WHEN history is told to go back history.back() # THEN the subspectrum is restored ss = history.current_subspectrum() assert ss is ss1 assert ss.vars == vars_1
def test_delete_last_subspectrum(ss1, ss2): """Test that, when a last subspectrum is deleted, it is removed from history._subspectra and history is reset to the previous subspectrum. """ # GIVEN a history instance with two complete subspectra objects and # pointing to the second subspectrum toolbar = FirstOrderBar(callback=fake_callback) history = History() history._toolbar = toolbar history._subspectra[history.current] = ss1 history.current = 1 history._subspectra.append(ss2) assert history.current_subspectrum() is ss2 # WHEN history is told to delete the subspectrum history.delete() # THEN current subspectrum set to previous subspectrum assert history._subspectra == [ss1] assert history.current_subspectrum() is ss1
def test_back_saves_toolbar_first(ss1, ss2, vars_2, vars_default): """Test that the current toolbar info is saved to the current subspectrum before moving back. """ # GIVEN a history instance with two complete subspectra objects and # pointing to the more recent subspectrum toolbar = FirstOrderBar(callback=fake_callback) history = History() history._toolbar = toolbar history._subspectra[history.current] = ss1 history.current = 1 history._subspectra.append(ss2) assert history.current_subspectrum().toolbar is not history._toolbar assert history.current_subspectrum().vars == vars_2 assert history._toolbar.vars == vars_default # WHEN history is told to go back history.back() # THEN the subspectrum was updated ss = history._subspectra[history.current + 1] assert ss is ss2 assert ss.vars == vars_default
def test_back(): """Test to see if history.back moves back 1 subspectrum in _subspectra list. """ # GIVEN a history with two subspectra, set to the more recent subspectrum history = History() testbar = FirstOrderBar() history.change_toolbar(testbar) ss1 = history.current_subspectrum() assert ss1.toolbar is history._toolbar history.add_subspectrum() ss2 = history.current_subspectrum() history.save() assert ss2.toolbar is history._toolbar assert ss1 is not ss2 # WHEN the history is told to move back one step action = history.back() # THEN the current_subspectrum now points to the previous subspectrum ss3 = history.current_subspectrum() assert ss1 is ss3 assert action
next_entry = current_entry.master._find_next_entry(current_entry) next_entry.focus() print('current: ', current_entry.widgetName, current_entry) print('next: ', next_entry.widgetName, next_entry) assert current_entry is not next_entry assert isinstance(current_entry, (tk.Entry, tk.Spinbox)) current_entry = next_entry if __name__ == '__main__': root = tk.Tk() root.title('test toolbars') # Note: immediately packing testbar broke things testbar = FirstOrderBar(root, callback=dummy_callback) # .pack(side=tk.TOP) print(type(testbar)) # noinspection PyProtectedMember first_widget = testbar._fields['# of nuclei'] first_entry = first_widget.entry current_entry = first_entry focusbutton = tk.Button(testbar, name='focus_button', text='Reset Focus', command=lambda: first_entry.focus()) focusnextbutton = tk.Button(testbar, name='focus_next_button', text='Next Focus', command=lambda: focus_next_entry())
def ss3(vars_3): """Create a third subspectrum for testing update_all_spectra.""" bar3 = FirstOrderBar(callback=fake_callback) bar3.reset(vars_3) ss3 = create_ss(bar3) return ss3