Skip to content

Commit 6a400c7

Browse files
committed
Add source_location support
1 parent ac56443 commit 6a400c7

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

include/class_description.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Result ClassDescription<T>::run(Formatters::BaseFormatter &printer) {
229229
template <class T>
230230
ExpectationValue<T> ItCD<T>::is_expected() {
231231
auto cd = static_cast<ClassDescription<T> *>(this->get_parent());
232-
ExpectationValue<T> expectation(*this, cd->subject);
232+
ExpectationValue<T> expectation(*this, cd->subject, std::source_location::current());
233233
return expectation;
234234
}
235235

include/expectations/expectation.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ Result Expectation<A>::to_have_error(std::string msg) {
395395
template <typename A>
396396
class ExpectationValue : public Expectation<A> {
397397
A value;
398+
std::source_location location;
398399

399400
public:
400401
/**
@@ -404,7 +405,8 @@ class ExpectationValue : public Expectation<A> {
404405
*
405406
* @return The constructed ExpectationValue.
406407
*/
407-
ExpectationValue(ItBase &it, A value) : Expectation<A>(it), value(value) {}
408+
ExpectationValue(ItBase &it, A value, std::source_location location)
409+
: Expectation<A>(it), value(value), location{location} {}
408410

409411
/**
410412
* @brief Create an Expectation using an initializer list.
@@ -414,8 +416,8 @@ class ExpectationValue : public Expectation<A> {
414416
* @return The constructed Expectation.
415417
*/
416418
template <typename U>
417-
ExpectationValue(ItBase &it, std::initializer_list<U> init_list)
418-
: Expectation<A>(it), value(std::vector<U>(init_list)) {}
419+
ExpectationValue(ItBase &it, std::initializer_list<U> init_list, std::source_location location)
420+
: Expectation<A>(it), value(std::vector<U>(init_list)), location{location} {}
419421

420422
/** @brief Get the target of the expectation. */
421423
A &get_target() & override { return value; }
@@ -436,9 +438,10 @@ class ExpectationFunc : public Expectation<decltype(std::declval<F>()())> {
436438
using block_ret_t = decltype(std::declval<F>()());
437439
F block;
438440
std::shared_ptr<block_ret_t> computed = nullptr;
441+
std::source_location location;
439442

440443
public:
441-
ExpectationFunc(ExpectationFunc<F> const &copy) : Expectation<block_ret_t>(copy), block(copy.block) {}
444+
ExpectationFunc(ExpectationFunc<F> const &copy, std::source_location location) : Expectation<block_ret_t>(copy), block(copy.block), location{location} {}
442445

443446
/**
444447
* @brief Create an ExpectationValue using a value.
@@ -447,7 +450,7 @@ class ExpectationFunc : public Expectation<decltype(std::declval<F>()())> {
447450
*
448451
* @return The constructed ExpectationValue.
449452
*/
450-
ExpectationFunc(ItBase &it, F block) : Expectation<block_ret_t>(it), block(block) {}
453+
ExpectationFunc(ItBase &it, F block, std::source_location location) : Expectation<block_ret_t>(it), block(block), location{location} {}
451454

452455
/**
453456
* @brief Create an Expectation using a function.

include/it.hpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ItD : public ItBase {
4848
*
4949
* @return the constructed ItD object
5050
*/
51-
ItD(const Child &parent, const char* description, Block block)
51+
ItD(const Child &parent, const char *description, Block block)
5252
: ItBase(parent, description), block(std::move(block)) {}
5353

5454
/**
@@ -104,7 +104,7 @@ class ItCD : public ItBase {
104104
T &subject;
105105

106106
// This is only ever instantiated by ClassDescription<T>
107-
ItCD(const Child &parent, T &subject, const char* description, Block block)
107+
ItCD(const Child &parent, T &subject, const char *description, Block block)
108108
: ItBase(parent, description), block(block), subject(subject) {}
109109

110110
ItCD(const Child &parent, T &subject, Block block) : ItBase(parent), block(block), subject(subject) {}
@@ -123,18 +123,18 @@ class ItCD : public ItBase {
123123
* @endcode
124124
*/
125125
template <Util::is_not_functional T>
126-
ExpectationValue<T> ItBase::expect(T value) {
127-
return ExpectationValue<T>(*this, value);
126+
ExpectationValue<T> ItBase::expect(T value, std::source_location location) {
127+
return ExpectationValue<T>(*this, value, location);
128128
}
129129

130130
template <Util::is_functional T>
131-
ExpectationFunc<T> ItBase::expect(T block) {
132-
return ExpectationFunc<T>(*this, block);
131+
ExpectationFunc<T> ItBase::expect(T block, std::source_location location) {
132+
return ExpectationFunc<T>(*this, block, location);
133133
}
134134

135135
template <typename T>
136-
ExpectationValue<T> ItBase::expect(Let<T> &let) {
137-
return ExpectationValue<T>(*this, let.value());
136+
ExpectationValue<T> ItBase::expect(Let<T> &let, std::source_location location) {
137+
return ExpectationValue<T>(*this, let.value(), location);
138138
}
139139

140140
/**
@@ -145,12 +145,13 @@ ExpectationValue<T> ItBase::expect(Let<T> &let) {
145145
* @endcode
146146
*/
147147
template <typename T>
148-
ExpectationValue<std::initializer_list<T>> ItBase::expect(std::initializer_list<T> init_list) {
149-
return ExpectationValue<std::initializer_list<T>>(*this, init_list);
148+
ExpectationValue<std::initializer_list<T>> ItBase::expect(std::initializer_list<T> init_list,
149+
std::source_location location) {
150+
return ExpectationValue<std::initializer_list<T>>(*this, init_list, location);
150151
}
151152

152-
inline ExpectationValue<std::string> ItBase::expect(const char *str) {
153-
return ExpectationValue<std::string>(*this, std::string(str));
153+
inline ExpectationValue<std::string> ItBase::expect(const char *str, std::source_location location) {
154+
return ExpectationValue<std::string>(*this, std::string(str), location);
154155
}
155156

156157
} // namespace CppSpec

include/it_base.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** @file */
22
#pragma once
33

4+
#include <source_location>
45
#include <string>
56
#include <utility>
67
#include <vector>
@@ -84,7 +85,7 @@ class ItBase : public Runnable {
8485
* @return a ExpectationValue object containing the given value.
8586
*/
8687
template <Util::is_not_functional T>
87-
ExpectationValue<T> expect(T value);
88+
ExpectationValue<T> expect(T value, std::source_location location = std::source_location::current());
8889

8990
/**
9091
* @brief The `expect` object generator for lambdas
@@ -96,7 +97,7 @@ class ItBase : public Runnable {
9697
* @return a ExpectationFunc object containing the given value.
9798
*/
9899
template <Util::is_functional T>
99-
ExpectationFunc<T> expect(T block);
100+
ExpectationFunc<T> expect(T block, std::source_location location = std::source_location::current());
100101

101102
/**
102103
* @brief The `expect` object generator for initializer lists
@@ -108,7 +109,7 @@ class ItBase : public Runnable {
108109
* @return a ExpectationValue object containing the given init_list.
109110
*/
110111
template <typename T>
111-
ExpectationValue<std::initializer_list<T>> expect(std::initializer_list<T> init_list);
112+
ExpectationValue<std::initializer_list<T>> expect(std::initializer_list<T> init_list, std::source_location location = std::source_location::current());
112113

113114
/**
114115
* @brief The `expect` object generator for Let
@@ -120,15 +121,15 @@ class ItBase : public Runnable {
120121
* @return a ExpectationValue object containing the given value.
121122
*/
122123
template <typename T>
123-
ExpectationValue<T> expect(Let<T> &let);
124+
ExpectationValue<T> expect(Let<T> &let, std::source_location location = std::source_location::current());
124125

125126
/**
126127
* @brief The `expect` object generator for const char*
127128
*
128129
* @param string the string to wrap
129130
* @return a ExpectationValue object containing a C++ string
130131
*/
131-
ExpectationValue<std::string> expect(const char *string);
132+
ExpectationValue<std::string> expect(const char *string, std::source_location location = std::source_location::current());
132133
};
133134

134135
} // namespace CppSpec

spec/expectations/expectation_spec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe expectation_spec("Expectation", $ {
106106
// we explicitly want a function. Any other time
107107
// that would be perfectly okay.
108108
std::function<int()> foo = [] { return 1 + 2; };
109-
ExpectationFunc<decltype(foo)> expectation(self, foo);
109+
ExpectationFunc<decltype(foo)> expectation(self, foo, std::source_location::current());
110110
expect(expectation.get_target()).to_equal(3);
111111
});
112112
});

spec/matchers/be_within_spec.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <source_location>
12
#include "cppspec.hpp"
23

34
using namespace CppSpec;
@@ -38,7 +39,7 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
3839
it("passes with negative arguments", _ {
3940
expect(-1.0001).to_be_within(5).percent_of(-1);
4041
});
41-
42+
4243
it("works with std::time", _ {
4344
expect(std::time(nullptr)).to_be_within(0.1).of(std::time(nullptr));
4445
});
@@ -57,7 +58,7 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
5758

5859
it("provides a description", _ {
5960
auto d = 5.1;
60-
ExpectationValue<double> ex(self, d);
61+
ExpectationValue<double> ex(self, d, std::source_location::current());
6162
Matchers::BeWithin<double, double> matcher(ex, 0.5);
6263
matcher.of(5.0);
6364
expect(matcher.description()).to_equal("be within 0.5 of 5");
@@ -83,7 +84,7 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
8384

8485
it("provides a description", _ {
8586
auto d = 5.1;
86-
ExpectationValue<double> ex(self, d);
87+
ExpectationValue<double> ex(self, d, std::source_location::current());
8788
Matchers::BeWithin<double, double> matcher(ex, 0.5);
8889
matcher.percent_of(5.0);
8990
expect(matcher.description()).to_equal("be within 0.5% of 5");
@@ -136,4 +137,4 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
136137
});
137138

138139

139-
CPPSPEC_MAIN(be_within_spec);
140+
CPPSPEC_MAIN(be_within_spec);

0 commit comments

Comments
 (0)