OTest2
A C++ testing framework
regressions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 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 <regressions.h>
21 
22 #include <assert.h>
23 #include <iostream>
24 #include <string>
25 
26 #include <assertstream.h>
27 #include <context.h>
28 #include <difflogblock.h>
29 #include <objectpath.h>
30 #include <testmark.h>
31 #include <testmarkdiffprinter.h>
33 #include <testmarkptr.h>
34 #include <testmarkstorage.h>
35 
36 namespace OTest2 {
37 
38 bool RegressionAssertion::compareObjectMark(
39  const std::string& key_,
40  TestMarkPtr test_mark_,
41  bool print_) {
42  assert(test_mark_ != nullptr);
43 
44  const Context& context_(otest2Context());
45 
46  /* -- get test mark from the storage */
47  const std::string full_key_(context_.object_path->getRegressionKey(key_));
48  TestMarkPtr stored_(context_.test_mark_storage->getTestMark(full_key_));
49 
50  /* -- compare the marks */
51  bool equal_(false);
52  if(stored_ != nullptr)
53  equal_ = test_mark_->isEqual(*stored_);
54 
55  /* -- report the assertion */
56  AssertStream report_(enterAssertion(equal_));
57  if(equal_)
58  report_ << "regression check '" << full_key_ << "' has passed" << commitMsg();
59  else
60  report_ << "regression check '" << full_key_ << "' has failed" << commitMsg();
61 
62  /* -- report the difference if the check fails */
63  if(!equal_) {
64  TestMarkFormatterAssert formatter_(&report_, "");
65 
66  /* -- print current test mark */
67  report_ << "--------- Current ---------" << commitMsg();
68  test_mark_->printMark(formatter_);
69  report_ << commitMsg();
70 
71  report_ << "--------- Original ---------" << commitMsg();
72  if(stored_ != nullptr) {
73  /* -- print the original mark */
74  stored_->printMark(formatter_);
75  report_ << commitMsg();
76  }
77 
78  report_ << "--------- Difference ---------" << commitMsg();
79  if(stored_ != nullptr) {
80  /* -- compute the difference */
81  std::vector<TestMark::LinearizedRecord> array1_;
82  std::vector<TestMark::LinearizedRecord> array2_;
83  DiffLogBlocks diff_log_;
84  DiffLogBuilderBlock log_builder_(&diff_log_);
85  test_mark_->computeDiff(*stored_, array1_, array2_, log_builder_);
86 
87  /* -- print the difference */
88  printTestMarkDiff(formatter_, array1_, array2_, diff_log_, 3);
89  report_ << commitMsg();
90  }
91  else {
92  /* -- the new mark is one big addition */
93  test_mark_->printAddMark(formatter_);
94  report_ << commitMsg();
95  }
96  }
97  else if(print_) {
98  /* -- print current test mark */
99  std::cout << "test mark key: " << full_key_ << std::endl;
100  TestMarkFormatterAssert formatter_(&report_, "");
101  test_mark_->printMark(formatter_);
102  }
103 
104  return report_.getResult();
105 }
106 
107 bool RegressionAssertion::storeObjectMark(
108  const std::string& key_,
109  TestMarkPtr test_mark_) {
110  assert(test_mark_ != nullptr);
111 
112  const Context& context_(otest2Context());
113 
114  /* -- store the mark */
115  const std::string full_key_(context_.object_path->getRegressionKey(key_));
116  context_.test_mark_storage->setTestMark(full_key_, test_mark_);
117 
118  /* -- report the assertion */
119  AssertStream report_(enterAssertion(false));
120  report_ << "stored regression mark '" << full_key_ << "'" << commitMsg();
121  return report_.getResult();
122 }
123 
124 } /* -- namespace OTest2 */
testmarkptr.h
OTest2::printTestMarkDiff
void printTestMarkDiff(TestMarkFormatter &formatter_, const std::vector< TestMark::LinearizedRecord > &left_, const std::vector< TestMark::LinearizedRecord > &right_, const DiffLogBlocks &diff_log_, int context_)
Print difference of two testmarks.
Definition: testmarkdiffprinter.cpp:28
assertstream.h
testmarkdiffprinter.h
regressions.h
OTest2::DiffLogBlocks
std::vector< DiffBlock > DiffLogBlocks
Ordered (indexes into the sequences) list of diff changes.
Definition: difflogblock.h:42
OTest2::AssertContext::enterAssertion
AssertStream enterAssertion(bool result_)
Enter an assertion.
Definition: assertcontext.cpp:45
testmarkstorage.h
OTest2::commitMsg
Private::Manipulator commitMsg()
Commit current assertion message.
Definition: assertstream.cpp:201
OTest2::TestMarkPtr
std::shared_ptr< TestMark > TestMarkPtr
Definition: testmarkptr.h:26
objectpath.h
testmarkformatterassert.h
OTest2
Definition: assertbean.h:25
testmark.h
context.h
difflogblock.h
OTest2::AssertContext::otest2Context
const Context & otest2Context() const
Get the OTest2 context.
Definition: assertcontext.cpp:78