OTest2
A C++ testing framework
testmarkbuilder.h
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 
20 #ifndef OTest2__INCLUDE_OTEST2_TESTMARKBUILDER_H_
21 #define OTest2__INCLUDE_OTEST2_TESTMARKBUILDER_H_
22 
23 #include <assert.h>
24 #include <cstdint>
25 #include <memory>
26 #include <string>
27 
29 #include <otest2/testmarkptr.h>
30 #include <otest2/utils.h>
31 
32 namespace OTest2 {
33 
38  private:
39  struct Impl;
40  Impl* pimpl;
41 
42  public:
47 
52 
53  /* -- avoid copying */
55  const TestMarkBuilder&) = delete;
57  const TestMarkBuilder&) = delete;
58 
59  private:
60  class Container {
61  public:
62  virtual ~Container() = default;
63  virtual void append(
64  const std::string& key_,
65  TestMarkPtr mark_) = 0;
66  virtual TestMark* getMark() = 0;
67  virtual TestMarkHashCode getHashCode() const = 0;
68  };
69 
70  template<typename ContainerMark_>
71  class OrderedContainer : public Container {
72  private:
73  std::unique_ptr<ContainerMark_> container;
74 
75  public:
76  explicit OrderedContainer(
77  std::unique_ptr<ContainerMark_>&& container_) :
78  container(std::move(container_)) {
79 
80  }
81  virtual void append(
82  const std::string& key_,
83  TestMarkPtr mark_) {
84  assert(key_.empty() && mark_ != nullptr);
85  container->append(mark_);
86  }
87  virtual TestMark* getMark() {
88  return container.release();
89  }
90  virtual TestMarkHashCode getHashCode() const {
91  return container->getHashCode();
92  }
93 
94  };
95 
96  template<typename ContainerMark_>
97  class UnorderedContainer : public Container {
98  private:
99  std::unique_ptr<ContainerMark_> container;
100 
101  public:
102  explicit UnorderedContainer(
103  std::unique_ptr<ContainerMark_>&& container_) :
104  container(std::move(container_)) {
105 
106  }
107  virtual void append(
108  const std::string& key_,
109  TestMarkPtr mark_) {
110  assert(!key_.empty() && mark_ != nullptr);
111  container->append(key_, mark_);
112  }
113  virtual TestMark* getMark() {
114  return container.release();
115  }
116  virtual TestMarkHashCode getHashCode() const {
117  return container->getHashCode();
118  }
119  };
120 
121  template<typename ContainerMark_>
122  std::unique_ptr<Container> createContainer(
123  std::unique_ptr<ContainerMark_>&& container_,
124  void (ContainerMark_::*)(TestMarkPtr)) {
125  return ::OTest2::make_unique<OrderedContainer<ContainerMark_> >(
126  std::move(container_));
127  }
128 
129  template<typename ContainerMark_>
130  std::unique_ptr<Container> createContainer(
131  std::unique_ptr<ContainerMark_>&& container_,
132  void (ContainerMark_::*)(const std::string&, TestMarkPtr)) {
133  return ::OTest2::make_unique<UnorderedContainer<ContainerMark_> >(
134  std::move(container_));
135  }
136 
137  void openContainerImpl(
138  std::unique_ptr<Container>&& container_);
139 
140  public:
146  void setKey(
147  const std::string& key_);
148 
154  void appendMark(
155  TestMarkPtr mark_);
156 
160  void appendNull();
161 
167  void appendBool(
168  bool value_);
169 
175  void appendInt(
176  int64_t value_);
177 
183  void appendFloat(
184  long double value_);
185 
191  void appendString(
192  const std::string& value_);
193 
199  template<typename ContainerMark_>
201  std::unique_ptr<ContainerMark_>&& container_) {
202  assert(container_ != nullptr);
203  openContainerImpl(
204  createContainer(std::move(container_), &ContainerMark_::append));
205  }
206 
210  void openList();
211 
217  void openList(
218  const std::string& prefix_);
219 
223  void openMap();
224 
230  void openMap(
231  const std::string& prefix_);
232 
239  void closeContainer();
240 
246  TestMarkPtr stealMark() const;
247 };
248 
249 } /* namespace OTest2 */
250 
251 #endif /* OTest2__INCLUDE_OTEST2_TESTMARKBUILDER_H_ */
testmarkptr.h
OTest2::TestMarkBuilder::~TestMarkBuilder
~TestMarkBuilder()
Dtor.
Definition: testmarkbuilder.cpp:119
testmarkhashcode.h
OTest2::TestMarkBuilder::appendFloat
void appendFloat(long double value_)
Append a float mark.
Definition: testmarkbuilder.cpp:148
OTest2::TestMarkHashCode
std::uint64_t TestMarkHashCode
Definition: testmarkhashcode.h:28
OTest2::TestMarkBuilder::closeContainer
void closeContainer()
Close currently opened container.
Definition: testmarkbuilder.cpp:181
OTest2::TestMark::getHashCode
TestMarkHashCode getHashCode() const noexcept
Get testmarks' hash code.
Definition: testmark.cpp:83
OTest2::TestMarkBuilder::stealMark
TestMarkPtr stealMark() const
Get constructed testmark and reset the builder.
Definition: testmarkbuilder.cpp:187
utils.h
OTest2::TestMarkBuilder::operator=
TestMarkBuilder & operator=(const TestMarkBuilder &)=delete
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::TestMarkBuilder::setKey
void setKey(const std::string &key_)
Set current key.
Definition: testmarkbuilder.cpp:123
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
Builder of test marks.
Definition: testmarkbuilder.h:37
OTest2::TestMarkBuilder::appendInt
void appendInt(int64_t value_)
Append an integer mark.
Definition: testmarkbuilder.cpp:143
OTest2::TestMarkBuilder::openContainer
void openContainer(std::unique_ptr< ContainerMark_ > &&container_)
Open a new nested level of test mark container.
Definition: testmarkbuilder.h:200
OTest2::TestMark
Generic interface of a test mark node.
Definition: testmark.h:41
OTest2::TestMarkBuilder::openList
void openList()
Open new nested list.
Definition: testmarkbuilder.cpp:163