test: refactor: Give unit test functions access to test state

Add unit test subclasses as needed so unit test functions that need to access
members like m_rng can reference it directly.
This commit is contained in:
Ryan Ofsky
2024-08-14 07:58:26 -04:00
committed by MarcoFalke
parent fab023e177
commit 3dc527f460
16 changed files with 145 additions and 81 deletions

View File

@@ -110,8 +110,7 @@ static ScriptError_t ParseScriptError(const std::string& name)
return SCRIPT_ERR_UNKNOWN_ERROR;
}
BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup)
struct ScriptTest : BasicTestingSetup {
void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, uint32_t flags, const std::string& message, int scriptError, CAmount nValue = 0)
{
bool expect = (scriptError == SCRIPT_ERR_OK);
@@ -136,6 +135,7 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, combined_flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message + strprintf(" (with flags %x)", combined_flags));
}
}
}; // struct ScriptTest
void static NegateSignatureS(std::vector<unsigned char>& vchSig) {
// Parse the signature.
@@ -369,11 +369,11 @@ public:
return *this;
}
TestBuilder& Test()
TestBuilder& Test(ScriptTest& test)
{
TestBuilder copy = *this; // Make a copy so we can rollback the push.
DoPush();
DoTest(creditTx->vout[0].scriptPubKey, spendTx.vin[0].scriptSig, scriptWitness, flags, comment, scriptError, nValue);
test.DoTest(creditTx->vout[0].scriptPubKey, spendTx.vin[0].scriptSig, scriptWitness, flags, comment, scriptError, nValue);
*this = copy;
return *this;
}
@@ -425,6 +425,8 @@ std::string JSONPrettyPrint(const UniValue& univalue)
}
} // namespace
BOOST_FIXTURE_TEST_SUITE(script_tests, ScriptTest)
BOOST_AUTO_TEST_CASE(script_build)
{
const KeyData keys;
@@ -884,7 +886,7 @@ BOOST_AUTO_TEST_CASE(script_build)
std::string strGen;
#endif
for (TestBuilder& test : tests) {
test.Test();
test.Test(*this);
std::string str = JSONPrettyPrint(test.GetJSON());
#ifdef UPDATE_JSON_TESTS
strGen += str + ",\n";