Skip to content

Commit 7f2e34b

Browse files
author
thinwybk
committed
add: test undo / change: cls name, setup, teardown
1 parent a6bcf8e commit 7f2e34b

1 file changed

Lines changed: 30 additions & 21 deletions

File tree

‎test_command.py‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,52 @@
22
import unittest, os, shutil, subprocess
33

44

5-
class StrategyTest(unittest.TestCase):
5+
class CommandTest(unittest.TestCase):
66

7-
def __get_directories(self):
7+
@classmethod
8+
def __get_test_directory(self):
89
"""
9-
Get the directories relevant for tests:
10-
- self.file_dir: the directory of this script
11-
- self.test_dir: the root directory for tests
10+
Get the temporary directory for the tests.
1211
"""
13-
self.file_dir = os.path.dirname(os.path.realpath(__file__))
14-
self.test_dir = os.path.join(self.file_dir, 'test_command')
12+
self.test_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_command')
1513

16-
def setUp(self):
14+
@classmethod
15+
def setUpClass(self):
1716
"""
18-
Creates a temporary directory and file:
19-
./test_command
17+
- Create a temporary directory and file
18+
/test_command
2019
/foo.txt
20+
- get the temporary test directory
21+
- and initializes the command stack.
2122
"""
2223
os.mkdir('test_command')
2324
open('test_command/foo.txt', 'w').close()
24-
self.__get_directories()
25-
26-
def test_sequential_execution(self):
25+
self.__get_test_directory()
2726
self.command_stack = []
2827
self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'foo.txt'), os.path.join(self.test_dir, 'bar.txt')))
2928
self.command_stack.append(MoveFileCommand(os.path.join(self.test_dir, 'bar.txt'), os.path.join(self.test_dir, 'baz.txt')))
29+
30+
def test_sequential_execution(self):
3031
self.command_stack[0].execute()
31-
output_after_first_command = os.listdir(self.test_dir)
32-
self.assertEqual(output_after_first_command[0], 'bar.txt')
32+
output_after_first_execution = os.listdir(self.test_dir)
33+
self.assertEqual(output_after_first_execution[0], 'bar.txt')
3334
self.command_stack[1].execute()
34-
output_after_second_command = os.listdir(self.test_dir)
35-
self.assertEqual(output_after_second_command[0], 'baz.txt')
35+
output_after_second_execution = os.listdir(self.test_dir)
36+
self.assertEqual(output_after_second_execution[0], 'baz.txt')
37+
38+
def test_sequential_undo(self):
39+
self.command_stack = list(reversed(self.command_stack))
40+
self.command_stack[0].undo()
41+
output_after_first_undo = os.listdir(self.test_dir)
42+
self.assertEqual(output_after_first_undo[0], 'bar.txt')
43+
self.command_stack[1].undo()
44+
output_after_second_undo = os.listdir(self.test_dir)
45+
self.assertEqual(output_after_second_undo[0], 'foo.txt')
3646

37-
def tearDown(self):
47+
@classmethod
48+
def tearDownClass(self):
3849
"""
39-
Removes the temporary directory and its content:
40-
./test_command
41-
...
50+
Remove the temporary directory /test_command and its content.
4251
"""
4352
shutil.rmtree('test_command')
4453

0 commit comments

Comments
 (0)