xref: /freebsd/contrib/kyua/utils/config/keys_test.cpp (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 // Copyright 2012 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #include "utils/config/keys.hpp"
30 
31 #include <atf-c++.hpp>
32 
33 #include "utils/config/exceptions.hpp"
34 
35 namespace config = utils::config;
36 
37 
38 ATF_TEST_CASE_WITHOUT_HEAD(flatten_key__one);
39 ATF_TEST_CASE_BODY(flatten_key__one)
40 {
41     config::detail::tree_key key;
42     key.push_back("foo");
43     ATF_REQUIRE_EQ("foo", config::detail::flatten_key(key));
44 }
45 
46 
47 ATF_TEST_CASE_WITHOUT_HEAD(flatten_key__many);
48 ATF_TEST_CASE_BODY(flatten_key__many)
49 {
50     config::detail::tree_key key;
51     key.push_back("foo");
52     key.push_back("1");
53     key.push_back("bar");
54     ATF_REQUIRE_EQ("foo.1.bar", config::detail::flatten_key(key));
55 }
56 
57 
58 ATF_TEST_CASE_WITHOUT_HEAD(parse_key__one);
59 ATF_TEST_CASE_BODY(parse_key__one)
60 {
61     config::detail::tree_key exp_key;
62     exp_key.push_back("one");
63     ATF_REQUIRE(exp_key == config::detail::parse_key("one"));
64 }
65 
66 
67 ATF_TEST_CASE_WITHOUT_HEAD(parse_key__many);
68 ATF_TEST_CASE_BODY(parse_key__many)
69 {
70     config::detail::tree_key exp_key;
71     exp_key.push_back("one");
72     exp_key.push_back("2");
73     exp_key.push_back("foo");
74     ATF_REQUIRE(exp_key == config::detail::parse_key("one.2.foo"));
75 }
76 
77 
78 ATF_TEST_CASE_WITHOUT_HEAD(parse_key__empty_key);
79 ATF_TEST_CASE_BODY(parse_key__empty_key)
80 {
81     ATF_REQUIRE_THROW_RE(config::invalid_key_error,
82                          "Empty key",
83                          config::detail::parse_key(""));
84 }
85 
86 
87 ATF_TEST_CASE_WITHOUT_HEAD(parse_key__empty_component);
88 ATF_TEST_CASE_BODY(parse_key__empty_component)
89 {
90     ATF_REQUIRE_THROW_RE(config::invalid_key_error,
91                          "Empty component in key '.'",
92                          config::detail::parse_key("."));
93     ATF_REQUIRE_THROW_RE(config::invalid_key_error,
94                          "Empty component in key 'a.'",
95                          config::detail::parse_key("a."));
96     ATF_REQUIRE_THROW_RE(config::invalid_key_error,
97                          "Empty component in key '.b'",
98                          config::detail::parse_key(".b"));
99     ATF_REQUIRE_THROW_RE(config::invalid_key_error,
100                          "Empty component in key 'a..b'",
101                          config::detail::parse_key("a..b"));
102 }
103 
104 
105 ATF_INIT_TEST_CASES(tcs)
106 {
107     ATF_ADD_TEST_CASE(tcs, flatten_key__one);
108     ATF_ADD_TEST_CASE(tcs, flatten_key__many);
109 
110     ATF_ADD_TEST_CASE(tcs, parse_key__one);
111     ATF_ADD_TEST_CASE(tcs, parse_key__many);
112     ATF_ADD_TEST_CASE(tcs, parse_key__empty_key);
113     ATF_ADD_TEST_CASE(tcs, parse_key__empty_component);
114 }
115