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