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
31 test_peel_leading_slash(std::string s, std::string const& expected, bool should_be_found)
32 {
33 try
34 {
35 bool const found(Source::peel_leading_slash(&s));
36 BEAST_EXPECT(found == should_be_found);
37 BEAST_EXPECT(s == expected);
38 }
39 catch (...)
40 {
41 fail("unhandled exception");
42 ;
43 }
44 }
45
46 void
49 std::string const& expected_remainder,
50 bool should_be_found)
51 {
52 try
53 {
54 bool const found(Source::peel_trailing_slashstar(&s));
55 BEAST_EXPECT(found == should_be_found);
56 BEAST_EXPECT(s == expected_remainder);
57 }
58 catch (...)
59 {
60 fail("unhandled exception");
61 ;
62 }
63 }
64
65 void
66 test_find_one(Source& root, Source* expected, std::string const& name)
67 {
68 try
69 {
70 Source const* source(root.find_one(name));
71 BEAST_EXPECT(source == expected);
72 }
73 catch (...)
74 {
75 fail("unhandled exception");
76 ;
77 }
78 }
79
80 void
81 test_find_path(Source& root, std::string const& path, Source* expected)
82 {
83 try
84 {
85 Source const* source(root.find_path(path));
86 BEAST_EXPECT(source == expected);
87 }
88 catch (...)
89 {
90 fail("unhandled exception");
91 ;
92 }
93 }
94
95 void
96 test_find_one_deep(Source& root, std::string const& name, Source* expected)
97 {
98 try
99 {
100 Source const* source(root.find_one_deep(name));
101 BEAST_EXPECT(source == expected);
102 }
103 catch (...)
104 {
105 fail("unhandled exception");
106 ;
107 }
108 }
109
110 void
111 test_find(Source& root, std::string path, Source* expected, bool expected_star)
112 {
113 try
114 {
115 auto const result(root.find(path));
116 BEAST_EXPECT(result.first == expected);
117 BEAST_EXPECT(result.second == expected_star);
118 }
119 catch (...)
120 {
121 fail("unhandled exception");
122 ;
123 }
124 }
125
126 void
127 run() override
128 {
129 Source a("a");
130 Source b("b");
131 Source c("c");
132 Source d("d");
133 Source e("e");
134 Source f("f");
135 Source g("g");
136
137 //
138 // a { b { d { f }, e }, c { g } }
139 //
140
141 a.add(b);
142 a.add(c);
143 c.add(g);
144 b.add(d);
145 b.add(e);
146 d.add(f);
147
148 testcase("peel_name");
149 test_peel_name("a", "a", "");
150 test_peel_name("foo/bar", "foo", "bar");
151 test_peel_name("foo/goo/bar", "foo", "goo/bar");
152 test_peel_name("", "", "");
153
154 testcase("peel_leading_slash");
155 test_peel_leading_slash("foo/", "foo/", false);
156 test_peel_leading_slash("foo", "foo", false);
157 test_peel_leading_slash("/foo/", "foo/", true);
158 test_peel_leading_slash("/foo", "foo", true);
159
160 testcase("peel_trailing_slashstar");
161 test_peel_trailing_slashstar("/foo/goo/*", "/foo/goo", true);
162 test_peel_trailing_slashstar("foo/goo/*", "foo/goo", true);
163 test_peel_trailing_slashstar("/foo/goo/", "/foo/goo", false);
164 test_peel_trailing_slashstar("foo/goo", "foo/goo", false);
165 test_peel_trailing_slashstar("", "", false);
166 test_peel_trailing_slashstar("/", "", false);
167 test_peel_trailing_slashstar("/*", "", true);
168 test_peel_trailing_slashstar("//", "/", false);
169 test_peel_trailing_slashstar("**", "*", true);
170 test_peel_trailing_slashstar("*/", "*", false);
171
172 testcase("find_one");
173 test_find_one(a, &b, "b");
174 test_find_one(a, nullptr, "d");
175 test_find_one(b, &e, "e");
176 test_find_one(d, &f, "f");
177
178 testcase("find_path");
179 test_find_path(a, "a", nullptr);
180 test_find_path(a, "e", nullptr);
181 test_find_path(a, "a/b", nullptr);
182 test_find_path(a, "a/b/e", nullptr);
183 test_find_path(a, "b/e/g", nullptr);
184 test_find_path(a, "b/e/f", nullptr);
185 test_find_path(a, "b", &b);
186 test_find_path(a, "b/e", &e);
187 test_find_path(a, "b/d/f", &f);
188
189 testcase("find_one_deep");
190 test_find_one_deep(a, "z", nullptr);
191 test_find_one_deep(a, "g", &g);
192 test_find_one_deep(a, "b", &b);
193 test_find_one_deep(a, "d", &d);
194 test_find_one_deep(a, "f", &f);
195
196 testcase("find");
197 test_find(a, "", &a, false);
198 test_find(a, "*", &a, true);
199 test_find(a, "/b", &b, false);
200 test_find(a, "b", &b, false);
201 test_find(a, "d", &d, false);
202 test_find(a, "/b*", &b, true);
203 test_find(a, "b*", &b, true);
204 test_find(a, "d*", &d, true);
205 test_find(a, "/b/*", &b, true);
206 test_find(a, "b/*", &b, true);
207 test_find(a, "d/*", &d, true);
208 test_find(a, "a", nullptr, false);
209 test_find(a, "/d", nullptr, false);
210 test_find(a, "/d*", nullptr, true);
211 test_find(a, "/d/*", nullptr, true);
212 }
213};
214
215BEAST_DEFINE_TESTSUITE(PropertyStream, beast, beast);
216} // 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:51
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:150
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:519