Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
VarSub.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "VarSub.hpp"
18
19#include <boost/test/unit_test.hpp>
20
21using namespace std;
22using namespace toolbox;
23
24namespace {
25
26template <typename FnT>
27string apply_copy(FnT fn, string s)
28{
29 fn(s);
30 return s;
31}
32
33string get_var(const string& name)
34{
35 string val;
36 if (name == "FOO") {
37 val = "101";
38 } else if (name == "BAR") {
39 val = "202";
40 } else if (name == "BAZ") {
41 val = "FOO";
42 } else if (name == "QUX") {
43 val = "BAR";
44 } else if (name == "FOOBAR") {
45 val = "${FOO}${BAR}";
46 }
47 return val;
48}
49
50} // namespace
51
53
55{
56 VarSub fn{get_var};
57
58 BOOST_CHECK_EQUAL(apply_copy(fn, "${FOO}"), "101");
59 BOOST_CHECK_EQUAL(apply_copy(fn, "${BAR}"), "202");
60 BOOST_CHECK_EQUAL(apply_copy(fn, "<${FOO}>"), "<101>");
61 BOOST_CHECK_EQUAL(apply_copy(fn, "<${FOO} ${BAR}>"), "<101 202>");
62}
63
65{
66 VarSub fn{get_var};
67
68 BOOST_CHECK(apply_copy(fn, "${}").empty());
69 BOOST_CHECK(apply_copy(fn, "${123}").empty());
70 BOOST_CHECK(apply_copy(fn, "${EMPTY}").empty());
71}
72
74{
75 VarSub fn{get_var};
76
77 BOOST_CHECK_EQUAL(apply_copy(fn, R"(\\)"), R"(\)");
78 BOOST_CHECK_EQUAL(apply_copy(fn, R"(\\\\)"), R"(\\)");
79 BOOST_CHECK_EQUAL(apply_copy(fn, R"(\>)"), R"(>)");
80 BOOST_CHECK_EQUAL(apply_copy(fn, R"(<\\)"), R"(<\)");
81 BOOST_CHECK_EQUAL(apply_copy(fn, R"(\\a\\b\\c)"), R"(\a\b\c)");
82 BOOST_CHECK_EQUAL(apply_copy(fn, R"(\a\b\c)"), R"(abc)");
83 BOOST_CHECK_EQUAL(apply_copy(fn, R"(\${FOO})"), R"(${FOO})");
84 BOOST_CHECK_EQUAL(apply_copy(fn, R"($\{FOO})"), R"(${FOO})");
85 BOOST_CHECK_EQUAL(apply_copy(fn, R"(${\FOO})"), R"(101)");
86 BOOST_CHECK_EQUAL(apply_copy(fn, R"(${FOO\})"), R"(${FOO})");
87 BOOST_CHECK_EQUAL(apply_copy(fn, R"(${FOO}\)"), R"(101\)");
88}
89
91{
92 VarSub fn{get_var};
93
94 BOOST_CHECK_EQUAL(apply_copy(fn, "$"), "$");
95 BOOST_CHECK_EQUAL(apply_copy(fn, "{"), "{");
96 BOOST_CHECK_EQUAL(apply_copy(fn, "}"), "}");
97 BOOST_CHECK_EQUAL(apply_copy(fn, "$FOO"), "$FOO");
98 BOOST_CHECK_EQUAL(apply_copy(fn, "{FOO"), "{FOO");
99 BOOST_CHECK_EQUAL(apply_copy(fn, "${FOO"), "${FOO");
100 BOOST_CHECK_EQUAL(apply_copy(fn, "FOO}"), "FOO}");
101 BOOST_CHECK_EQUAL(apply_copy(fn, "$${FOO}"), "$101");
102}
103
105{
106 VarSub fn{get_var};
107
108 BOOST_CHECK_EQUAL(apply_copy(fn, "${FOOBAR}"), "101202");
109 BOOST_CHECK_EQUAL(apply_copy(fn, "${${BAZ}}"), "101");
110 BOOST_CHECK_EQUAL(apply_copy(fn, "${${BAZ}BAR}"), "101202");
111 BOOST_CHECK_EQUAL(apply_copy(fn, "${FOO${QUX}}"), "101202");
112 BOOST_CHECK_EQUAL(apply_copy(fn, "${${BAZ}${QUX}}"), "101202");
113}
114
116{
117 VarSub fn{[](const string& name) {
118 string val;
119 if (name == "FOO") {
120 val = "${BAR}";
121 } else if (name == "BAR") {
122 val = "${BAZ}";
123 } else if (name == "BAZ") {
124 val = "${QUX}";
125 } else if (name == "QUX") {
126 val = "${FOO}";
127 }
128 return val;
129 }};
130
131 BOOST_CHECK(apply_copy(fn, "${FOO}").empty());
132 BOOST_CHECK(apply_copy(fn, "${FOO}${FOO}").empty());
133 BOOST_CHECK(apply_copy(fn, "${FOO${FOO}}").empty());
134 BOOST_CHECK_EQUAL(apply_copy(fn, "<${FOO}>"), "<>");
135 BOOST_CHECK_EQUAL(apply_copy(fn, "<${FOO${FOO}}>"), "<>");
136 BOOST_CHECK_EQUAL(apply_copy(fn, "<${FOO} ${FOO}>"), "< >");
137}
138
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE(VarSubBasicCase)
Definition VarSub.ut.cpp:54
Variable substitution.
Definition VarSub.hpp:32
STL namespace.
constexpr bool empty(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s)
Definition Struct.hpp:92
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))