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