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/utility/PropertiesToString.h>
17 : : #include <ssrc/wispers/utility/BindProperties.h>
18 : :
19 : : #include <boost/variant/get.hpp>
20 : :
21 : : #define BOOST_TEST_MODULE BindPropertiesTest
22 : : #include <boost/test/unit_test.hpp>
23 : :
24 : : using namespace NS_SSRC_WSPR_UTILITY;
25 : :
26 : 4 : WISP_STRUCT(IntPair, ((int, first))((int, second)));
27 : 2 : WISP_STRUCT(NestedPair, ((IntPair, first))((IntPair, second)));
28 : :
29 : : typedef std::vector<IntPair> IntPairV;
30 : :
31 : : WSPR_IS_BINDABLE_SEQUENCE(IntPairV)
32 : :
33 : : typedef std::vector<IntPairV> IntPairVV;
34 : :
35 : : WSPR_IS_BINDABLE_SEQUENCE(IntPairVV)
36 : :
37 : : WSPR_IS_PRIMITIVE_BINDABLE_SEQUENCE(std::vector<int>)
38 : :
39 : : typedef std::vector<std::vector<int> > IntVV;
40 : :
41 : : WSPR_IS_BINDABLE_SEQUENCE(IntVV)
42 : :
43 [ # # # # : 0 : WISP_STRUCT(FooStruct,
# # ]
44 : : ((IntPair, first))
45 : : ((IntPairV, second))
46 : : ((std::vector<int>, intv))
47 : : ((IntVV, third))
48 [ # # # # : 0 : ((IntPairVV, fourth)));
# # ]
49 : :
50 [ + - + - : 3 : BOOST_AUTO_TEST_CASE(test_bind_properties) {
- + # # ]
51 : 2 : Properties props;
52 : 1 : NestedPair pair(IntPair(1, 2), IntPair(3, 4));
53 : :
54 [ + - + - ]: 2 : const string expected("{[\"first\"]={[\"first\"]=1,[\"second\"]=2},[\"second\"]={[\"first\"]=3,[\"second\"]=4}}");
55 [ + - ]: 1 : pair.visit(BindProperties(props));
56 : :
57 [ + - + - : 1 : BOOST_CHECK_EQUAL(expected, to_string(props));
+ - + - +
- + - + -
- + ]
58 : 1 : }
59 : :
60 : 0 : void test_bindable_sequence() {
61 : 0 : Properties props;
62 [ # # # # ]: 0 : FooStruct foo;
63 : :
64 : 0 : foo.first.first = 1;
65 : 0 : foo.first.second = 2;
66 [ # # ]: 0 : foo.second.push_back(IntPair(3, 4));
67 [ # # ]: 0 : foo.second.push_back(IntPair(5, 6));
68 : :
69 [ # # ]: 0 : foo.intv.push_back(13);
70 [ # # ]: 0 : foo.intv.push_back(14);
71 : :
72 [ # # ]: 0 : foo.third.resize(2);
73 [ # # # # ]: 0 : foo.third[0].push_back(11);
74 [ # # # # ]: 0 : foo.third[1].push_back(12);
75 : :
76 [ # # ]: 0 : foo.fourth.resize(2);
77 [ # # # # ]: 0 : foo.fourth[0].push_back(IntPair(7, 8));
78 [ # # # # ]: 0 : foo.fourth[1].push_back(IntPair(9, 10));
79 : :
80 [ # # ]: 0 : foo.visit(BindProperties(props));
81 : :
82 : : // This stuff is just a sanity check in case to_string doesn't print
83 : : // nested sequences properly.
84 [ # # # # : 0 : BOOST_CHECK(props.get_ptr<property_vector>("third") != 0);
# # # # #
# # # # #
# # # # ]
85 [ # # # # : 0 : BOOST_CHECK(props.get_ptr<property_vector>("third")->size() == 2);
# # # # #
# # # # #
# # # # #
# ]
86 [ # # # # : 0 : BOOST_CHECK(props.get_ptr<property_vector>("third")->at(0).get_ptr<primitive_property_vector>() != 0);
# # # # #
# # # # #
# # # # #
# # # ]
87 [ # # # # : 0 : BOOST_CHECK(props.get_ptr<property_vector>("third")->at(0).get_ptr<primitive_property_vector>()->size() == 1);
# # # # #
# # # # #
# # # # #
# # # #
# ]
88 [ # # # # : 0 : BOOST_CHECK(boost::get<int>(props.get_ptr<property_vector>("third")->at(0).get_ptr<primitive_property_vector>()->at(0)) == 11);
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
89 : :
90 : : const string expected("{[\"first\"]={[\"first\"]=1,[\"second\"]=2},"
91 : : "[\"fourth\"]={{{[\"first\"]=7,[\"second\"]=8}},"
92 : : "{{[\"first\"]=9,[\"second\"]=10}}},"
93 : : "[\"intv\"]={13,14},"
94 : : "[\"second\"]={{[\"first\"]=3,[\"second\"]=4},{[\"first\"]=5,[\"second\"]=6}},"
95 [ # # # # ]: 0 : "[\"third\"]={{11},{12}}}");
96 : :
97 [ # # # # : 0 : BOOST_CHECK_EQUAL(to_string(props), expected);
# # # # #
# # # # #
# # ]
98 [ + - + - ]: 3 : }
|