Skip to content

Commit 7a820ce

Browse files
committed
test, build: Separate read_json function into its own module
1 parent 835212c commit 7a820ce

File tree

8 files changed

+38
-21
lines changed

8 files changed

+38
-21
lines changed

src/Makefile.test_util.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \
1010
TEST_UTIL_H = \
1111
test/util/blockfilter.h \
1212
test/util/chainstate.h \
13+
test/util/json.h \
1314
test/util/logging.h \
1415
test/util/mining.h \
1516
test/util/net.h \
@@ -28,6 +29,7 @@ libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(BOOST_CPPFLAGS)
2829
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
2930
libtest_util_a_SOURCES = \
3031
test/util/blockfilter.cpp \
32+
test/util/json.cpp \
3133
test/util/logging.cpp \
3234
test/util/mining.cpp \
3335
test/util/net.cpp \

src/test/base58_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <test/data/base58_encode_decode.json.h>
66

77
#include <base58.h>
8+
#include <test/util/json.h>
89
#include <test/util/setup_common.h>
910
#include <util/strencodings.h>
1011
#include <util/vector.h>
@@ -16,8 +17,6 @@
1617

1718
using namespace std::literals;
1819

19-
UniValue read_json(const std::string& jsondata);
20-
2120
BOOST_FIXTURE_TEST_SUITE(base58_tests, BasicTestingSetup)
2221

2322
// Goal: test low-level base58 encoding functionality

src/test/key_io_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
#include <key.h>
99
#include <key_io.h>
1010
#include <script/script.h>
11+
#include <test/util/json.h>
1112
#include <test/util/setup_common.h>
1213
#include <util/strencodings.h>
1314

1415
#include <boost/test/unit_test.hpp>
1516

1617
#include <univalue.h>
1718

18-
UniValue read_json(const std::string& jsondata);
19-
2019
BOOST_FIXTURE_TEST_SUITE(key_io_tests, BasicTestingSetup)
2120

2221
// Goal: check that parsed keys match test payload

src/test/script_tests.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <script/sign.h>
1616
#include <script/signingprovider.h>
1717
#include <streams.h>
18+
#include <test/util/json.h>
1819
#include <test/util/setup_common.h>
1920
#include <test/util/transaction_utils.h>
2021
#include <util/strencodings.h>
@@ -41,18 +42,6 @@ static const unsigned int gFlags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
4142
unsigned int ParseScriptFlags(std::string strFlags);
4243
std::string FormatScriptFlags(unsigned int flags);
4344

44-
UniValue read_json(const std::string& jsondata)
45-
{
46-
UniValue v;
47-
48-
if (!v.read(jsondata) || !v.isArray())
49-
{
50-
BOOST_ERROR("Parse error.");
51-
return UniValue(UniValue::VARR);
52-
}
53-
return v.get_array();
54-
}
55-
5645
struct ScriptErrorDesc
5746
{
5847
ScriptError_t err;

src/test/sighash_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <serialize.h>
1111
#include <streams.h>
1212
#include <test/data/sighash.json.h>
13+
#include <test/util/json.h>
1314
#include <test/util/setup_common.h>
1415
#include <util/strencodings.h>
1516
#include <util/system.h>
@@ -21,8 +22,6 @@
2122

2223
#include <univalue.h>
2324

24-
UniValue read_json(const std::string& jsondata);
25-
2625
// Old script.cpp SignatureHash function
2726
uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
2827
{

src/test/transaction_tests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <script/signingprovider.h>
2222
#include <script/standard.h>
2323
#include <streams.h>
24+
#include <test/util/json.h>
2425
#include <test/util/script.h>
2526
#include <test/util/transaction_utils.h>
2627
#include <util/strencodings.h>
@@ -37,9 +38,6 @@
3738

3839
typedef std::vector<unsigned char> valtype;
3940

40-
// In script_tests.cpp
41-
UniValue read_json(const std::string& jsondata);
42-
4341
static CFeeRate g_dust{DUST_RELAY_TX_FEE};
4442
static bool g_bare_multi{DEFAULT_PERMIT_BAREMULTISIG};
4543

src/test/util/json.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/util/json.h>
6+
7+
#include <string>
8+
#include <util/check.h>
9+
10+
#include <univalue.h>
11+
12+
UniValue read_json(const std::string& jsondata)
13+
{
14+
UniValue v;
15+
Assert(v.read(jsondata) && v.isArray());
16+
return v.get_array();
17+
}

src/test/util/json.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2023 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_TEST_UTIL_JSON_H
6+
#define BITCOIN_TEST_UTIL_JSON_H
7+
8+
#include <string>
9+
10+
#include <univalue.h>
11+
12+
UniValue read_json(const std::string& jsondata);
13+
14+
#endif // BITCOIN_TEST_UTIL_JSON_H

0 commit comments

Comments
 (0)