Skip to content

Commit

Permalink
Tests for JSON utils
Browse files Browse the repository at this point in the history
  • Loading branch information
haenoe committed Mar 31, 2024
1 parent fbd750d commit 8525624
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions tests/unit/libutil/json-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <gtest/gtest.h>

#include "error.hh"
#include "json-utils.hh"

namespace nix {
Expand Down Expand Up @@ -55,4 +56,104 @@ TEST(from_json, vectorOfOptionalInts) {
ASSERT_FALSE(vals.at(1).has_value());
}

TEST(valueAt, simpleObject) {
nlohmann::json simple = nlohmann::json::parse(R"({ "hello": "world" })");

ASSERT_EQ(valueAt(simple, "hello"), "world");

nlohmann::json nested = nlohmann::json::parse(R"({ "hello": { "world": "" } })");

auto & nestedObject = valueAt(nested, "hello");

ASSERT_EQ(valueAt(nestedObject, "world"), "");
}

TEST(valueAt, missingKey) {
nlohmann::json json = nlohmann::json::parse(R"({ "hello": { "nested": "world" } })");

ASSERT_THROW(valueAt(json, "foo"), Error);
}

TEST(getObject, rightAssertions) {
nlohmann::json simple = nlohmann::json::parse(R"({ "object": {} })");

ASSERT_EQ(getObject(valueAt(simple, "object")), nlohmann::json::parse("{}"));

nlohmann::json nested = nlohmann::json::parse(R"({ "object": { "object": {} } })");

auto & nestedObject = getObject(valueAt(nested, "object"));

ASSERT_EQ(nestedObject, nlohmann::json::parse(R"({ "object": {} })"));
ASSERT_EQ(getObject(valueAt(nestedObject, "object")), nlohmann::json::parse("{}"));
}

TEST(getObject, wrongAssertions) {
nlohmann::json json = nlohmann::json::parse(R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })");

ASSERT_THROW(getObject(valueAt(json, "array")), Error);
ASSERT_THROW(getObject(valueAt(json, "string")), Error);
ASSERT_THROW(getObject(valueAt(json, "int")), Error);
ASSERT_THROW(getObject(valueAt(json, "boolean")), Error);
}

TEST(getArray, rightAssertions) {
nlohmann::json simple = nlohmann::json::parse(R"({ "array": [] })");

ASSERT_EQ(getArray(valueAt(simple, "array")), nlohmann::json::parse("[]"));
}

TEST(getArray, wrongAssertions) {
nlohmann::json json = nlohmann::json::parse(R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })");

ASSERT_THROW(getArray(valueAt(json, "object")), Error);
ASSERT_THROW(getArray(valueAt(json, "string")), Error);
ASSERT_THROW(getArray(valueAt(json, "int")), Error);
ASSERT_THROW(getArray(valueAt(json, "boolean")), Error);
}

TEST(getString, rightAssertions) {
nlohmann::json simple = nlohmann::json::parse(R"({ "string": "" })");

ASSERT_EQ(getString(valueAt(simple, "string")), "");
}

TEST(getString, wrongAssertions) {
nlohmann::json json = nlohmann::json::parse(R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })");

ASSERT_THROW(getString(valueAt(json, "object")), Error);
ASSERT_THROW(getString(valueAt(json, "array")), Error);
ASSERT_THROW(getString(valueAt(json, "int")), Error);
ASSERT_THROW(getString(valueAt(json, "boolean")), Error);
}

TEST(getInteger, rightAssertions) {
nlohmann::json simple = nlohmann::json::parse(R"({ "int": 0 })");

ASSERT_EQ(getInteger(valueAt(simple, "int")), 0);
}

TEST(getInteger, wrongAssertions) {
nlohmann::json json = nlohmann::json::parse(R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })");

ASSERT_THROW(getInteger(valueAt(json, "object")), Error);
ASSERT_THROW(getInteger(valueAt(json, "array")), Error);
ASSERT_THROW(getInteger(valueAt(json, "string")), Error);
ASSERT_THROW(getInteger(valueAt(json, "boolean")), Error);
}

TEST(getBoolean, rightAssertions) {
nlohmann::json simple = nlohmann::json::parse(R"({ "boolean": false })");

ASSERT_EQ(getBoolean(valueAt(simple, "boolean")), false);
}

TEST(getBoolean, wrongAssertions) {
nlohmann::json json = nlohmann::json::parse(R"({ "object": {}, "array": [], "string": "", "int": 0, "boolean": false })");

ASSERT_THROW(getBoolean(valueAt(json, "object")), Error);
ASSERT_THROW(getBoolean(valueAt(json, "array")), Error);
ASSERT_THROW(getBoolean(valueAt(json, "string")), Error);
ASSERT_THROW(getBoolean(valueAt(json, "int")), Error);
}

} /* namespace nix */

0 comments on commit 8525624

Please sign in to comment.