1 |
commit: f7c40576759f562a3911b2adbc49eabb4e94a0e4 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Thu Mar 31 03:13:53 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Thu Mar 31 08:29:31 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=f7c40576 |
7 |
|
8 |
Refactor unit test to allow adding new rules |
9 |
|
10 |
Extract some logic into subclass in order to make it possible to |
11 |
add new rules. |
12 |
|
13 |
--- |
14 |
test/walker_test.cpp | 67 +++++++++++++++++++++++++++---------------------- |
15 |
1 files changed, 37 insertions(+), 30 deletions(-) |
16 |
|
17 |
diff --git a/test/walker_test.cpp b/test/walker_test.cpp |
18 |
index 41720ad..c026605 100644 |
19 |
--- a/test/walker_test.cpp |
20 |
+++ b/test/walker_test.cpp |
21 |
@@ -38,10 +38,9 @@ class walker_test: public ::testing::Test |
22 |
pANTLR3_INPUT_STREAM input; |
23 |
plibbashLexer lxr; |
24 |
pANTLR3_COMMON_TOKEN_STREAM tstream; |
25 |
- plibbashParser psr; |
26 |
- libbashParser_arithmetics_return langAST; |
27 |
- pANTLR3_COMMON_TREE_NODE_STREAM nodes; |
28 |
protected: |
29 |
+ pANTLR3_COMMON_TREE_NODE_STREAM nodes; |
30 |
+ plibbashParser psr; |
31 |
virtual void SetUp() |
32 |
{ |
33 |
walker = shared_ptr<interpreter>(new interpreter); |
34 |
@@ -55,30 +54,13 @@ protected: |
35 |
lxr->free(lxr); |
36 |
input->close(input); |
37 |
} |
38 |
- void init_walker(const char* script); |
39 |
+ void init_parser(const char*); |
40 |
public: |
41 |
plibbashWalker treePsr; |
42 |
shared_ptr<interpreter> walker; |
43 |
- |
44 |
- int run_arithmetic(const char* script) |
45 |
- { |
46 |
- init_walker(script); |
47 |
- return treePsr->arithmetics(treePsr); |
48 |
- } |
49 |
- |
50 |
- void check_arithmetic_assignment(const char* script, |
51 |
- const string& name, |
52 |
- int exp_value) |
53 |
- { |
54 |
- // the return value of the arithmetic expression should be equal to |
55 |
- // the new value of the variable |
56 |
- EXPECT_EQ(exp_value, run_arithmetic(script)); |
57 |
- EXPECT_EQ(exp_value, walker->resolve<int>(name)); |
58 |
- } |
59 |
}; |
60 |
|
61 |
- |
62 |
-void walker_test::init_walker(const char *script){ |
63 |
+void walker_test::init_parser(const char *script){ |
64 |
|
65 |
auto start = reinterpret_cast<pANTLR3_UINT8>(const_cast<char *>(script)); |
66 |
input = antlr3NewAsciiStringInPlaceStream(start, |
67 |
@@ -115,17 +97,42 @@ void walker_test::init_walker(const char *script){ |
68 |
ANTLR3_FPRINTF(stderr, "Out of memory trying to allocate parser\n"); |
69 |
FAIL(); |
70 |
} |
71 |
+} |
72 |
|
73 |
- langAST = psr->arithmetics(psr); |
74 |
- nodes = antlr3CommonTreeNodeStreamNewTree(langAST.tree, |
75 |
+class arithmetic_walker: public walker_test |
76 |
+{ |
77 |
+ libbashParser_arithmetics_return langAST; |
78 |
+protected: |
79 |
+ void init_walker(const char* script) |
80 |
+ { |
81 |
+ init_parser(script); |
82 |
+ langAST = psr->arithmetics(psr); |
83 |
+ nodes = antlr3CommonTreeNodeStreamNewTree(langAST.tree, |
84 |
ANTLR3_SIZE_HINT); |
85 |
- treePsr = libbashWalkerNew(nodes); |
86 |
- walker->define("value", 100); |
87 |
- set_interpreter(walker); |
88 |
-} |
89 |
+ treePsr = libbashWalkerNew(nodes); |
90 |
+ walker->define("value", 100); |
91 |
+ set_interpreter(walker); |
92 |
+ } |
93 |
+ |
94 |
+ int run_arithmetic(const char* script) |
95 |
+ { |
96 |
+ init_walker(script); |
97 |
+ return treePsr->arithmetics(treePsr); |
98 |
+ } |
99 |
+ |
100 |
+ void check_arithmetic_assignment(const char* script, |
101 |
+ const string& name, |
102 |
+ int exp_value) |
103 |
+ { |
104 |
+ // the return value of the arithmetic expression should be equal to |
105 |
+ // the new value of the variable |
106 |
+ EXPECT_EQ(exp_value, run_arithmetic(script)); |
107 |
+ EXPECT_EQ(exp_value, walker->resolve<int>(name)); |
108 |
+ } |
109 |
+}; |
110 |
|
111 |
#define TEST_BINARY_ARITHMETIC(name, script, exp_value)\ |
112 |
- TEST_F(walker_test, name)\ |
113 |
+ TEST_F(arithmetic_walker, name)\ |
114 |
{EXPECT_EQ(exp_value, run_arithmetic(script));} |
115 |
|
116 |
TEST_BINARY_ARITHMETIC(logicor_true, "0 || -2", 1) |
117 |
@@ -167,7 +174,7 @@ TEST_BINARY_ARITHMETIC(complex_cal2, "10*${value}<<3%2**5", 8000) |
118 |
TEST_BINARY_ARITHMETIC(complex_cal3, "(20&5|3||1*100-20&5*10)+~(2*5)", -10) |
119 |
|
120 |
#define TEST_ARITHMETIC_ASSIGNMENT(name, script, var_name, exp_value)\ |
121 |
- TEST_F(walker_test, name) \ |
122 |
+ TEST_F(arithmetic_walker, name) \ |
123 |
{ check_arithmetic_assignment(script, var_name, exp_value); } |
124 |
|
125 |
TEST_ARITHMETIC_ASSIGNMENT(assignment, "new_var=10", "new_var", 10) |