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 b931c8d commit 3b148a7
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/yaml/yaml_parse_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,52 @@
#include <sourcemeta/jsontoolkit/json.h>
#include <sourcemeta/jsontoolkit/yaml.h>

TEST(YAML_parse, scalar_1) {
const std::string input{"1"};
const auto result{sourcemeta::jsontoolkit::from_yaml(input)};
const sourcemeta::jsontoolkit::JSON expected{1};
EXPECT_EQ(result, expected);
}

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),
Expand All @@ -14,3 +60,9 @@ TEST(YAML_parse, blank) {
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 3b148a7

Please sign in to comment.