OTest2
A C++ testing framework
testmarkprinter.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 
20 #include <testmarkprinter.h>
21 
22 #include <assert.h>
23 
24 #include <testmarkformatter.h>
25 #include <utils.h>
26 
27 namespace OTest2 {
28 
29 struct TestMarkPrinter::Impl {
30  const std::vector<TestMark::LinearizedRecord>* array;
31  struct StackRecord {
32  const TestMark::LinearizedRecord* mark;
33  void (TestMarkFormatter::* open)(const std::string&, const TestMark*, int);
34  void (TestMarkFormatter::* close)(const std::string&, const TestMark*, int);
35  int indent;
36  };
37  std::vector<StackRecord> stack;
38  int& index;
39 
40  explicit Impl(
41  const std::vector<TestMark::LinearizedRecord>* array_,
42  int& index_);
43 
44  /* -- avoid copying */
45  Impl(
46  const Impl&) = delete;
47  Impl& operator =(
48  const Impl&) = delete;
49 
50  void printTop(
51  TestMarkFormatter& formatter_,
52  bool open_);
53  bool handleItem(
54  TestMarkFormatter& formatter_,
55  void (TestMarkFormatter::* open_)(const std::string&, const TestMark*, int),
56  void (TestMarkFormatter::* close_)(const std::string&, const TestMark*, int));
57 };
58 
59 TestMarkPrinter::Impl::Impl(
60  const std::vector<TestMark::LinearizedRecord>* array_,
61  int& index_) :
62  array(array_),
63  stack(),
64  index(index_) {
65  assert(array != nullptr);
66  index = 0;
67 }
68 
69 void TestMarkPrinter::Impl::printTop(
70  TestMarkFormatter& formatter_,
71  bool open_) {
72  assert(!stack.empty());
73 
74  const auto& top_(stack.back());
75  if(top_.open != nullptr) {
76  if(open_)
77  (formatter_.*top_.open)(top_.mark->label, top_.mark->me, top_.indent);
78  else
79  (formatter_.*top_.close)(top_.mark->label, top_.mark->me, top_.indent);
80  }
81 }
82 
83 bool TestMarkPrinter::Impl::handleItem(
84  TestMarkFormatter& formatter_,
85  void (TestMarkFormatter::* open_)(const std::string&, const TestMark*, int),
86  void (TestMarkFormatter::* close_)(const std::string&, const TestMark*, int)) {
87  assert(array != nullptr);
88 
89  /* -- out of the end */
90  if(index >= array->size()) {
91  /* -- finish all opened nodes */
92  if(!stack.empty()) {
93  printTop(formatter_, false);
94  stack.pop_back();
95  return true;
96  }
97 
98  /* -- empty stack, empty input sequence => final end */
99  return false;
100  }
101 
102  auto& curr_(array->at(index));
103  if(index == 0) {
104  /* -- first item */
105  stack.push_back({&curr_, open_, close_, 0});
106  printTop(formatter_, true);
107  }
108  else {
109  assert(!stack.empty());
110 
111  /* -- level(s) up from the tree */
112  const auto* top_(&stack.back());
113  if(top_->mark->level + 1 != curr_.level) {
114  printTop(formatter_, false);
115  stack.pop_back();
116  assert(!stack.empty());
117  return true;
118  }
119 
120  /* -- level down into the tree */
121  stack.push_back({&curr_, open_, close_, top_->indent + 1});
122  printTop(formatter_, true);
123  }
124 
125  /* -- move to next one */
126  ++index;
127 
128  return true;
129 }
130 
132  const std::vector<TestMark::LinearizedRecord>* array_,
133  int& index_) :
134  pimpl(new Impl(array_, index_)) {
135 
136 }
137 
139  odelete(pimpl);
140 }
141 
143  TestMarkFormatter& formatter_) {
144  return pimpl->handleItem(
146 }
147 
149  TestMarkFormatter& formatter_) {
150  return pimpl->handleItem(
152 }
153 
155  TestMarkFormatter& formatter_) {
156  return pimpl->handleItem(
158 }
159 
161  TestMarkFormatter& formatter_) {
162  return pimpl->handleItem(formatter_, nullptr, nullptr);
163 }
164 
165 } /* namespace OTest2 */
OTest2::TestMarkPrinter::skipLine
bool skipLine(TestMarkFormatter &os_)
Skip current line.
Definition: testmarkprinter.cpp:160
OTest2::TestMarkFormatter::openAdded
virtual void openAdded(const std::string &label_, const TestMark *mark_, int indent_)=0
Print opening of an added testmark.
OTest2::TestMarkFormatter::openDeleted
virtual void openDeleted(const std::string &label_, const TestMark *mark_, int indent_)=0
Print opening of a deleted testmark.
testmarkformatter.h
utils.h
OTest2::TestMarkPrinter::~TestMarkPrinter
~TestMarkPrinter()
Dtor.
Definition: testmarkprinter.cpp:138
OTest2::TestMarkFormatter::closeDeleted
virtual void closeDeleted(const std::string &label_, const TestMark *mark_, int indent_)=0
Print closing of a deleted testmark.
OTest2
Definition: assertbean.h:25
OTest2::TestMarkPrinter::operator=
TestMarkPrinter & operator=(const TestMarkPrinter &)=delete
OTest2::TestMarkPrinter::TestMarkPrinter
TestMarkPrinter(const std::vector< TestMark::LinearizedRecord > *array_, int &index_)
Ctor.
Definition: testmarkprinter.cpp:131
OTest2::TestMarkFormatter::closeAdded
virtual void closeAdded(const std::string &label_, const TestMark *mark_, int indent_)=0
Print closing of an added testmark.
OTest2::TestMarkFormatter::openMark
virtual void openMark(const std::string &label_, const TestMark *mark_, int indent_)=0
Print opening of a testmark.
testmarkprinter.h
OTest2::TestMarkPrinter::printAdded
bool printAdded(TestMarkFormatter &formatter_)
Print added line.
Definition: testmarkprinter.cpp:154
OTest2::TestMarkPrinter::printLine
bool printLine(TestMarkFormatter &formatter_)
Print current line into a stream.
Definition: testmarkprinter.cpp:142
OTest2::TestMarkPrinter::printDeleted
bool printDeleted(TestMarkFormatter &formatter_)
Print deleted line.
Definition: testmarkprinter.cpp:148
OTest2::TestMarkFormatter::closeMark
virtual void closeMark(const std::string &label_, const TestMark *mark_, int indent_)=0
Print closing of a testmark.
OTest2::TestMarkFormatter
Generic interface of the testmark formatter.
Definition: testmarkformatter.h:32
OTest2::odelete
void odelete(T_ *&object_)
Delete a pointer and set it invalid.
Definition: utils.h:34