Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
test: clang-format -i src/univalue/test/unitester.cpp
  • Loading branch information
MarcoFalke committed Dec 20, 2024
commit faf7eac364fb7f421a649b483286ac8681d92b31
36 changes: 18 additions & 18 deletions src/univalue/test/unitester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,32 @@

static std::string rtrim(std::string s)
{
s.erase(s.find_last_not_of(" \n\r\t")+1);
s.erase(s.find_last_not_of(" \n\r\t") + 1);
return s;
}

static void runtest(std::string filename, const std::string& jdata)
{
std::string prefix = filename.substr(0, 4);
std::string prefix = filename.substr(0, 4);

bool wantPass = (prefix == "pass") || (prefix == "roun");
bool wantFail = (prefix == "fail");
bool wantRoundTrip = (prefix == "roun");
assert(wantPass || wantFail);
bool wantPass = (prefix == "pass") || (prefix == "roun");
bool wantFail = (prefix == "fail");
bool wantRoundTrip = (prefix == "roun");
assert(wantPass || wantFail);

UniValue val;
bool testResult = val.read(jdata);
UniValue val;
bool testResult = val.read(jdata);

if (wantPass) {
assert(testResult == true);
} else {
assert(testResult == false);
}
if (wantPass) {
assert(testResult == true);
} else {
assert(testResult == false);
}

if (wantRoundTrip) {
std::string odata = val.write(0, 0);
assert(odata == rtrim(jdata));
}
if (wantRoundTrip) {
std::string odata = val.write(0, 0);
assert(odata == rtrim(jdata));
}
}

#define TEST_FILE(name) {#name, json_tests::name}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand why a macro is the smallest change to make runtest happy, but I think we can avoid that weird string parsing and introduce enums instead:

enum class TestType { Fail, Pass, RoundTrip };

and define the test as:

inline constexpr std::map<std::string_view, TestType> tests{
    {json_tests::fail1, TestType::Fail},
    {json_tests::fail2, TestType::Fail},
    {json_tests::fail3, TestType::Fail},
...

which could simplify runtest to:

static void runtest(std::string_view data, TestType type) {
    UniValue val;
    bool testResult = val.read(std::string{data});

    if (type == TestType::Fail) {
        assert(!testResult);
        return;
    }
    ...

@maflcko maflcko Dec 20, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to keep the changes minimal. Also, I am not sure if an enum class is the right choice, so I'll leave this as-is for now.

Expand Down Expand Up @@ -185,7 +185,7 @@ void no_nul_test()
assert(val.read({buf + 3, 7}));
}

int main (int argc, char *argv[])
int main(int argc, char* argv[])
{
for (const auto& [file, json] : tests) {
runtest(std::string{file}, std::string{json});
Expand Down