Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <keysetget.hpp>
10 :
11 : #include <tests.hpp>
12 :
13 20 : TEST (ks, get)
14 : {
15 12 : KeySet ks (5, *Key ("user/map", KEY_END), *Key ("user/map/a", KEY_END), *Key ("user/map/b", KEY_END),
16 14 : *Key ("user/map/c", KEY_VALUE, "value", KEY_END), KS_END);
17 16 : EXPECT_EQ (ks.get<std::string> ("user/map/c"), "value");
18 6 : EXPECT_THROW (ks.get<std::string> ("user/map/x"), KeyNotFoundException);
19 : typedef std::map<std::string, std::string> map;
20 12 : map m = ks.get<map> ("user/map");
21 8 : EXPECT_EQ (m.size (), 3);
22 12 : EXPECT_EQ (m["a"], "");
23 12 : EXPECT_EQ (m["b"], "");
24 12 : EXPECT_EQ (m["c"], "value");
25 8 : EXPECT_EQ (m.size (), 3) << "we inserted a value during test";
26 2 : }
27 :
28 : namespace kdb
29 : {
30 :
31 : template <>
32 : struct KeySetTypeWrapper<int>
33 : {
34 6 : int operator() (KeySet const & ks, std::string const & name, option_t const options) const
35 : {
36 12 : Key k = ks.lookup (name, options);
37 6 : if (!k) return -5;
38 4 : if (k.getStringSize () <= 1) return -3;
39 2 : return k.get<int> () + 5;
40 : }
41 : };
42 :
43 : class Point
44 : {
45 : public:
46 : int x, y;
47 :
48 2 : Point (int a, int b) : x (a), y (b)
49 : {
50 : }
51 : };
52 :
53 0 : bool operator== (Point const & our, Point const & other)
54 : {
55 2 : return our.x == other.x && our.y == other.y;
56 : }
57 :
58 0 : ostream & operator<< (ostream & os, Point const & p)
59 : {
60 0 : os << p.x << " " << p.y;
61 0 : return os;
62 : }
63 :
64 : template <>
65 : struct KeySetTypeWrapper<Point>
66 : {
67 2 : Point operator() (KeySet const & ks, std::string const & name, option_t const options) const
68 : {
69 8 : Key x = ks.lookup (name + "/x", options);
70 2 : if (!x) throw KeyNotFoundException (name + "/x not found");
71 8 : Key y = ks.lookup (name + "/y", options);
72 2 : if (!y) throw KeyNotFoundException (name + "/y not found");
73 :
74 4 : return Point (x.get<int> (), y.get<int> ());
75 : }
76 : };
77 : } // namespace kdb
78 :
79 20 : TEST (ks, getOwnType)
80 : {
81 12 : KeySet ks (5, *Key ("user/owntype", KEY_END), *Key ("user/owntype/a", KEY_END), *Key ("user/owntype/b", KEY_END),
82 8 : *Key ("user/owntype/c", KEY_VALUE, "20", KEY_END), *Key ("user/owntype/x", KEY_VALUE, "5", KEY_END),
83 18 : *Key ("user/owntype/y", KEY_VALUE, "12", KEY_END), KS_END);
84 14 : EXPECT_EQ (ks.get<int> ("user/owntype/a"), -3);
85 14 : EXPECT_EQ (ks.get<int> ("user/owntype/c"), 25);
86 14 : EXPECT_EQ (ks.get<int> ("user/owntype/n"), -5);
87 14 : EXPECT_EQ (ks.get<Point> ("user/owntype"), Point (5, 12));
88 2 : }
89 :
90 20 : TEST (ks, getCascading)
91 : {
92 12 : KeySet ks (5, *Key ("user/map", KEY_END), *Key ("user/map/a", KEY_END), *Key ("user/map/b", KEY_END),
93 8 : *Key ("user/map/c", KEY_VALUE, "winvalue", KEY_END), *Key ("user/map/d", KEY_VALUE, "dvalue", KEY_END),
94 12 : *Key ("system/map", KEY_END), *Key ("system/map/a", KEY_END), *Key ("system/map/b", KEY_END),
95 28 : *Key ("system/map/c", KEY_VALUE, "value", KEY_END), *Key ("system/map/e", KEY_VALUE, "evalue", KEY_END), KS_END);
96 16 : EXPECT_EQ (ks.get<std::string> ("/map/c"), "winvalue");
97 : typedef std::map<std::string, std::string> map;
98 12 : map m = ks.get<map> ("/map");
99 8 : EXPECT_EQ (m.size (), 5);
100 12 : EXPECT_EQ (m["a"], "");
101 12 : EXPECT_EQ (m["b"], "");
102 12 : EXPECT_EQ (m["c"], "winvalue");
103 12 : EXPECT_EQ (m["d"], "dvalue");
104 12 : EXPECT_EQ (m["e"], "evalue");
105 8 : EXPECT_EQ (m.size (), 5);
106 8 : }
|