Skip to content

Commit

Permalink
test: move HasReason so it can be reused
Browse files Browse the repository at this point in the history
Summary:
```
Move the class `HasReason` from `miner_tests.cpp` to
`setup_common.h` so that it can be reused by other tests.
```

Partial backport (2/4) of core [[bitcoin/bitcoin#19485 | PR19485]]:
bitcoin/bitcoin@fe42411

Depends on D9186.

Test Plan:
  ninja check-bitcoin-miner_tests

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D9187
  • Loading branch information
vasild authored and Fabcien committed Feb 9, 2021
1 parent 7af7f84 commit 4adf771
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
12 changes: 0 additions & 12 deletions src/test/miner_tests.cpp
Expand Up @@ -45,18 +45,6 @@ struct MinerTestingSetup : public TestingSetup {

BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)

// BOOST_CHECK_EXCEPTION predicates to check the specific validation error
class HasReason {
public:
explicit HasReason(const std::string &reason) : m_reason(reason) {}
bool operator()(const std::runtime_error &e) const {
return std::string(e.what()).find(m_reason) != std::string::npos;
};

private:
const std::string m_reason;
};

static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE_PER_KB);

BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams &params) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/util/setup_common.h
Expand Up @@ -201,4 +201,20 @@ std::ostream &operator<<(std::ostream &os, const ScriptError &err);

CBlock getBlock13b8a();

/**
* BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
* Use as
* BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
*/
class HasReason {
public:
explicit HasReason(const std::string &reason) : m_reason(reason) {}
template <typename E> bool operator()(const E &e) const {
return std::string(e.what()).find(m_reason) != std::string::npos;
};

private:
const std::string m_reason;
};

#endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H

0 comments on commit 4adf771

Please sign in to comment.