Skip to content

Commit 6a331e3

Browse files
committed
More documentation and API cleaning
1 parent 3b331fa commit 6a331e3

File tree

3 files changed

+142
-31
lines changed

3 files changed

+142
-31
lines changed

include/child.hpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
/** @file */
1+
/**
2+
* Copyright 2016 Katherine Whitlock
3+
*
4+
* @file child.hpp
5+
* @brief Contains the base Child class.
6+
*
7+
* @author Katherine Whitlock (toroidalcode)
8+
*/
9+
210
#ifndef CPPSPEC_CHILD_HPP
311
#define CPPSPEC_CHILD_HPP
412
#pragma once
@@ -7,9 +15,9 @@
715
#define CPPSPEC_DEBUG false
816
#endif
917

10-
#include <string>
11-
#include <memory>
1218
#include <iostream>
19+
#include <memory>
20+
#include <string>
1321
#include <typeinfo>
1422

1523
namespace CppSpec {
@@ -46,10 +54,12 @@ class Child {
4654
// All instances of Child start out healthy.
4755
bool status = true;
4856

57+
// The global formatter when running the tests. Would typically point to one
58+
// of the globally instantiated formatters in formatters.hpp
4959
Formatters::BaseFormatter *formatter = nullptr;
5060

5161
public:
52-
// Default constructor/desctructor
62+
// Default constructor/destructor
5363
Child() = default;
5464
virtual ~Child() = default;
5565

@@ -62,7 +72,9 @@ class Child {
6272
Child &operator=(const Child &) = default;
6373

6474
// Custom constructors
65-
static Child of(const Child& parent) noexcept { return Child().set_parent(&parent); }
75+
static Child of(const Child &parent) noexcept {
76+
return Child().set_parent(&parent);
77+
}
6678

6779
/*--------- Parent helper functions -------------*/
6880

@@ -72,16 +84,19 @@ class Child {
7284

7385
// TODO: Look in to making these references instead of pointer returns
7486
/** @brief Get the Child's parent. */
75-
Child *get_parent() const noexcept { return parent; }
87+
Child *get_parent() const noexcept { return parent; }
7688

7789
template <class C>
7890
C get_parent_as() const noexcept {
7991
return static_cast<C>(get_parent());
8092
}
8193

8294
/** @brief Set the Child's parent */
83-
Child set_parent(Child *parent) noexcept { this->parent = parent; return *this; }
84-
Child& set_parent(const Child *parent) noexcept {
95+
Child set_parent(Child *parent) noexcept {
96+
this->parent = parent;
97+
return *this;
98+
}
99+
Child &set_parent(const Child *parent) noexcept {
85100
this->parent = const_cast<Child *>(parent);
86101
return *this;
87102
}

include/expectations/handler.hpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
/** @file */
1+
/**
2+
* Copyright 2016 Katherine Whitlock
3+
*
4+
* @file handler.hpp
5+
* @brief Contains the primary handlers for running Matchers
6+
*
7+
* @author Katherine Whitlock (toroidalcode)
8+
*/
9+
210
#ifndef CPPSPEC_EXPECTATIONS_HANDLER_HPP
311
#define CPPSPEC_EXPECTATIONS_HANDLER_HPP
412
#pragma once
@@ -8,26 +16,44 @@
816

917
namespace CppSpec {
1018

19+
/** @brief Handles "positive" expectations (i.e. non-negated) */
1120
struct PositiveExpectationHandler {
12-
template <typename A, class Matcher>
21+
template <class Matcher>
1322
static Result handle_matcher(Matcher &matcher);
1423
static std::string verb() { return "should"; }
1524
};
1625

26+
/** @brief Handles "negative" expectations (i.e. negated with '.not_() */
1727
struct NegativeExpectationHandler {
18-
template <typename A, class Matcher>
28+
template <class Matcher>
1929
static Result handle_matcher(Matcher &matcher);
2030
static std::string verb() { return "should not"; }
2131
};
2232

23-
template <typename A, class Matcher>
33+
/**
34+
* @brief runs a positive expectation
35+
*
36+
* @tparam Matcher the class of Matcher to use
37+
* @param matcher the matcher to use
38+
*
39+
* @return the Result of the expectation
40+
*/
41+
template <class Matcher>
2442
Result PositiveExpectationHandler::handle_matcher(Matcher &matcher) {
2543
// TODO: handle expectation failure here
2644
return !matcher.match() ? Result::failure_with(matcher.failure_message())
2745
: Result::success();
2846
}
2947

30-
template <typename A, class Matcher>
48+
/**
49+
* @brief runs a negative expectation
50+
*
51+
* @tparam Matcher the class of Matcher to use
52+
* @param matcher the matcher to use
53+
*
54+
* @return the Result of the expectation
55+
*/
56+
template <class Matcher>
3157
Result NegativeExpectationHandler::handle_matcher(Matcher &matcher) {
3258
// TODO: handle expectation failure here
3359
return !matcher.negated_match()

include/matchers/matcher_base.hpp

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,184 @@
1-
/** @file */
1+
/**
2+
* Copyright 2016 Katherine Whitlock
3+
*
4+
* @file matcher_base.hpp
5+
* @brief Contains the base class for all Matchers
6+
*
7+
* @author Katherine Whitlock (toroidalcode)
8+
*/
9+
210
#ifndef CPPSPEC_MATCHERS_MATCHER_BASE_HPP
311
#define CPPSPEC_MATCHERS_MATCHER_BASE_HPP
412
#pragma once
513

614
#include <string>
7-
#include "pretty_matchers.hpp"
815
#include "expectations/handler.hpp"
916
#include "it_base.hpp"
17+
#include "pretty_matchers.hpp"
1018

1119
namespace CppSpec {
1220

1321
template <class T>
14-
class Expectation;
22+
class Expectation; // Forward declaration of Expectation
1523

1624
namespace Matchers {
1725

26+
/**
27+
* @brief the base class for all Matcher classes and objects
28+
*
29+
* @tparam Actual the type of Actual 'thing' to match against
30+
* in the context of `expect(2)`, it would be `int`
31+
*
32+
* @tparam Expected the type of Expected 'thing' to match for
33+
* in the contet of `expect(2).to_equal(2.0)`,
34+
* it would be `float`
35+
*/
1836
template <typename Actual, typename Expected>
1937
class MatcherBase : public Runnable, public Pretty {
2038
std::string custom_failure_message = "";
2139

2240
protected:
23-
Expected expected;
41+
Expected expected; // The expected object contained by the matcher
42+
43+
// A reference to the Expectation (i.e. `expect(2)`)
2444
Expectation<Actual> &expectation;
2545

2646
public:
47+
// Copy constructor
2748
MatcherBase(MatcherBase<Actual, Expected> const &copy) = default;
2849

50+
// Constructor when matcher has no 'object' to match for
2951
explicit MatcherBase(Expectation<Actual> &expectation)
3052
: Runnable(*expectation.get_parent()), // We want the parent of the
3153
// matcher to be the `it` block,
3254
// not the
3355
// Expectation.
3456
expectation(expectation) {}
3557

58+
// Constructor when the matcher has an object to match for. This is the most
59+
// commonly used constructor
3660
MatcherBase(Expectation<Actual> &expectation, Expected expected)
3761
: Runnable(*expectation.get_parent()),
3862
expected(expected),
3963
expectation(expectation) {}
4064

41-
// TODO: match and negated match should return Result
42-
virtual bool match() = 0;
43-
virtual bool negated_match() { return !match(); }
65+
/*--------- Helper functions -------------*/
66+
4467
virtual std::string failure_message();
4568
virtual std::string failure_message_when_negated();
4669
virtual std::string description();
70+
71+
// Get the 'actual' object from the Expectation
4772
Actual &get_actual() { return expectation.get_target(); }
73+
74+
// Get the 'expected' object from the Matcher
4875
Expected &get_expected() { return expected; }
76+
77+
// Get the Expectation itself
4978
Expectation<Actual> &get_expectation() { return expectation; }
79+
80+
// Set the message to give on match failure
5081
virtual MatcherBase &set_message(std::string message);
82+
83+
/*--------- Primary functions -------------*/
84+
85+
// Run the matcher
5186
Result run(Formatters::BaseFormatter &printer) override;
87+
88+
// TODO: match and negated match should return Result
89+
virtual bool match() = 0;
90+
virtual bool negated_match() { return !match(); }
91+
92+
// typedef Expected
5293
typedef Expected expected_t;
5394
};
5495

96+
/**
97+
* @brief Set a custom failure message
98+
*
99+
* @param message the message to give
100+
* @return the modified Matcher
101+
*/
55102
template <typename A, typename E>
56103
MatcherBase<A, E> &MatcherBase<A, E>::set_message(std::string message) {
57104
this->custom_failure_message = message;
58105
return *this;
59106
}
60107

108+
/**
109+
* @brief Get message to give on match failure
110+
*
111+
* @return the message
112+
*/
61113
template <typename A, typename E>
62114
std::string MatcherBase<A, E>::failure_message() {
63115
if (not custom_failure_message.empty()) {
64116
return this->custom_failure_message;
65117
} else {
66118
std::stringstream ss;
67-
ss << "expected " << Pretty::to_word(get_actual()) << " to " << description();
119+
ss << "expected " << Pretty::to_word(get_actual()) << " to "
120+
<< description();
68121
return ss.str();
69122
}
70123
}
71124

125+
/**
126+
* @brief Get message to give on match failure when negated
127+
*
128+
* @return the message
129+
*/
72130
template <typename A, typename E>
73131
std::string MatcherBase<A, E>::failure_message_when_negated() {
74132
if (not custom_failure_message.empty()) {
75133
return this->custom_failure_message;
76134
} else {
77135
std::stringstream ss;
78-
ss << "expected " << Pretty::to_word(get_actual()) << " to not " << description();
136+
ss << "expected " << Pretty::to_word(get_actual()) << " to not "
137+
<< description();
79138
return ss.str();
80139
}
81140
}
82141

142+
/**
143+
* @brief Get the description of the Matcher
144+
*
145+
* @return the description
146+
*/
83147
template <typename A, typename E>
84148
std::string MatcherBase<A, E>::description() {
85149
std::stringstream ss;
86150
std::string pretty_expected = this->to_sentance(expected);
87-
// ss << "match " << this->name_to_sentance(Util::demangle(typeid(*this).name()))
88-
// << "(" << pretty_expected.substr(1, pretty_expected.length()) << ")";
151+
// ss << "match " <<
152+
// this->name_to_sentance(Util::demangle(typeid(*this).name()))
153+
// << "(" << pretty_expected.substr(1, pretty_expected.length()) << ")";
89154
ss << "match" << Pretty::to_sentance(expected);
90155
return ss.str();
91156
}
92157

158+
/**
159+
* @brief Run the Matcher object
160+
*
161+
* @param printer the formatter to print using
162+
*
163+
* @return the Result of running the Matcher
164+
*/
93165
template <typename A, typename E>
94166
Result MatcherBase<A, E>::run(Formatters::BaseFormatter &printer) {
95167
ItBase *par = static_cast<ItBase *>(this->get_parent());
96168
// If we need a description for our test, generate it
97169
// unless we're ignoring the output.
98170
if (par->needs_description() && !expectation.get_ignore_failure()) {
99171
std::stringstream ss;
100-
ss << (expectation.get_sign()
101-
? PositiveExpectationHandler::verb()
102-
: NegativeExpectationHandler::verb())
172+
ss << (expectation.get_sign() ? PositiveExpectationHandler::verb()
173+
: NegativeExpectationHandler::verb())
103174
<< " " << this->description();
104175
std::string ss_str = ss.str();
105176
par->set_description(ss_str);
106177
}
107178

108-
Result matched =
109-
expectation.get_sign()
110-
? PositiveExpectationHandler::handle_matcher<A>(*this)
111-
: NegativeExpectationHandler::handle_matcher<A>(*this);
179+
Result matched = expectation.get_sign()
180+
? PositiveExpectationHandler::handle_matcher(*this)
181+
: NegativeExpectationHandler::handle_matcher(*this);
112182

113183
// If our items didn't match, we obviously failed.
114184
// Only report the failure if we aren't actively ignoring it.

0 commit comments

Comments
 (0)