Skip to content

Commit

Permalink
added tests for json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
zjeffer committed Jan 14, 2024
1 parent f744d90 commit e3875ba
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/util/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ struct fmt::formatter<Json::Value> : ostream_formatter {};
namespace waybar::util {

struct JsonParser {
JsonParser() {}
JsonParser() = default;

const Json::Value parse(const std::string& data) const {
Json::Value parse(const std::string& data) const {
Json::Value root(Json::objectValue);
if (data.empty()) {
return root;
Expand Down
26 changes: 26 additions & 0 deletions test/JsonParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "util/json.hpp"

#if __has_include(<catch2/catch_test_macros.hpp>)
#include <catch2/catch_test_macros.hpp>
#else
#include <catch2/catch.hpp>
#endif

TEST_CASE("Simple json", "[json]") {
SECTION("Parse simple json") {
std::string stringToTest = R"({"number": 5, "string": "test"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["number"].asInt() == 5);
REQUIRE(jsonValue["string"].asString() == "test");
}
}

TEST_CASE("Json with unicode", "[json]") {
SECTION("Parse json with unicode") {
std::string stringToTest = R"({"test": "\xab"})";
waybar::util::JsonParser parser;
Json::Value jsonValue = parser.parse(stringToTest);
REQUIRE(jsonValue["test"].asString() == "\xab");
}
}
1 change: 1 addition & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test_dep = [
]
test_src = files(
'main.cpp',
'JsonParser.cpp',
'SafeSignal.cpp',
'config.cpp',
'../src/config.cpp',
Expand Down

0 comments on commit e3875ba

Please sign in to comment.