rippled
Loading...
Searching...
No Matches
beast_PropertyStream_test.cpp
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/unit_test.h>
21#include <xrpl/beast/utility/PropertyStream.h>
22
23namespace beast {
24
26{
27public:
29
30 void
33 std::string const& expected,
34 std::string const& expected_remainder)
35 {
36 try
37 {
38 std::string const peeled_name = Source::peel_name(&s);
39 BEAST_EXPECT(peeled_name == expected);
40 BEAST_EXPECT(s == expected_remainder);
41 }
42 catch (...)
43 {
44 fail("unhandled exception");
45 ;
46 }
47 }
48
49 void
52 std::string const& expected,
53 bool should_be_found)
54 {
55 try
56 {
57 bool const found(Source::peel_leading_slash(&s));
58 BEAST_EXPECT(found == should_be_found);
59 BEAST_EXPECT(s == expected);
60 }
61 catch (...)
62 {
63 fail("unhandled exception");
64 ;
65 }
66 }
67
68 void
71 std::string const& expected_remainder,
72 bool should_be_found)
73 {
74 try
75 {
76 bool const found(Source::peel_trailing_slashstar(&s));
77 BEAST_EXPECT(found == should_be_found);
78 BEAST_EXPECT(s == expected_remainder);
79 }
80 catch (...)
81 {
82 fail("unhandled exception");
83 ;
84 }
85 }
86
87 void
88 test_find_one(Source& root, Source* expected, std::string const& name)
89 {
90 try
91 {
92 Source* source(root.find_one(name));
93 BEAST_EXPECT(source == expected);
94 }
95 catch (...)
96 {
97 fail("unhandled exception");
98 ;
99 }
100 }
101
102 void
103 test_find_path(Source& root, std::string const& path, Source* expected)
104 {
105 try
106 {
107 Source* source(root.find_path(path));
108 BEAST_EXPECT(source == expected);
109 }
110 catch (...)
111 {
112 fail("unhandled exception");
113 ;
114 }
115 }
116
117 void
118 test_find_one_deep(Source& root, std::string const& name, Source* expected)
119 {
120 try
121 {
122 Source* source(root.find_one_deep(name));
123 BEAST_EXPECT(source == expected);
124 }
125 catch (...)
126 {
127 fail("unhandled exception");
128 ;
129 }
130 }
131
132 void
134 Source& root,
135 std::string path,
136 Source* expected,
137 bool expected_star)
138 {
139 try
140 {
141 auto const result(root.find(path));
142 BEAST_EXPECT(result.first == expected);
143 BEAST_EXPECT(result.second == expected_star);
144 }
145 catch (...)
146 {
147 fail("unhandled exception");
148 ;
149 }
150 }
151
152 void
153 run() override
154 {
155 Source a("a");
156 Source b("b");
157 Source c("c");
158 Source d("d");
159 Source e("e");
160 Source f("f");
161 Source g("g");
162
163 //
164 // a { b { d { f }, e }, c { g } }
165 //
166
167 a.add(b);
168 a.add(c);
169 c.add(g);
170 b.add(d);
171 b.add(e);
172 d.add(f);
173
174 testcase("peel_name");
175 test_peel_name("a", "a", "");
176 test_peel_name("foo/bar", "foo", "bar");
177 test_peel_name("foo/goo/bar", "foo", "goo/bar");
178 test_peel_name("", "", "");
179
180 testcase("peel_leading_slash");
181 test_peel_leading_slash("foo/", "foo/", false);
182 test_peel_leading_slash("foo", "foo", false);
183 test_peel_leading_slash("/foo/", "foo/", true);
184 test_peel_leading_slash("/foo", "foo", true);
185
186 testcase("peel_trailing_slashstar");
187 test_peel_trailing_slashstar("/foo/goo/*", "/foo/goo", true);
188 test_peel_trailing_slashstar("foo/goo/*", "foo/goo", true);
189 test_peel_trailing_slashstar("/foo/goo/", "/foo/goo", false);
190 test_peel_trailing_slashstar("foo/goo", "foo/goo", false);
191 test_peel_trailing_slashstar("", "", false);
192 test_peel_trailing_slashstar("/", "", false);
193 test_peel_trailing_slashstar("/*", "", true);
194 test_peel_trailing_slashstar("//", "/", false);
195 test_peel_trailing_slashstar("**", "*", true);
196 test_peel_trailing_slashstar("*/", "*", false);
197
198 testcase("find_one");
199 test_find_one(a, &b, "b");
200 test_find_one(a, nullptr, "d");
201 test_find_one(b, &e, "e");
202 test_find_one(d, &f, "f");
203
204 testcase("find_path");
205 test_find_path(a, "a", nullptr);
206 test_find_path(a, "e", nullptr);
207 test_find_path(a, "a/b", nullptr);
208 test_find_path(a, "a/b/e", nullptr);
209 test_find_path(a, "b/e/g", nullptr);
210 test_find_path(a, "b/e/f", nullptr);
211 test_find_path(a, "b", &b);
212 test_find_path(a, "b/e", &e);
213 test_find_path(a, "b/d/f", &f);
214
215 testcase("find_one_deep");
216 test_find_one_deep(a, "z", nullptr);
217 test_find_one_deep(a, "g", &g);
218 test_find_one_deep(a, "b", &b);
219 test_find_one_deep(a, "d", &d);
220 test_find_one_deep(a, "f", &f);
221
222 testcase("find");
223 test_find(a, "", &a, false);
224 test_find(a, "*", &a, true);
225 test_find(a, "/b", &b, false);
226 test_find(a, "b", &b, false);
227 test_find(a, "d", &d, false);
228 test_find(a, "/b*", &b, true);
229 test_find(a, "b*", &b, true);
230 test_find(a, "d*", &d, true);
231 test_find(a, "/b/*", &b, true);
232 test_find(a, "b/*", &b, true);
233 test_find(a, "d/*", &d, true);
234 test_find(a, "a", nullptr, false);
235 test_find(a, "/d", nullptr, false);
236 test_find(a, "/d*", nullptr, true);
237 test_find(a, "/d/*", nullptr, true);
238 }
239};
240
241BEAST_DEFINE_TESTSUITE(PropertyStream, beast, beast);
242} // 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:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:533