Branch data Line data Source code
1 : : /* Copyright 2006-2011 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/lua/Properties.h>
17 : : #include <ssrc/wispers/utility/NumberVisitor.h>
18 : : #include "table_lua.h"
19 : :
20 : : #define BOOST_TEST_MODULE PropertiesTest
21 : : #include <boost/test/unit_test.hpp>
22 : :
23 : : using ssrc::wispers::utility::Properties;
24 : : using ssrc::wispers::utility::primitive_property_vector;
25 : : using ssrc::wispers::utility::property_vector;
26 : : using ssrc::wispers::utility::NumberVisitor;
27 : : using ssrc::wispers::utility::get_number;
28 : : using namespace NS_SSRC_WSPR_LUA;
29 : :
30 : : struct PropertiesTestFixture {
31 : : lua_State *_state;
32 : :
33 : 3 : PropertiesTestFixture() {
34 : 3 : _state = luaL_newstate();
35 : 3 : luaL_openlibs(_state);
36 : 3 : }
37 : :
38 : 3 : ~PropertiesTestFixture() {
39 : 3 : lua_close(_state);
40 : 3 : }
41 : : };
42 : :
43 : 1 : BOOST_FIXTURE_TEST_SUITE(all, PropertiesTestFixture)
44 : :
45 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_to_table) {
+ - ]
46 : 2 : Properties properties;
47 [ + - + - ]: 2 : primitive_property_vector v;
48 : :
49 [ + - ]: 1 : properties.set(1, "a", "b", "c");
50 [ + - ]: 1 : properties.set(2, "a", "b", "d");
51 [ + - ]: 1 : properties.set(3, "a", "b", "e");
52 : :
53 [ + + ]: 11 : for(int i = 0; i < 10; ++i) {
54 [ + - + - : 10 : v.push_back(i);
+ - ]
55 : : }
56 : :
57 [ + - + - : 1 : properties.set(v, "b", "c");
+ - ]
58 : :
59 [ + - ]: 1 : property_vector & p = properties.create_property_vector("c", "d");
60 : :
61 [ + + ]: 11 : for(int i = 0; i < 10; ++i) {
62 : : Properties *node;
63 [ + - + - : 10 : p.push_back(node = new Properties());
+ - ]
64 [ + - ]: 10 : node->set(i, "foo");
65 [ + - ]: 10 : property_vector & pv = node->create_property_vector("bar");
66 [ + - + - : 10 : pv.push_back(node = new Properties());
+ - ]
67 [ + - ]: 10 : node->set(i, "foo");
68 : : }
69 : :
70 [ + - ]: 1 : properties_to_table(_state, properties);
71 [ + - ]: 1 : lua_setglobal(_state, "p");
72 : :
73 [ + - + - : 1 : int result = luaL_dofile(_state, PROPTEST_LUA);
+ - - + ]
74 : :
75 [ - + ]: 1 : if(result != 0) {
76 [ # # # # : 0 : std::cerr << lua_tostring(_state, -1) << std::endl;
# # ]
77 : : }
78 : :
79 [ + - + - : 1 : BOOST_REQUIRE(result == 0);
+ - + - +
- + - + -
- + ]
80 : :
81 [ + - ]: 1 : lua_getglobal(_state, "test_properties_to_table");
82 : :
83 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(pcall(_state, &properties));
+ - + - +
- + - + -
+ - - + #
# # # # #
# # # # #
# # # # #
# # ]
84 : 1 : }
85 : :
86 : 2 : void test_to_properties(const properties_ptr & properties) {
87 [ + - + - : 2 : BOOST_REQUIRE(*properties->get_ptr<string>("test", "string") == "/tmp");
+ - + - +
- - + ]
88 [ + - + - : 2 : BOOST_REQUIRE(*properties->get_ptr<lua_Number>("test", "number") == 12);
+ - + - -
+ ]
89 [ + - + - : 2 : BOOST_REQUIRE(*properties->get_ptr<bool>("test", "bool") == true);
+ - + - -
+ ]
90 [ + - + - : 2 : BOOST_REQUIRE(*properties->get_ptr<string>("test", "node", "a") == "A");
+ - + - +
- - + ]
91 [ + - + - : 2 : BOOST_REQUIRE(*properties->get_ptr<string>("test", "node", "b") == "B");
+ - + - +
- - + ]
92 : :
93 : : primitive_property_vector *ppv =
94 : 2 : properties->get_ptr<primitive_property_vector>("test","primitive_vector");
95 : :
96 [ + - + - : 2 : BOOST_REQUIRE(ppv != 0);
+ - - + ]
97 [ + - + - : 2 : BOOST_REQUIRE(ppv->size() == 3);
+ - + - -
+ ]
98 [ + - + - : 2 : BOOST_REQUIRE(boost::get<lua_Number>((*ppv)[0]) == 1);
+ - + - +
- - + ]
99 [ + - + - : 2 : BOOST_REQUIRE(boost::get<lua_Number>((*ppv)[1]) == 2);
+ - + - +
- - + ]
100 [ + - + - : 2 : BOOST_REQUIRE(boost::get<lua_Number>((*ppv)[2]) == 3);
+ - + - +
- - + ]
101 : :
102 : 2 : int value = 0;
103 [ + - ]: 2 : boost::apply_visitor(NumberVisitor<int &>(value), ppv->operator[](2));
104 [ + - - + ]: 2 : BOOST_REQUIRE_EQUAL(value, 3);
105 [ + - - + ]: 2 : BOOST_REQUIRE_EQUAL(get_number<int>(ppv->operator[](1)), 2);
106 : :
107 [ + - - + ]: 2 : BOOST_REQUIRE_EQUAL(get_number<int>(*properties, 0, "test", "number"), 12);
108 : :
109 : : property_vector *pv =
110 : 2 : properties->get_ptr<property_vector>("test", "property_vector");
111 : :
112 [ + - + - : 2 : BOOST_REQUIRE(pv != 0);
+ - - + ]
113 [ + - + - : 2 : BOOST_REQUIRE(pv->size() == 2);
+ - + - -
+ ]
114 [ + - + - : 2 : BOOST_REQUIRE(*pv->at(0).get_ptr<lua_Number>("value") == 1);
+ - + - +
- - + ]
115 [ + - + - : 2 : BOOST_REQUIRE(*pv->at(1).get_ptr<lua_Number>("value") == 2);
+ - + - +
- - + ]
116 : 2 : }
117 : :
118 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_to_properties1) {
+ - ]
119 : 2 : properties_ptr properties;
120 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(properties = load_properties(_state, TABLE_LUA));
+ - + - +
- + - + -
+ - + - +
- + - + -
- + # # #
# # # # #
# # # # #
# # # #
# ]
121 [ + - ]: 1 : test_to_properties(properties);
122 : 1 : }
123 : :
124 [ + - + - : 5 : BOOST_AUTO_TEST_CASE(test_to_properties2) {
+ - ]
125 : 1 : const char *code = TABLE_LUA_STR;
126 : 2 : properties_ptr properties;
127 [ + - + - : 1 : BOOST_REQUIRE_NO_THROW(properties =
+ - + - +
- + - + -
+ - + - +
- + - + -
- + # # #
# # # # #
# # # # #
# # # #
# ]
128 : : load_string_properties(_state, code));
129 [ + - ]: 1 : test_to_properties(properties);
130 : 1 : }
131 : :
132 [ + - + - : 4 : BOOST_AUTO_TEST_SUITE_END()
- + # # ]
|