rippled
Loading...
Searching...
No Matches
beast_PropertyStream_test.cpp
1#include <xrpl/beast/unit_test.h>
2#include <xrpl/beast/utility/PropertyStream.h>
3
4namespace beast {
5
7{
8public:
10
11 void
14 std::string const& expected,
15 std::string const& expected_remainder)
16 {
17 try
18 {
19 std::string const peeled_name = Source::peel_name(&s);
20 BEAST_EXPECT(peeled_name == expected);
21 BEAST_EXPECT(s == expected_remainder);
22 }
23 catch (...)
24 {
25 fail("unhandled exception");
26 ;
27 }
28 }
29
30 void
33 std::string const& expected,
34 bool should_be_found)
35 {
36 try
37 {
38 bool const found(Source::peel_leading_slash(&s));
39 BEAST_EXPECT(found == should_be_found);
40 BEAST_EXPECT(s == expected);
41 }
42 catch (...)
43 {
44 fail("unhandled exception");
45 ;
46 }
47 }
48
49 void
52 std::string const& expected_remainder,
53 bool should_be_found)
54 {
55 try
56 {
57 bool const found(Source::peel_trailing_slashstar(&s));
58 BEAST_EXPECT(found == should_be_found);
59 BEAST_EXPECT(s == expected_remainder);
60 }
61 catch (...)
62 {
63 fail("unhandled exception");
64 ;
65 }
66 }
67
68 void
69 test_find_one(Source& root, Source* expected, std::string const& name)
70 {
71 try
72 {
73 Source* source(root.find_one(name));
74 BEAST_EXPECT(source == expected);
75 }
76 catch (...)
77 {
78 fail("unhandled exception");
79 ;
80 }
81 }
82
83 void
84 test_find_path(Source& root, std::string const& path, Source* expected)
85 {
86 try
87 {
88 Source* source(root.find_path(path));
89 BEAST_EXPECT(source == expected);
90 }
91 catch (...)
92 {
93 fail("unhandled exception");
94 ;
95 }
96 }
97
98 void
99 test_find_one_deep(Source& root, std::string const& name, Source* expected)
100 {
101 try
102 {
103 Source* source(root.find_one_deep(name));
104 BEAST_EXPECT(source == expected);
105 }
106 catch (...)
107 {
108 fail("unhandled exception");
109 ;
110 }
111 }
112
113 void
115 Source& root,
116 std::string path,
117 Source* expected,
118 bool expected_star)
119 {
120 try
121 {
122 auto const result(root.find(path));
123 BEAST_EXPECT(result.first == expected);
124 BEAST_EXPECT(result.second == expected_star);
125 }
126 catch (...)
127 {
128 fail("unhandled exception");
129 ;
130 }
131 }
132
133 void
134 run() override
135 {
136 Source a("a");
137 Source b("b");
138 Source c("c");
139 Source d("d");
140 Source e("e");
141 Source f("f");
142 Source g("g");
143
144 //
145 // a { b { d { f }, e }, c { g } }
146 //
147
148 a.add(b);
149 a.add(c);
150 c.add(g);
151 b.add(d);
152 b.add(e);
153 d.add(f);
154
155 testcase("peel_name");
156 test_peel_name("a", "a", "");
157 test_peel_name("foo/bar", "foo", "bar");
158 test_peel_name("foo/goo/bar", "foo", "goo/bar");
159 test_peel_name("", "", "");
160
161 testcase("peel_leading_slash");
162 test_peel_leading_slash("foo/", "foo/", false);
163 test_peel_leading_slash("foo", "foo", false);
164 test_peel_leading_slash("/foo/", "foo/", true);
165 test_peel_leading_slash("/foo", "foo", true);
166
167 testcase("peel_trailing_slashstar");
168 test_peel_trailing_slashstar("/foo/goo/*", "/foo/goo", true);
169 test_peel_trailing_slashstar("foo/goo/*", "foo/goo", true);
170 test_peel_trailing_slashstar("/foo/goo/", "/foo/goo", false);
171 test_peel_trailing_slashstar("foo/goo", "foo/goo", false);
172 test_peel_trailing_slashstar("", "", false);
173 test_peel_trailing_slashstar("/", "", false);
174 test_peel_trailing_slashstar("/*", "", true);
175 test_peel_trailing_slashstar("//", "/", false);
176 test_peel_trailing_slashstar("**", "*", true);
177 test_peel_trailing_slashstar("*/", "*", false);
178
179 testcase("find_one");
180 test_find_one(a, &b, "b");
181 test_find_one(a, nullptr, "d");
182 test_find_one(b, &e, "e");
183 test_find_one(d, &f, "f");
184
185 testcase("find_path");
186 test_find_path(a, "a", nullptr);
187 test_find_path(a, "e", nullptr);
188 test_find_path(a, "a/b", nullptr);
189 test_find_path(a, "a/b/e", nullptr);
190 test_find_path(a, "b/e/g", nullptr);
191 test_find_path(a, "b/e/f", nullptr);
192 test_find_path(a, "b", &b);
193 test_find_path(a, "b/e", &e);
194 test_find_path(a, "b/d/f", &f);
195
196 testcase("find_one_deep");
197 test_find_one_deep(a, "z", nullptr);
198 test_find_one_deep(a, "g", &g);
199 test_find_one_deep(a, "b", &b);
200 test_find_one_deep(a, "d", &d);
201 test_find_one_deep(a, "f", &f);
202
203 testcase("find");
204 test_find(a, "", &a, false);
205 test_find(a, "*", &a, true);
206 test_find(a, "/b", &b, false);
207 test_find(a, "b", &b, false);
208 test_find(a, "d", &d, false);
209 test_find(a, "/b*", &b, true);
210 test_find(a, "b*", &b, true);
211 test_find(a, "d*", &d, true);
212 test_find(a, "/b/*", &b, true);
213 test_find(a, "b/*", &b, true);
214 test_find(a, "d/*", &d, true);
215 test_find(a, "a", nullptr, false);
216 test_find(a, "/d", nullptr, false);
217 test_find(a, "/d*", nullptr, true);
218 test_find(a, "/d/*", nullptr, true);
219 }
220};
221
222BEAST_DEFINE_TESTSUITE(PropertyStream, beast, beast);
223} // namespace beast
Subclasses can be called to write to a stream and have children.
static bool peel_leading_slash(std::string *path)
void add(Source &source)
Add a child source.
static std::string peel_name(std::string *path)
static bool peel_trailing_slashstar(std::string *path)
void run() override
Runs the suite.
void test_peel_leading_slash(std::string s, std::string const &expected, bool should_be_found)
void test_find_one(Source &root, Source *expected, std::string const &name)
void test_peel_trailing_slashstar(std::string s, std::string const &expected_remainder, bool should_be_found)
void test_peel_name(std::string s, std::string const &expected, std::string const &expected_remainder)
void test_find_path(Source &root, std::string const &path, Source *expected)
void test_find_one_deep(Source &root, std::string const &name, Source *expected)
void test_find(Source &root, std::string path, Source *expected, bool expected_star)
Abstract stream with RAII containers that produce a property tree.
A testsuite class.
Definition suite.h:52
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:152
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:530