class TimeMachineTestCase(unittest.TestCase): def setUp(self): self.tm = TimeMachine() def test_one_change_list(self): l = [1, 2, 3, 4] self.tm(do=partial(l.append, 5), undo=l.pop) self.assertEqual(l, [1, 2, 3, 4, 5]) self.tm.reset() self.assertEqual(l, [1, 2, 3, 4]) def test_str_handles_different_types_of_stored_operations(self): def normal_function(): pass partial_function = partial(str, 1) self.tm(do=normal_function, undo=partial_function) if six.PY2: self.assertEqual( self.tm.__str__().split('\n')[2:-1], ["undo: <type 'str'> (1,) None", 'redo: normal_function']) elif six.PY3: self.assertEqual( self.tm.__str__().split('\n')[2:-1], ["undo: <class 'str'> (1,) None", 'redo: normal_function']) def test_with_statement(self): l = [1, 2, 3, 4] with TimeMachine() as tm: tm(do=partial(l.append, 33), undo=partial(l.pop)) tm(do=partial(l.append, 66), undo=partial(l.pop)) tm(do=partial(l.append, 99), undo=partial(l.pop)) self.assertEqual(l, [1, 2, 3, 4])
class TimeMachineTestCase(unittest.TestCase): def setUp(self): self.tm = TimeMachine() def test_one_change_list(self): l = [1, 2, 3, 4] self.tm(do=partial(l.append, 5), undo=l.pop) self.assertEqual(l, [1, 2, 3, 4, 5]) self.tm.reset() self.assertEqual(l, [1, 2, 3, 4]) def test_str_handles_different_types_of_stored_operations(self): def normal_function(): pass partial_function = partial(str, 1) self.tm(do=normal_function, undo=partial_function) self.assertEqual(self.tm.__str__().split('\n')[2:-1], ["undo: " + str(str) + " (1,) {}", 'redo: normal_function']) def test_with_statement(self): l = [1, 2, 3, 4] with TimeMachine() as tm: tm(do=partial(l.append, 33), undo=partial(l.pop)) tm(do=partial(l.append, 66), undo=partial(l.pop)) tm(do=partial(l.append, 99), undo=partial(l.pop)) self.assertEqual(l, [1, 2, 3, 4])
def test_str_handles_different_types_of_stored_operations(self): tm = TimeMachine() def normal_function(): pass partial_function = partial(str, 1) tm(do=normal_function, undo=partial_function) assert tm.__str__().split('\n')[2:-1] == ["undo: " + str(str) + " (1,) {}", 'redo: normal_function']