Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for the key helper
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #include <gtest/gtest.h>
11 : #include <helper/keyhelper.hpp>
12 :
13 : using namespace kdb;
14 : using namespace kdb::tools::helper;
15 :
16 20 : TEST (RebasePath, RebasesCorrectlyWithValidArguments)
17 : {
18 4 : Key target = Key ("user/test/configold/subdir/k1", KEY_END);
19 4 : Key oldParent = Key ("user/test/configold", KEY_END);
20 4 : Key newParent = Key ("user/test/confignew", KEY_END);
21 :
22 8 : EXPECT_EQ ("user/test/confignew/subdir/k1", rebasePath (target, oldParent, newParent));
23 2 : }
24 :
25 20 : TEST (RebasePath, RebasesCorrectlyWithCascadingParent)
26 : {
27 4 : Key target = Key ("user/test/configold/subdir/k1", KEY_END);
28 4 : Key oldParent = Key ("/test/configold", KEY_END);
29 4 : Key newParent = Key ("user/test/confignew", KEY_END);
30 :
31 8 : EXPECT_EQ ("user/test/confignew/subdir/k1", rebasePath (target, oldParent, newParent));
32 2 : }
33 :
34 20 : TEST (RebasePath, WorksForKeyOnSameLevel)
35 : {
36 4 : Key target = Key ("user/test/configold", KEY_END);
37 4 : Key oldParent = Key ("user/test/configold", KEY_END);
38 4 : Key newParent = Key ("user/test/confignew", KEY_END);
39 :
40 8 : EXPECT_EQ ("user/test/confignew", rebasePath (target, oldParent, newParent));
41 2 : }
42 :
43 20 : TEST (RebasePath, ThrowsExceptionOnInvalidRebase)
44 : {
45 4 : Key target = Key ("user/test/k1", KEY_END);
46 4 : Key oldParent = Key ("user/test/configold", KEY_END);
47 4 : Key newParent = Key ("user/test/confignew", KEY_END);
48 :
49 4 : EXPECT_THROW (rebasePath (target, oldParent, newParent), InvalidRebaseException);
50 2 : }
51 :
52 20 : TEST (RebasePath, CalculatesPathCorrectlyWithCascadingTarget)
53 : {
54 4 : Key target = Key ("/test/k1", KEY_END);
55 4 : Key oldParent = Key ("spec/test", KEY_END);
56 4 : Key newParent = Key ("spec/test", KEY_END);
57 :
58 8 : EXPECT_EQ ("spec/test/k1", rebasePath (target, oldParent, newParent));
59 2 : }
60 :
61 20 : TEST (RebaseKey, RebasesCorrectlyWithValidArguments)
62 : {
63 4 : Key target = Key ("user/test/configold/subdir/k1", KEY_VALUE, "testvalue", KEY_END);
64 4 : Key oldParent = Key ("user/test/configold", KEY_END);
65 4 : Key newParent = Key ("user/test/confignew", KEY_END);
66 4 : Key expected = Key ("user/test/confignew/subdir/k1", KEY_VALUE, "testvalue", KEY_END);
67 :
68 4 : Key result = rebaseKey (target, oldParent, newParent);
69 :
70 10 : EXPECT_EQ (expected.getName (), result.getName ());
71 10 : EXPECT_EQ (expected.getString (), result.getString ());
72 2 : }
73 :
74 20 : TEST (RebaseKey, RebasesCorrectlyWithCascadingParent)
75 : {
76 4 : Key target = Key ("user/test/configold/subdir/k1", KEY_VALUE, "testvalue", KEY_END);
77 4 : Key oldParent = Key ("/test/configold", KEY_END);
78 4 : Key newParent = Key ("user/test/confignew", KEY_END);
79 4 : Key expected = Key ("user/test/confignew/subdir/k1", KEY_VALUE, "testvalue", KEY_END);
80 :
81 4 : Key result = rebaseKey (target, oldParent, newParent);
82 :
83 10 : EXPECT_EQ (expected.getName (), result.getName ());
84 10 : EXPECT_EQ (expected.getString (), result.getString ());
85 2 : }
86 :
87 20 : TEST (RebaseKey, ThrowsExceptionOnInvalidRebase)
88 : {
89 4 : Key target = Key ("user/test/k1", KEY_END);
90 4 : Key oldParent = Key ("user/test/configold", KEY_END);
91 4 : Key newParent = Key ("user/test/confignew", KEY_END);
92 :
93 4 : EXPECT_THROW (rebasePath (target, oldParent, newParent), InvalidRebaseException);
94 2 : }
95 :
96 20 : TEST (RebaseKey, CreatesCopy)
97 : {
98 4 : Key target = Key ("user/test/configold/subdir/k1", KEY_VALUE, "testvalue", KEY_END);
99 4 : Key oldParent = Key ("user/test/configold", KEY_END);
100 4 : Key newParent = Key ("user/test/confignew", KEY_END);
101 :
102 4 : Key result = rebaseKey (target, oldParent, newParent);
103 10 : target.setString ("newvalue");
104 8 : EXPECT_EQ ("testvalue", result.getString ());
105 2 : }
106 :
107 20 : TEST (RemoveNamespace, Basics)
108 : {
109 4 : Key key ("user/test/configold/subdir/k1", KEY_VALUE, "testvalue", KEY_END);
110 2 : removeNamespace (key);
111 8 : EXPECT_EQ (key, Key ("/test/configold/subdir/k1", KEY_END));
112 2 : }
113 :
114 20 : TEST (CommonKeyName, Key1)
115 : {
116 14 : EXPECT_EQ (commonKeyName (Key ("system/test/script/error/x", KEY_END), Key ("system/test/script/x", KEY_END)),
117 0 : Key ("system/test/script", KEY_END));
118 14 : EXPECT_EQ (commonKeyName (Key ("system/test/script//x", KEY_END), Key ("system/test/script/other//x", KEY_END)),
119 0 : Key ("system/test/script", KEY_END));
120 14 : EXPECT_EQ (commonKeyName (Key ("user/test/script//x", KEY_END), Key ("system/test/script/other//x", KEY_END)),
121 0 : Key ("/test/script", KEY_END));
122 14 : EXPECT_EQ (commonKeyName (Key ("/test/script//x", KEY_END), Key ("system/test/script/other//x", KEY_END)),
123 0 : Key ("/test/script", KEY_END));
124 14 : EXPECT_EQ (commonKeyName (Key ("/test/script//x", KEY_END), Key ("/test/script/other//x", KEY_END)), Key ("/test/script", KEY_END));
125 14 : EXPECT_EQ (commonKeyName (Key ("system/test/script//x", KEY_END), Key ("user/test/script/other//x", KEY_END)),
126 0 : Key ("/test/script", KEY_END));
127 14 : EXPECT_EQ (commonKeyName (Key ("user/test/script//x", KEY_END), Key ("user/test/script/other//x", KEY_END)),
128 0 : Key ("user/test/script", KEY_END));
129 8 : }
|