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