Skip to content

Commit

Permalink
Add basic YAML parsing tests
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Cruz Viotti <[email protected]>
  • Loading branch information
jviotti committed Jan 14, 2025
1 parent ac7686a commit 7e50557
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ if(JSONTOOLKIT_TESTS)
add_subdirectory(test/jsonl)
endif()

if(JSONTOOLKIT_JSON AND JSONTOOLKIT_YAML)
add_subdirectory(test/yaml)
endif()

if(PROJECT_IS_TOP_LEVEL)
# Otherwise we need the child project to link
# against the sanitizers too.
Expand Down
12 changes: 12 additions & 0 deletions test/yaml/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_executable(sourcemeta_jsontoolkit_yaml_unit
yaml_parse_test.cc)
noa_add_default_options(PRIVATE sourcemeta_jsontoolkit_yaml_unit)
target_link_libraries(sourcemeta_jsontoolkit_yaml_unit
PRIVATE GTest::gtest GTest::gtest_main)
target_link_libraries(sourcemeta_jsontoolkit_yaml_unit
PRIVATE sourcemeta::jsontoolkit::json)
target_link_libraries(sourcemeta_jsontoolkit_yaml_unit
PRIVATE sourcemeta::jsontoolkit::yaml)
set_target_properties(sourcemeta_jsontoolkit_yaml_unit
PROPERTIES FOLDER "JSON Toolkit/YAML")
add_test(NAME JSON COMMAND sourcemeta_jsontoolkit_yaml_unit --gtest_brief=1)
61 changes: 61 additions & 0 deletions test/yaml/yaml_parse_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <gtest/gtest.h>

#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/yaml.h>

TEST(YAML_parse, object_1) {
const std::string input{"hello: world\nfoo: 1\nbar: true"};

const auto result{sourcemeta::jsontoolkit::from_yaml(input)};

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"hello": "world",
"foo": 1,
"bar": true
})JSON");

EXPECT_EQ(result, expected);
}

TEST(YAML_parse, object_2) {
const std::string input{"foo: >\n bar\n baz"};

const auto result{sourcemeta::jsontoolkit::from_yaml(input)};

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"foo": "bar baz"
})JSON");

EXPECT_EQ(result, expected);
}

TEST(YAML_parse, array_1) {
const std::string input{"- foo\n- true"};

const auto result{sourcemeta::jsontoolkit::from_yaml(input)};

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON([ "foo", true ])JSON");

EXPECT_EQ(result, expected);
}

TEST(YAML_parse, empty) {
const std::string input{""};
EXPECT_THROW(sourcemeta::jsontoolkit::from_yaml(input),
sourcemeta::jsontoolkit::YAMLParseError);
}

TEST(YAML_parse, blank) {
const std::string input{" "};
EXPECT_THROW(sourcemeta::jsontoolkit::from_yaml(input),
sourcemeta::jsontoolkit::YAMLParseError);
}

TEST(YAML_parse, invalid_1) {
const std::string input{"{ xx"};
EXPECT_THROW(sourcemeta::jsontoolkit::from_yaml(input),
sourcemeta::jsontoolkit::YAMLParseError);
}

0 comments on commit 7e50557

Please sign in to comment.