def testOverwrite(self): self.events = [[[1], 0], [[1, 3], 20], [[1, 2, 3], 50], [[1], 20], [[1], 30], [[2], 10], [[2, 3], 60], [[1], 10], [[2], 10], [[3], 20]] self.random_name = str(np.random.rand()) while self.random_name == "0": # just in case... self.random_name = str(np.random.rand()) self.random_name = self.random_name[2:] os.mkdir(self.random_name) self.assertEqual(pulsebox.ino_make(self.random_name, self.events, overwrite=True), 0) with pytest.raises(EnvironmentError): pulsebox.ino_make(self.random_name, self.events, overwrite=False) os.remove(os.path.join(self.random_name, self.random_name + ".ino")) os.rmdir(self.random_name)
def gui_ino_make(widget, ino_file="test.ino", verbose=True): """ Collect unsorted channel events from entry fields Args: * widget: An obligatory GTK arg. Kwargs: * ino_file: Save results to this file (defaults to "test.ino"). * verbose: Toggle verbose output (defaults to False). """ try: assert type(ino_file) is str except AssertionError: if verbose: sys.stderr.write("gui_ino_make: ERROR: ino_file {} is not a string. Exiting.\n".format(ino_file)) return 1 events_unsorted = [] if 1 > 0: # TODO: user-chosen filename + check for chan_num in range(pbox.ARDUINO_CH_COUNT): if checks_chans[chan_num].get_active(): # if checkbox ticked # get data written into channel's entry field entry = entries_chans[chan_num].get_text().split(" ") if len(entry) > 0: # checkbox can be ticked with no entry # put data in a list beginning with channel number chan_events = [["CH" + str(chan_num + 1)]] chan_events.append(entry) chan_events = sum(chan_events, []) # flatten the list # remove empty '' if present for eachrun in range(chan_events.count('')): chan_events.remove('') # convert event times to int chan_events[1:] = [int(x) for x in chan_events[1:]] events_unsorted.append(chan_events) # TODO: improve status updates (success/fail, retcodes...) statusbar.push(1, "Making .ino...") events_sorted = pbox.ino_events_sort(events_unsorted) pbox.ino_make(ino_file, events_sorted, overwrite=True, verbose=True) statusbar.push(1, ".ino successfully created")
def testValid(self): print os.getcwd() self.ref_file = os.path.join("tests", "test.ino") self.events = [[[1], 0], [[1, 3], 20], [[1, 2, 3], 50], [[1], 20], [[1], 30], [[2], 10], [[2, 3], 60], [[1], 10], [[2], 10], [[3], 20]] self.random_name = str(np.random.rand()) while self.random_name == "0": # just in case... self.random_name = str(np.random.rand()) self.random_name = self.random_name[2:] self.new_file = os.path.join(self.random_name, self.random_name + ".ino") self.assertEqual(pulsebox.ino_make(self.random_name, self.events), 0) self.assertTrue(filecmp.cmp(self.ref_file, self.new_file, shallow=False)) os.remove(self.new_file) os.rmdir(self.random_name)
def testValueError(self): self.channel_count = pulsebox.ARDUINO_CH_COUNT with pytest.raises(ValueError): pulsebox.ino_make("example.ino", [[[1], 0], [[0], 20]]) with pytest.raises(ValueError): pulsebox.ino_make("example.ino", [[[1], 0], [[self.channel_count + 1], 20]]) with pytest.raises(ValueError): pulsebox.ino_make("example.ino", [[[1], 0], [[1, 3], -2]])
def testTypeError(self): with pytest.raises(TypeError): pulsebox.ino_make(1, [[[1], 0], [[1, 3], 20]]) with pytest.raises(TypeError): pulsebox.ino_make(0.1, [[[1], 0], [[1, 3], 20]]) with pytest.raises(TypeError): pulsebox.ino_make(True, [[[1], 0], [[1, 3], 20]]) with pytest.raises(TypeError): pulsebox.ino_make("", [[[1], 0], [[1, 3], 20]]) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", 1) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", 0.1) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", "a") with pytest.raises(TypeError): pulsebox.ino_make("example.ino", True) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", []) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", [[[1], 0], 1]) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", [[[1], 0], [1]]) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", [[[1], 0], [1, 2, 3]]) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", [[[1], 0], [1, 2]]) with pytest.raises(TypeError): pulsebox.ino_make("example.ino", [[[1], 0], [[1, 3], "a"]])