Skip to content

Commit abead75

Browse files
committed
Unit tests for FunctionReplace
1 parent 4499afd commit abead75

2 files changed

Lines changed: 118 additions & 1 deletion

File tree

src/s2e2/functions/function_replace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _check_arguments(self):
2020

2121

2222
def _result(self):
23-
if not self._arguments[0]:
23+
if self._arguments[0] is None:
2424
return None
2525

2626
source = self._arguments[0]
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from s2e2.error import ExpressionError
2+
from s2e2.functions.function_replace import FunctionReplace
3+
4+
import pytest
5+
6+
7+
class TestFunctionReplace:
8+
9+
def setup_class(self):
10+
self.function = FunctionReplace()
11+
12+
13+
def teardown_class(self):
14+
self.function = None
15+
16+
17+
def test_positive_good_arguments_stack_size(self):
18+
stack = ['ABA', 'A', 'B']
19+
self.function.invoke(stack)
20+
assert len(stack) == 1
21+
22+
23+
def test_positive_good_arguments_result_type(self):
24+
stack = ['ABA', 'A', 'B']
25+
self.function.invoke(stack)
26+
assert isinstance(stack[0], str)
27+
28+
29+
def test_positive_string_replace_result_value(self):
30+
stack = ['ABA', 'A', 'B']
31+
self.function.invoke(stack)
32+
assert stack[0] == 'BBB'
33+
34+
35+
def test_positive_regex_replace_result_value(self):
36+
stack = ['ABCABA', 'A.*?C', 'D']
37+
self.function.invoke(stack)
38+
assert stack[0] == 'DABA'
39+
40+
41+
def test_positive_special_symbol_replace_result_value(self):
42+
stack = ['A * B == C', '\\*', '+']
43+
self.function.invoke(stack)
44+
assert stack[0] == 'A + B == C'
45+
46+
47+
def test_positive_first_argument_none_result_value(self):
48+
stack = [None, 'A', 'B']
49+
self.function.invoke(stack)
50+
assert stack[0] is None
51+
52+
53+
def test_positive_first_argument_empty_string_result_value(self):
54+
stack = ['', 'A', 'B']
55+
self.function.invoke(stack)
56+
assert stack[0] == ''
57+
58+
59+
def test_positive_third_argument_empty_string_result_value(self):
60+
stack = ['ABA', 'B', '']
61+
self.function.invoke(stack)
62+
assert stack[0] == 'AA'
63+
64+
65+
def test_positive_more_arguments_stack_size(self):
66+
stack = [False, 'ABA', 'A', 'B']
67+
self.function.invoke(stack)
68+
assert len(stack) == 2
69+
70+
71+
def test_negative_fewer_arguments(self):
72+
stack = ['ABA', 'A']
73+
with pytest.raises(ExpressionError) as ex:
74+
self.function.invoke(stack)
75+
assert 'Not enough arguments' in str(ex.value)
76+
77+
78+
def test_negative_first_argument_wrong_type(self):
79+
stack = [5, 'A', 'B']
80+
with pytest.raises(ExpressionError) as ex:
81+
self.function.invoke(stack)
82+
assert 'Invalid arguments' in str(ex.value)
83+
84+
85+
def test_negative_second_argument_empty_string(self):
86+
stack = ['ABA', '', 'B']
87+
with pytest.raises(ExpressionError) as ex:
88+
self.function.invoke(stack)
89+
assert 'Invalid arguments' in str(ex.value)
90+
91+
92+
def test_negative_second_argument_none(self):
93+
stack = ['ABA', None, 'B']
94+
with pytest.raises(ExpressionError) as ex:
95+
self.function.invoke(stack)
96+
assert 'Invalid arguments' in str(ex.value)
97+
98+
99+
def test_negative_second_argument_wrong_type(self):
100+
stack = ['ABA', 5, 'B']
101+
with pytest.raises(ExpressionError) as ex:
102+
self.function.invoke(stack)
103+
assert 'Invalid arguments' in str(ex.value)
104+
105+
106+
def test_negative_third_argument_none(self):
107+
stack = ['ABA', 'A', None]
108+
with pytest.raises(ExpressionError) as ex:
109+
self.function.invoke(stack)
110+
assert 'Invalid arguments' in str(ex.value)
111+
112+
113+
def test_negative_third_argument_wrong_type(self):
114+
stack = ['ABA', 'A', 5]
115+
with pytest.raises(ExpressionError) as ex:
116+
self.function.invoke(stack)
117+
assert 'Invalid arguments' in str(ex.value)

0 commit comments

Comments
 (0)