6
6
7
7
8
8
class ForthTest (unittest .TestCase ):
9
+ # Utility functions
10
+ def assertRaisesWithMessage (self , exception ):
11
+ return self .assertRaisesRegex (exception , r".+" )
9
12
10
- # parsing and numbers
11
13
14
+ class ParsingAndNumbersTest (ForthTest ):
12
15
def test_numbers_just_get_pushed_onto_the_stack (self ):
13
16
self .assertEqual (evaluate (["1 2 3 4 5" ]), [1 , 2 , 3 , 4 , 5 ])
14
17
15
- # addition
16
18
19
+ class AdditionTest (ForthTest ):
17
20
def test_can_add_two_numbers (self ):
18
21
self .assertEqual (evaluate (["1 2 +" ]), [3 ])
19
22
@@ -25,8 +28,8 @@ def test_errors_if_there_is_only_one_value_on_the_stack(self):
25
28
with self .assertRaisesWithMessage (StackUnderflowError ):
26
29
evaluate (["1 +" ])
27
30
28
- # subtraction
29
31
32
+ class SubtractionTest (ForthTest ):
30
33
def test_can_subtract_two_numbers (self ):
31
34
self .assertEqual (evaluate (["3 4 -" ]), [- 1 ])
32
35
@@ -38,8 +41,8 @@ def test_errors_if_there_is_only_one_value_on_the_stack(self):
38
41
with self .assertRaisesWithMessage (StackUnderflowError ):
39
42
evaluate (["1 -" ])
40
43
41
- # multiplication
42
44
45
+ class MultiplicationTest (ForthTest ):
43
46
def test_can_multiply_two_numbers (self ):
44
47
self .assertEqual (evaluate (["2 4 *" ]), [8 ])
45
48
@@ -51,8 +54,8 @@ def test_errors_if_there_is_only_one_value_on_the_stack(self):
51
54
with self .assertRaisesWithMessage (StackUnderflowError ):
52
55
evaluate (["1 *" ])
53
56
54
- # division
55
57
58
+ class DivisionTest (ForthTest ):
56
59
def test_can_divide_two_numbers (self ):
57
60
self .assertEqual (evaluate (["12 3 /" ]), [4 ])
58
61
@@ -72,16 +75,16 @@ def test_errors_if_there_is_only_one_value_on_the_stack(self):
72
75
with self .assertRaisesWithMessage (StackUnderflowError ):
73
76
evaluate (["1 /" ])
74
77
75
- # combined arithmetic
76
78
79
+ class CombinedArithmeticTest (ForthTest ):
77
80
def test_addition_and_subtraction (self ):
78
81
self .assertEqual (evaluate (["1 2 + 4 -" ]), [- 1 ])
79
82
80
83
def test_multiplication_and_division (self ):
81
84
self .assertEqual (evaluate (["2 4 * 3 /" ]), [2 ])
82
85
83
- # dup
84
86
87
+ class DupTest (ForthTest ):
85
88
def test_copies_a_value_on_the_stack (self ):
86
89
self .assertEqual (evaluate (["1 dup" ]), [1 , 1 ])
87
90
@@ -92,8 +95,8 @@ def test_errors_if_there_is_nothing_on_the_stack(self):
92
95
with self .assertRaisesWithMessage (StackUnderflowError ):
93
96
evaluate (["dup" ])
94
97
95
- # drop
96
98
99
+ class DropTest (ForthTest ):
97
100
def test_removes_the_top_value_on_the_stack_if_it_is_the_only_one (self ):
98
101
self .assertEqual (evaluate (["1 drop" ]), [])
99
102
@@ -104,8 +107,8 @@ def test_errors_if_there_is_nothing_on_the_stack(self):
104
107
with self .assertRaisesWithMessage (StackUnderflowError ):
105
108
evaluate (["drop" ])
106
109
107
- # swap
108
110
111
+ class SwapTest (ForthTest ):
109
112
def test_swaps_the_top_two_values_on_the_stack_if_they_are_the_only_ones (self ):
110
113
self .assertEqual (evaluate (["1 2 swap" ]), [2 , 1 ])
111
114
@@ -120,8 +123,8 @@ def test_errors_if_there_is_only_one_value_on_the_stack(self):
120
123
with self .assertRaisesWithMessage (StackUnderflowError ):
121
124
evaluate (["1 swap" ])
122
125
123
- # over
124
126
127
+ class OverTest (ForthTest ):
125
128
def test_copies_the_second_element_if_there_are_only_two (self ):
126
129
self .assertEqual (evaluate (["1 2 over" ]), [1 , 2 , 1 ])
127
130
@@ -136,8 +139,8 @@ def test_errors_if_there_is_only_one_value_on_the_stack(self):
136
139
with self .assertRaisesWithMessage (StackUnderflowError ):
137
140
evaluate (["1 over" ])
138
141
139
- # user-defined words
140
142
143
+ class UserDefinedWordsTest (ForthTest ):
141
144
def test_can_consist_of_built_in_words (self ):
142
145
self .assertEqual (evaluate ([": dup-twice dup dup ;" , "1 dup-twice" ]), [1 , 1 , 1 ])
143
146
@@ -171,8 +174,8 @@ def test_errors_if_executing_a_non_existent_word(self):
171
174
with self .assertRaisesWithMessage (ValueError ):
172
175
evaluate (["foo" ])
173
176
174
- # case-insensitivity
175
177
178
+ class CaseInsensitivityTest (ForthTest ):
176
179
def test_dup_is_case_insensitive (self ):
177
180
self .assertEqual (evaluate (["1 DUP Dup dup" ]), [1 , 1 , 1 , 1 ])
178
181
0 commit comments