Branch data Line data Source code
1 : : /* Copyright 2006-2009 Savarese Software Research Corporation
2 : : *
3 : : * Licensed under the Apache License, Version 2.0 (the "License");
4 : : * you may not use this file except in compliance with the License.
5 : : * You may obtain a copy of the License at
6 : : *
7 : : * https://www.savarese.com/software/ApacheLicense-2.0
8 : : *
9 : : * Unless required by applicable law or agreed to in writing, software
10 : : * distributed under the License is distributed on an "AS IS" BASIS,
11 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : : * See the License for the specific language governing permissions and
13 : : * limitations under the License.
14 : : */
15 : :
16 : : #include <ssrc/wispers/fcgi/parameter_map.h>
17 : : #include <ssrc/wispers/lua/lua.h>
18 : : #include <ssrc/wispers/lua/map.h>
19 : :
20 : : using namespace NS_SSRC_WSPR_LUA;
21 : :
22 : : #define BOOST_TEST_MODULE LuaTest
23 : : #include <boost/test/unit_test.hpp>
24 : :
25 : : class LuaTestFixture {
26 : : protected:
27 : : lua_State *_state;
28 : :
29 : : public:
30 : 5 : LuaTestFixture() {
31 : 5 : _state = luaL_newstate();
32 : 5 : }
33 : :
34 : 5 : ~LuaTestFixture() {
35 : 5 : lua_close(_state);
36 : 5 : }
37 : : };
38 : :
39 : 1 : BOOST_FIXTURE_TEST_SUITE(all, LuaTestFixture)
40 : :
41 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_get_value) {
+ - ]
42 [ + - + - : 1 : BOOST_CHECK(luaL_dofile(_state, TEST_LUA) == 0);
+ - + - +
- + - + -
- + ]
43 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "wspr", "test", "string") == "/tmp");
+ - + - +
- + - -
+ ]
44 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "wspr", "test", "int") == 12);
+ - + - -
+ ]
45 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "wspr", "test", "bool") == true);
+ - + - -
+ ]
46 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "wspr", "test", "foo") == "");
+ - + - +
- + - -
+ ]
47 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "wspr", "test", "foo") == 0);
+ - + - -
+ ]
48 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "wspr", "test", "foo") == false);
+ - + - -
+ ]
49 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "wspr", "foo", "string") == "");
+ - + - +
- + - -
+ ]
50 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "wspr", "foo", "int") == 0);
+ - + - -
+ ]
51 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "wspr", "foo", "bool") == false);
+ - + - -
+ ]
52 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "foo", "test", "string") == "");
+ - + - +
- + - -
+ ]
53 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "foo", "test", "int") == 0);
+ - + - -
+ ]
54 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "foo", "test", "bool") == false);
+ - + - -
+ ]
55 : 1 : }
56 : :
57 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_create_value) {
+ - ]
58 : 1 : const bool b = true;
59 : 1 : const int num = 1776;
60 [ + - ]: 2 : const string str = "bar";
61 : 1 : const char *ch = "char";
62 : :
63 [ + - ]: 1 : create_value(_state, b, "b");
64 [ + - ]: 1 : create_value(_state, num, "num");
65 [ + - ]: 1 : create_value(_state, str, "str");
66 [ + - ]: 1 : create_value(_state, ch, "ch");
67 [ + - ]: 1 : create_value(_state, "literal", "cch");
68 : :
69 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "b") == b);
+ - + - +
- + - + -
+ - - + ]
70 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "num") == num);
+ - + - +
- + - + -
+ - - + ]
71 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "str") == str);
+ - + - +
- + - + -
+ - + - +
- - + ]
72 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "ch") == ch);
+ - + - +
- + - + -
+ - + - +
- - + ]
73 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "cch") == "literal");
+ - + - +
- + - + -
+ - + - +
- - + ]
74 : :
75 : : // create_value creates an entirely new set of tables, so you have
76 : : // to assert conditions immediately after each call, or the previous
77 : : // values will be lost.
78 [ + - ]: 1 : create_value(_state, b, "a", "b", "c", "b");
79 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "a", "b", "c", "b") == b);
+ - + - +
- + - + -
+ - - + ]
80 : :
81 [ + - ]: 1 : create_value(_state, num, "a", "b", "c","num");
82 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "a", "b", "c", "num") == num);
+ - + - +
- + - + -
+ - - + ]
83 : :
84 [ + - ]: 1 : create_value(_state, str, "a", "b", "c", "str");
85 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "a", "b", "c", "str") == str);
+ - + - +
- + - + -
+ - + - +
- - + ]
86 : :
87 [ + - ]: 1 : create_value(_state, ch, "a", "b", "c", "ch");
88 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "a", "b", "c", "ch") == ch);
+ - + - +
- + - + -
+ - + - +
- - + ]
89 : :
90 [ + - ]: 1 : create_value(_state, "literal", "a", "b", "c", "cch");
91 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "a", "b", "c", "cch") == "literal");
+ - + - +
- + - + -
+ - + - +
- - + ]
92 : 1 : }
93 : :
94 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_set_value) {
+ - ]
95 : 1 : const bool b = true;
96 : 1 : const int num = 1776;
97 [ + - ]: 2 : const string str = "bar";
98 : 1 : const char *ch = "char";
99 : :
100 [ + - ]: 1 : set_value(_state, b, "b");
101 [ + - ]: 1 : set_value(_state, num, "num");
102 [ + - ]: 1 : set_value(_state, str, "str");
103 [ + - ]: 1 : set_value(_state, ch, "ch");
104 [ + - ]: 1 : set_value(_state, "literal", "cch");
105 : :
106 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "b") == b);
+ - + - +
- + - + -
+ - - + ]
107 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "num") == num);
+ - + - +
- + - + -
+ - - + ]
108 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "str") == str);
+ - + - +
- + - + -
+ - + - +
- - + ]
109 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "ch") == ch);
+ - + - +
- + - + -
+ - + - +
- - + ]
110 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "cch") == "literal");
+ - + - +
- + - + -
+ - + - +
- - + ]
111 : :
112 [ + - ]: 1 : create_value(_state, b, "a", "b", "c", "b");
113 [ + - ]: 1 : set_value(_state, num, "a", "b", "c","num");
114 [ + - ]: 1 : set_value(_state, str, "a", "b", "c", "str");
115 [ + - ]: 1 : set_value(_state, ch, "a", "b", "c", "ch");
116 [ + - ]: 1 : set_value(_state, "literal", "a", "b", "c", "cch");
117 : :
118 [ + - + - : 1 : BOOST_CHECK(get_value<bool>(_state, "a", "b", "c", "b") == b);
+ - + - +
- + - + -
+ - - + ]
119 [ + - + - : 1 : BOOST_CHECK(get_value<int>(_state, "a", "b", "c", "num") == num);
+ - + - +
- + - + -
+ - - + ]
120 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "a", "b", "c", "str") == str);
+ - + - +
- + - + -
+ - + - +
- - + ]
121 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "a", "b", "c", "ch") == ch);
+ - + - +
- + - + -
+ - + - +
- - + ]
122 [ + - + - : 1 : BOOST_CHECK(get_value<string>(_state, "a", "b", "c", "cch") == "literal");
+ - + - +
- + - + -
+ - + - +
- - + ]
123 : 1 : }
124 : :
125 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_set_field) {
+ - ]
126 : 1 : const bool b = true;
127 : 1 : const int num = 1776;
128 [ + - ]: 2 : const string str = "bar";
129 : 1 : const char *ch = "char";
130 : :
131 [ + - ]: 1 : lua_newtable(_state);
132 : :
133 [ + - ]: 1 : set_field(_state, -1, b, "b");
134 [ + - ]: 1 : set_field(_state, -1, num, "num");
135 [ + - ]: 1 : set_field(_state, -1, str, "str");
136 [ + - ]: 1 : set_field(_state, -1, ch, "ch");
137 [ + - ]: 1 : set_field(_state, -1, "literal", "cch");
138 : :
139 [ + - + - : 1 : BOOST_CHECK(get_field<bool>(_state, -1, "b") == b);
+ - + - +
- + - + -
+ - - + ]
140 [ + - + - : 1 : BOOST_CHECK(get_field<int>(_state, -1, "num") == num);
+ - + - +
- + - + -
+ - - + ]
141 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "str") == str);
+ - + - +
- + - + -
+ - + - +
- - + ]
142 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "ch") == ch);
+ - + - +
- + - + -
+ - + - +
- - + ]
143 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "cch") == "literal");
+ - + - +
- + - + -
+ - + - +
- - + ]
144 : :
145 [ + - ]: 1 : set_field(_state, -1, b, "a", "b", "c", "b");
146 [ + - ]: 1 : set_field(_state, -1, num, "a", "b", "c","num");
147 [ + - ]: 1 : set_field(_state, -1, str, "a", "b", "c", "str");
148 [ + - ]: 1 : set_field(_state, -1, ch, "a", "b", "c", "ch");
149 [ + - ]: 1 : set_field(_state, -1, "literal", "a", "b", "c", "cch");
150 : :
151 [ + - + - : 1 : BOOST_CHECK(get_field<bool>(_state, -1, "a", "b", "c", "b") == b);
+ - + - +
- + - + -
+ - - + ]
152 [ + - + - : 1 : BOOST_CHECK(get_field<int>(_state, -1, "a", "b", "c", "num") == num);
+ - + - +
- + - + -
+ - - + ]
153 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "a", "b", "c", "str") == str);
+ - + - +
- + - + -
+ - + - +
- - + ]
154 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "a", "b", "c", "ch") == ch);
+ - + - +
- + - + -
+ - + - +
- - + ]
155 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "a", "b", "c", "cch") == "literal");
+ - + - +
- + - + -
+ - + - +
- - + ]
156 : :
157 [ + - ]: 1 : lua_pop(_state, 1);
158 : 1 : }
159 : :
160 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_map_to_table) {
+ - ]
161 : : typedef NS_SSRC_WSPR_FCGI::parameter_map parameter_map;
162 : : typedef parameter_map::value_type value_type;
163 [ + - ]: 2 : parameter_map map;
164 : :
165 [ + - ]: 1 : luaL_openlibs(_state);
166 : :
167 [ + - + - : 1 : map.insert(value_type("a", "1"));
+ - ]
168 [ + - + - : 1 : map.insert(value_type("a", "2"));
+ - ]
169 [ + - + - : 1 : map.insert(value_type("a", "3"));
+ - ]
170 [ + - + - : 1 : map.insert(value_type("foo", "bar"));
+ - ]
171 [ + - + - : 1 : map.insert(value_type("bar", "foo"));
+ - ]
172 : :
173 [ + - ]: 1 : map_to_table(_state, map);
174 : :
175 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "foo") == "bar");
+ - + - +
- + - + -
+ - + - +
- - + ]
176 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, "bar") == "foo");
+ - + - +
- + - + -
+ - + - +
- - + ]
177 : :
178 [ + - ]: 1 : lua_getglobal(_state, "table");
179 [ + - ]: 1 : lua_getfield(_state, -1, "sort");
180 [ + - ]: 1 : lua_getfield(_state, -3, "a");
181 : :
182 [ + - + - : 1 : BOOST_CHECK(lua_pcall(_state, 1, 0, 0) == 0);
+ - + - +
- + - + -
+ - - + ]
183 : :
184 [ + - ]: 1 : lua_pop(_state, 1);
185 [ + - ]: 1 : lua_getfield(_state, -1, "a");
186 : :
187 [ + - + - : 1 : BOOST_CHECK(3u == lua_objlen(_state, -1));
+ - + - +
- + - + -
+ - - + ]
188 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, 1) == "1");
+ - + - +
- + - + -
+ - + - +
- - + ]
189 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, 2) == "2");
+ - + - +
- + - + -
+ - + - +
- - + ]
190 [ + - + - : 1 : BOOST_CHECK(get_field<string>(_state, -1, 3) == "3");
+ - + - +
- + - + -
+ - + - +
- - + ]
191 : :
192 [ + - ]: 1 : lua_pop(_state, 2);
193 : 1 : }
194 : :
195 [ + - + - ]: 3 : BOOST_AUTO_TEST_SUITE_END()
|