OTest2
A C++ testing framework
testmarkbuilder.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include <testmarkbuilder.h>
20 
21 #include <assert.h>
22 #include <vector>
23 
24 #include <testmarkbool.h>
25 #include <testmarkfloat.h>
26 #include <testmarkint.h>
27 #include <testmarklist.h>
28 #include <testmarkmap.h>
29 #include <testmarknull.h>
30 #include <testmarkstring.h>
31 #include <utils.h>
32 
33 namespace OTest2 {
34 
35 struct TestMarkBuilder::Impl {
36  TestMarkPtr root;
37  class RootContainer : public Container {
38  public:
39  TestMarkPtr* root;
40  explicit RootContainer(
41  TestMarkPtr* root_);
42  virtual void append(
43  const std::string& key_,
44  TestMarkPtr mark_);
45  virtual TestMark* getMark();
46  virtual TestMarkHashCode getHashCode() const;
47  };
48  struct Record {
49  std::string key;
50  std::unique_ptr<Container> container;
51 
52  explicit Record(
53  std::string key_,
54  std::unique_ptr<Container>&& container_);
55  };
56  typedef std::vector<Record> Stack;
57  Stack stack;
58 
59  void appendItem(
60  TestMarkPtr item_);
61  void appendItem(
62  TestMark* item_);
63 };
64 
65 TestMarkBuilder::Impl::RootContainer::RootContainer(
66  TestMarkPtr* root_) :
67  root(root_) {
68  assert(root != nullptr);
69 }
70 
71 void TestMarkBuilder::Impl::RootContainer::append(
72  const std::string& key_,
73  TestMarkPtr mark_) {
74  assert(key_.empty() && mark_ != nullptr && *root == nullptr);
75  *root = mark_;
76 }
77 
78 TestMark* TestMarkBuilder::Impl::RootContainer::getMark() {
79  assert(false);
80  return nullptr;
81 }
82 
83 TestMarkHashCode TestMarkBuilder::Impl::RootContainer::getHashCode() const {
84  return TestMarkHashCode();
85 }
86 
87 TestMarkBuilder::Impl::Record::Record(
88  std::string key_,
89  std::unique_ptr<Container>&& container_) :
90  key(std::move(key_)),
91  container(std::move(container_)) {
92 
93 }
94 
95 void TestMarkBuilder::Impl::appendItem(
96  TestMarkPtr item_) {
97  assert(item_ != nullptr);
98 
99  Record& record_(stack.back());
100  record_.container->append(record_.key, item_);
101  record_.key = "";
102 }
103 
104 void TestMarkBuilder::Impl::appendItem(
105  TestMark* item_) {
106  assert(item_ != nullptr);
107 
108  TestMarkPtr node_(item_);
109  appendItem(node_);
110 }
111 
113  pimpl(new Impl) {
114  pimpl->stack.emplace_back(
115  "",
116  ::OTest2::make_unique<Impl::RootContainer>(&pimpl->root));
117 }
118 
120  odelete(pimpl);
121 }
122 
124  const std::string& key_) {
125  assert(pimpl->stack.back().key.empty() && !key_.empty());
126  pimpl->stack.back().key = key_;
127 }
128 
130  TestMarkPtr mark_) {
131  pimpl->appendItem(mark_);
132 }
133 
135  pimpl->appendItem(new TestMarkNull);
136 }
137 
139  bool value_) {
140  pimpl->appendItem(new TestMarkBool(value_));
141 }
142 
144  int64_t value_) {
145  pimpl->appendItem(new TestMarkInt(value_));
146 }
147 
149  long double value_) {
150  pimpl->appendItem(new TestMarkFloat(value_));
151 }
152 
154  const std::string& value_) {
155  pimpl->appendItem(new TestMarkString(value_));
156 }
157 
158 void TestMarkBuilder::openContainerImpl(
159  std::unique_ptr<typename TestMarkBuilder::Container>&& container_) {
160  pimpl->stack.emplace_back("", std::move(container_));
161 }
162 
164  openContainer(::OTest2::make_unique<TestMarkList>());
165 }
166 
168  const std::string& prefix_) {
169  openContainer(::OTest2::make_unique<TestMarkList>(prefix_));
170 }
171 
173  openContainer(::OTest2::make_unique<TestMarkMap>());
174 }
175 
177  const std::string& prefix_) {
178  openContainer(::OTest2::make_unique<TestMarkMap>(prefix_));
179 }
180 
182  std::unique_ptr<TestMark> container_(pimpl->stack.back().container->getMark());
183  pimpl->stack.pop_back();
184  pimpl->appendItem(container_.release());
185 }
186 
188  assert(pimpl->stack.size() == 1);
189  assert(pimpl->root != nullptr);
190  TestMarkPtr retval(pimpl->root);
191  pimpl->root.reset();
192  return retval;
193 }
194 
195 } /* namespace OTest2 */
OTest2::TestMarkBool
Bool test mark.
Definition: testmarkbool.h:32
testmarkbuilder.h
testmarkmap.h
OTest2::TestMarkBuilder::~TestMarkBuilder
~TestMarkBuilder()
Dtor.
Definition: testmarkbuilder.cpp:119
testmarkstring.h
OTest2::TestMarkBuilder::appendFloat
void appendFloat(long double value_)
Append a float mark.
Definition: testmarkbuilder.cpp:148
testmarkint.h
OTest2::TestMarkHashCode
std::uint64_t TestMarkHashCode
Definition: testmarkhashcode.h:28
OTest2::TestMarkBuilder::closeContainer
void closeContainer()
Close currently opened container.
Definition: testmarkbuilder.cpp:181
OTest2::TestMarkString
String test mark.
Definition: testmarkstring.h:34
OTest2::TestMarkBuilder::stealMark
TestMarkPtr stealMark() const
Get constructed testmark and reset the builder.
Definition: testmarkbuilder.cpp:187
utils.h
OTest2::TestMarkBuilder::openMap
void openMap()
Open nested map.
Definition: testmarkbuilder.cpp:172
OTest2::TestMarkPtr
std::shared_ptr< TestMark > TestMarkPtr
Definition: testmarkptr.h:26
OTest2
Definition: assertbean.h:25
OTest2::TestMarkInt
Integer test mark.
Definition: testmarkint.h:34
OTest2::TestMarkBuilder::setKey
void setKey(const std::string &key_)
Set current key.
Definition: testmarkbuilder.cpp:123
testmarklist.h
testmarknull.h
OTest2::TestMarkFloat
Floating point test mark.
Definition: testmarkfloat.h:32
OTest2::TestMarkBuilder::appendNull
void appendNull()
Append the null mark.
Definition: testmarkbuilder.cpp:134
OTest2::TestMarkBuilder::TestMarkBuilder
TestMarkBuilder()
Ctor.
Definition: testmarkbuilder.cpp:112
OTest2::TestMarkBuilder::appendBool
void appendBool(bool value_)
Append a boolean mark.
Definition: testmarkbuilder.cpp:138
OTest2::TestMarkBuilder::appendMark
void appendMark(TestMarkPtr mark_)
Append new test mark.
Definition: testmarkbuilder.cpp:129
OTest2::TestMarkBuilder::appendString
void appendString(const std::string &value_)
Append a string mark.
Definition: testmarkbuilder.cpp:153
OTest2::TestMarkBuilder::appendInt
void appendInt(int64_t value_)
Append an integer mark.
Definition: testmarkbuilder.cpp:143
testmarkbool.h
OTest2::TestMarkBuilder::openContainer
void openContainer(std::unique_ptr< ContainerMark_ > &&container_)
Open a new nested level of test mark container.
Definition: testmarkbuilder.h:200
testmarkfloat.h
OTest2::TestMarkBuilder::openList
void openList()
Open new nested list.
Definition: testmarkbuilder.cpp:163
OTest2::TestMarkNull
Test mark node representing a null pointer.
Definition: testmarknull.h:32
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34