Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 25 additions & 0 deletions test/yaml/yaml_error_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,28 @@ TEST(file_parse_error_column) {
"Unexpected scalar"};
EXPECT_EQ(error.column(), 7);
}

TEST(duplicate_key_error_message) {
const sourcemeta::core::YAMLDuplicateKeyError error{"foo", 4, 7};
EXPECT_STREQ(error.what(), "Duplicate key in YAML mapping");
}

TEST(duplicate_key_error_key) {
const sourcemeta::core::YAMLDuplicateKeyError error{"foo", 4, 7};
EXPECT_EQ(error.key(), "foo");
}

TEST(duplicate_key_error_empty_key) {
const sourcemeta::core::YAMLDuplicateKeyError error{"", 4, 7};
EXPECT_EQ(error.key(), "");
}

TEST(duplicate_key_error_line) {
const sourcemeta::core::YAMLDuplicateKeyError error{"foo", 4, 7};
EXPECT_EQ(error.line(), 4);
}

TEST(duplicate_key_error_column) {
const sourcemeta::core::YAMLDuplicateKeyError error{"foo", 4, 7};
EXPECT_EQ(error.column(), 7);
}
35 changes: 34 additions & 1 deletion test/yaml/yaml_parse_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,36 @@ TEST(block_mapping_resolved_key_duplicate_is_rejected) {
}
}

// The position reported is that of the offending second key, rather than of the
// mapping or of the first occurrence
TEST(block_mapping_duplicate_reports_the_second_key_position) {
const std::string input{"foo: 1\nbar: 2\nfoo: 3"};
try {
sourcemeta::core::parse_yaml(input);
FAIL();
} catch (const sourcemeta::core::YAMLDuplicateKeyError &error) {
EXPECT_EQ(error.key(), "foo");
EXPECT_EQ(error.line(), 3);
EXPECT_EQ(error.column(), 1);
} catch (...) {
FAIL();
}
}

TEST(flow_mapping_duplicate_reports_the_second_key_position) {
const std::string input{"{foo: 1, foo: 2}"};
try {
sourcemeta::core::parse_yaml(input);
FAIL();
} catch (const sourcemeta::core::YAMLDuplicateKeyError &error) {
EXPECT_EQ(error.key(), "foo");
EXPECT_EQ(error.line(), 1);
EXPECT_EQ(error.column(), 10);
} catch (...) {
FAIL();
}
}

TEST(flow_mapping_null_key_resolves_to_empty_string) {
const std::string input{"{~: v}"};
const auto result{sourcemeta::core::parse_yaml(input)};
Expand Down Expand Up @@ -2123,8 +2153,11 @@ TEST(explicit_key_block_mapping_duplicate_is_rejected) {
try {
const auto result{sourcemeta::core::parse_yaml("? a\n: 1\n? a\n: 2")};
FAIL();
} catch (const sourcemeta::core::YAMLParseError &error) {
} catch (const sourcemeta::core::YAMLDuplicateKeyError &error) {
EXPECT_EQ(error.key(), "a");
EXPECT_STREQ(error.what(), "Duplicate key in YAML mapping");
} catch (...) {
FAIL();
}
}

Expand Down
Loading