Skip to content

Commit

Permalink
improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed May 4, 2024
1 parent 613c9e6 commit b6dfa41
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/c4/yml/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void report_error_impl(const char* msg, size_t length, Location loc, FILE *f)
{
if(!loc.name.empty())
{
// this is more portable than using fprintf("%.*s:") which
// is not available in some embedded platforms
fwrite(loc.name.str, 1, loc.name.len, f);
fputc(':', f);
}
Expand All @@ -36,8 +38,11 @@ void report_error_impl(const char* msg, size_t length, Location loc, FILE *f)
fprintf(f, "%zu:", loc.col);
if(loc.offset)
fprintf(f, " (%zuB):", loc.offset);
fputc(' ', f);
}
fprintf(f, "%.*s\n", (int)length, msg);
RYML_ASSERT(!to_csubstr(msg).ends_with('\0'));
fwrite(msg, 1, length, f);
fputc('\n', f);
fflush(f);
}

Expand Down
8 changes: 5 additions & 3 deletions src/c4/yml/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,13 @@ struct _SubstrWriter


namespace detail {
// dumpfn is a function abstracting prints to terminal (or to string).
template<class DumpFn, class ...Args>
C4_NO_INLINE void _parse_dump(DumpFn dumpfn, csubstr fmt, Args&& ...args)
C4_NO_INLINE void _parse_dump(DumpFn &&dumpfn, csubstr fmt, Args&& ...args)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
{
// buffer for converting individual arguments.
char writebuf[RYML_LOGBUF_SIZE];
auto results = format_dump_resume(dumpfn, writebuf, fmt, std::forward<Args>(args)...);
auto results = format_dump_resume(std::forward<DumpFn>(dumpfn), writebuf, fmt, std::forward<Args>(args)...);
// resume writing if the results failed to fit the buffer
if(C4_UNLIKELY(results.bufsize > RYML_LOGBUF_SIZE)) // bufsize will be that of the largest element serialized. Eg int(1) will require 1 byte.
{
Expand All @@ -559,7 +561,7 @@ C4_NO_INLINE void _parse_dump(DumpFn dumpfn, csubstr fmt, Args&& ...args)
#else
substr largerbuf = {static_cast<char*>(alloca(bufsize)), results.bufsize};
#endif
results = format_dump_resume(dumpfn, results, largerbuf, fmt, std::forward<Args>(args)...);
results = format_dump_resume(std::forward<DumpFn>(dumpfn), results, largerbuf, fmt, std::forward<Args>(args)...);
}
}
template<class ...Args>
Expand Down
2 changes: 1 addition & 1 deletion src/c4/yml/node_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ typedef enum : type_bits {
// style flags:
//
FLOW_SL = __(14), ///< mark container with single-line flow style (seqs as '[val1,val2], maps as '{key: val,key2: val2}')
FLOW_ML = __(15), ///< mark container with multi-line flow style (seqs as '[\n val1,\n val2\n], maps as '{\n key: val,\n key2: val2\n}')
FLOW_ML = __(15), ///< (NOT IMPLEMENTED, work in progress) mark container with multi-line flow style (seqs as '[\n val1,\n val2\n], maps as '{\n key: val,\n key2: val2\n}')
BLOCK = __(16), ///< mark container with block style (seqs as '- val\n', maps as 'key: val')
KEY_LITERAL = __(17), ///< mark key scalar as multiline, block literal |
VAL_LITERAL = __(18), ///< mark val scalar as multiline, block literal |
Expand Down
6 changes: 4 additions & 2 deletions src/c4/yml/tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ YamlTag_e to_tag(csubstr tag)
return TAG_TIMESTAMP;
else if(tag == "value")
return TAG_VALUE;
else if(tag == "yaml")
return TAG_YAML;

return TAG_NONE;
}
Expand Down Expand Up @@ -153,9 +155,9 @@ csubstr from_tag_long(YamlTag_e tag)
case TAG_YAML:
return {"<tag:yaml.org,2002:yaml>"};
case TAG_NONE:
default:
return {""};
}
return {""};
}

csubstr from_tag(YamlTag_e tag)
Expand Down Expand Up @@ -193,9 +195,9 @@ csubstr from_tag(YamlTag_e tag)
case TAG_YAML:
return {"!!yaml"};
case TAG_NONE:
default:
return {""};
}
return {""};
}


Expand Down
69 changes: 69 additions & 0 deletions test/test_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,75 @@ TEST(RYML_ASSERT, basic)
EXPECT_NE(get_callbacks().m_error, &test_error_impl);
}

struct Dumper
{
char errmsg[RYML_ERRMSG_SIZE] = {0};
detail::_SubstrWriter writer{errmsg};
void operator()(csubstr s)
{
writer.append(s);
}
};
TEST(_parse_dump, small_args)
{
const std::string str(/*count*/RYML_LOGBUF_SIZE-1, 's');
const csubstr fmt = "smaller={}";
const std::string expected = formatrs<std::string>(fmt, str);
{
Dumper dumper;
char writebuf[RYML_LOGBUF_SIZE];
DumpResults results = format_dump_resume(dumper, writebuf, fmt, str);
EXPECT_EQ(results.bufsize, str.size());
EXPECT_EQ(dumper.writer.curr(), to_csubstr(expected));
}
{
Dumper dumper;
detail::_parse_dump(dumper, fmt, str);
EXPECT_EQ(dumper.writer.curr(), to_csubstr(expected));
}
}

TEST(_parse_dump, large_args)
{
const std::string str(/*count*/RYML_LOGBUF_SIZE+1, 'l');
const csubstr fmt = "larger={}";
{
Dumper dumper;
char writebuf[RYML_LOGBUF_SIZE];
DumpResults results = format_dump_resume(dumper, writebuf, fmt, str);
const csubstr expected = "larger=";
EXPECT_EQ(results.bufsize, str.size());
EXPECT_EQ(dumper.writer.curr(), expected);
}
{
Dumper dumper;
detail::_parse_dump(dumper, fmt, str);
const std::string expected = formatrs<std::string>(fmt, str);
EXPECT_EQ(dumper.writer.curr(), to_csubstr(expected));
}
}

TEST(_parse_dump, unprintable_args)
{
const std::string str(/*count*/RYML_LOGBUF_SIZE_MAX+1, 'u');
const csubstr fmt = "unprintable={}";
{
Dumper dumper;
char writebuf[RYML_LOGBUF_SIZE];
DumpResults results = format_dump_resume(dumper, writebuf, fmt, str);
const csubstr expected = "unprintable=";
EXPECT_EQ(results.bufsize, str.size());
EXPECT_EQ(dumper.writer.curr(), expected);
}
{
Dumper dumper;
detail::_parse_dump(dumper, fmt, str);
const std::string zeros(/*count*/RYML_LOGBUF_SIZE_MAX+1, '\0');
const std::string expected = formatrs<std::string>(fmt, zeros);
EXPECT_EQ(dumper.writer.pos, expected.size());
EXPECT_EQ(dumper.writer.curr(), to_csubstr(expected).first(dumper.writer.buf.len));
}
}

// FIXME this is here merely to avoid a linker error
Case const* get_case(csubstr)
Expand Down
8 changes: 8 additions & 0 deletions test/test_emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ TEST(emit, existing_tree)
test_emits(t, expected, expected_json);
}

TEST(emit, no_node)
{
const Tree t = parse_in_arena("[foo, bar]");
std::string expected = "[foo,bar]";
std::string expected_json = R"(["foo","bar"])";
test_emits(t, NONE, expected, expected_json);
}

TEST(emit, existing_seq_node)
{
Tree nct = parse_in_arena("[foo, bar, [nested, seq], {nested: map}]");
Expand Down
53 changes: 42 additions & 11 deletions test/test_tag_property.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ namespace yml {

//-----------------------------------------------------------------------------

TEST(to_tag, user)
TEST(tag_functions, to_tag)
{
EXPECT_EQ(to_tag("!"), TAG_NONE);
EXPECT_EQ(to_tag("!."), TAG_NONE);
EXPECT_EQ(to_tag("!good_type"), TAG_NONE);
}

TEST(to_tag, double_exc_mark)
{
EXPECT_EQ(to_tag("!!" ), TAG_NONE);
EXPECT_EQ(to_tag("!!." ), TAG_NONE);

Expand Down Expand Up @@ -51,7 +47,7 @@ TEST(to_tag, double_exc_mark)
EXPECT_EQ(to_tag("!!value." ), TAG_NONE);
}

TEST(to_tag, with_namespace)
TEST(tag_functions, to_tag__with_namespace)
{
EXPECT_EQ(to_tag("tag:yaml.org,2002:" ), TAG_NONE);
EXPECT_EQ(to_tag("tag:yaml.org,2002:." ), TAG_NONE);
Expand All @@ -70,6 +66,7 @@ TEST(to_tag, with_namespace)
EXPECT_EQ(to_tag("tag:yaml.org,2002:str" ), TAG_STR);
EXPECT_EQ(to_tag("tag:yaml.org,2002:timestamp" ), TAG_TIMESTAMP);
EXPECT_EQ(to_tag("tag:yaml.org,2002:value" ), TAG_VALUE);
EXPECT_EQ(to_tag("tag:yaml.org,2002:yaml" ), TAG_YAML);

EXPECT_EQ(to_tag("tag:yaml.org,2002:map." ), TAG_NONE);
EXPECT_EQ(to_tag("tag:yaml.org,2002:omap." ), TAG_NONE);
Expand All @@ -85,9 +82,10 @@ TEST(to_tag, with_namespace)
EXPECT_EQ(to_tag("tag:yaml.org,2002:str." ), TAG_NONE);
EXPECT_EQ(to_tag("tag:yaml.org,2002:timestamp."), TAG_NONE);
EXPECT_EQ(to_tag("tag:yaml.org,2002:value." ), TAG_NONE);
EXPECT_EQ(to_tag("tag:yaml.org,2002:yaml." ), TAG_NONE);
}

TEST(to_tag, with_namespace_bracket)
TEST(tag_functions, to_tag__with_namespace_bracket)
{
EXPECT_EQ(to_tag("<tag:yaml.org,2002:" ), TAG_NONE);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:." ), TAG_NONE);
Expand All @@ -107,6 +105,7 @@ TEST(to_tag, with_namespace_bracket)
EXPECT_EQ(to_tag("<tag:yaml.org,2002:str>" ), TAG_STR);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:timestamp>" ), TAG_TIMESTAMP);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:value>" ), TAG_VALUE);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:yaml>" ), TAG_YAML);

EXPECT_EQ(to_tag("<tag:yaml.org,2002:map.>" ), TAG_NONE);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:omap.>" ), TAG_NONE);
Expand All @@ -122,9 +121,10 @@ TEST(to_tag, with_namespace_bracket)
EXPECT_EQ(to_tag("<tag:yaml.org,2002:str.>" ), TAG_NONE);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:timestamp.>"), TAG_NONE);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:value.>" ), TAG_NONE);
EXPECT_EQ(to_tag("<tag:yaml.org,2002:yaml.>" ), TAG_NONE);
}

TEST(from_tag, basic)
TEST(tag_functions, from_tag)
{
EXPECT_EQ("", from_tag(TAG_NONE));

Expand All @@ -142,9 +142,31 @@ TEST(from_tag, basic)
EXPECT_EQ("!!str" , from_tag(TAG_STR));
EXPECT_EQ("!!timestamp" , from_tag(TAG_TIMESTAMP));
EXPECT_EQ("!!value" , from_tag(TAG_VALUE));
EXPECT_EQ("!!yaml" , from_tag(TAG_YAML));
}

TEST(tag_functions, from_tag_long)
{
EXPECT_EQ("", from_tag_long(TAG_NONE));

EXPECT_EQ("<tag:yaml.org,2002:map>" , from_tag_long(TAG_MAP));
EXPECT_EQ("<tag:yaml.org,2002:omap>" , from_tag_long(TAG_OMAP));
EXPECT_EQ("<tag:yaml.org,2002:pairs>" , from_tag_long(TAG_PAIRS));
EXPECT_EQ("<tag:yaml.org,2002:set>" , from_tag_long(TAG_SET));
EXPECT_EQ("<tag:yaml.org,2002:seq>" , from_tag_long(TAG_SEQ));
EXPECT_EQ("<tag:yaml.org,2002:binary>" , from_tag_long(TAG_BINARY));
EXPECT_EQ("<tag:yaml.org,2002:bool>" , from_tag_long(TAG_BOOL));
EXPECT_EQ("<tag:yaml.org,2002:float>" , from_tag_long(TAG_FLOAT));
EXPECT_EQ("<tag:yaml.org,2002:int>" , from_tag_long(TAG_INT));
EXPECT_EQ("<tag:yaml.org,2002:merge>" , from_tag_long(TAG_MERGE));
EXPECT_EQ("<tag:yaml.org,2002:null>" , from_tag_long(TAG_NULL));
EXPECT_EQ("<tag:yaml.org,2002:str>" , from_tag_long(TAG_STR));
EXPECT_EQ("<tag:yaml.org,2002:timestamp>" , from_tag_long(TAG_TIMESTAMP));
EXPECT_EQ("<tag:yaml.org,2002:value>" , from_tag_long(TAG_VALUE));
EXPECT_EQ("<tag:yaml.org,2002:yaml>" , from_tag_long(TAG_YAML));
}

TEST(normalize_tag, basic)
TEST(tag_functions, normalize_tag)
{
EXPECT_EQ(normalize_tag("<tag:yaml.org,2002:" ), "<tag:yaml.org,2002:");
EXPECT_EQ(normalize_tag("<tag:yaml.org,2002:." ), "<tag:yaml.org,2002:.");
Expand All @@ -163,6 +185,7 @@ TEST(normalize_tag, basic)
EXPECT_EQ(normalize_tag("<tag:yaml.org,2002:str>" ), "!!str");
EXPECT_EQ(normalize_tag("<tag:yaml.org,2002:timestamp>" ), "!!timestamp");
EXPECT_EQ(normalize_tag("<tag:yaml.org,2002:value>" ), "!!value");
EXPECT_EQ(normalize_tag("<tag:yaml.org,2002:yaml>" ), "!!yaml");

EXPECT_EQ(normalize_tag("!<tag:yaml.org,2002:map>" ), "!!map");
EXPECT_EQ(normalize_tag("!<tag:yaml.org,2002:omap>" ), "!!omap");
Expand All @@ -178,6 +201,7 @@ TEST(normalize_tag, basic)
EXPECT_EQ(normalize_tag("!<tag:yaml.org,2002:str>" ), "!!str");
EXPECT_EQ(normalize_tag("!<tag:yaml.org,2002:timestamp>"), "!!timestamp");
EXPECT_EQ(normalize_tag("!<tag:yaml.org,2002:value>" ), "!!value");
EXPECT_EQ(normalize_tag("!<tag:yaml.org,2002:yaml>" ), "!!yaml");

EXPECT_EQ(normalize_tag("!!map" ), "!!map");
EXPECT_EQ(normalize_tag("!!omap" ), "!!omap");
Expand All @@ -193,6 +217,7 @@ TEST(normalize_tag, basic)
EXPECT_EQ(normalize_tag("!!str" ), "!!str");
EXPECT_EQ(normalize_tag("!!timestamp"), "!!timestamp");
EXPECT_EQ(normalize_tag("!!value" ), "!!value");
EXPECT_EQ(normalize_tag("!!yaml" ), "!!yaml");

EXPECT_EQ(normalize_tag("!!foo" ), "!!foo");

Expand All @@ -207,7 +232,7 @@ TEST(normalize_tag, basic)
EXPECT_EQ(normalize_tag("!<!>"), "<!>");
}

TEST(normalize_tag_long, basic)
TEST(tag_functions, normalize_tag_long)
{
EXPECT_EQ(normalize_tag_long("<tag:yaml.org,2002:" ), "<tag:yaml.org,2002:");
EXPECT_EQ(normalize_tag_long("<tag:yaml.org,2002:." ), "<tag:yaml.org,2002:.");
Expand All @@ -226,6 +251,7 @@ TEST(normalize_tag_long, basic)
EXPECT_EQ(normalize_tag_long("<tag:yaml.org,2002:str>" ), "<tag:yaml.org,2002:str>");
EXPECT_EQ(normalize_tag_long("<tag:yaml.org,2002:timestamp>" ), "<tag:yaml.org,2002:timestamp>");
EXPECT_EQ(normalize_tag_long("<tag:yaml.org,2002:value>" ), "<tag:yaml.org,2002:value>");
EXPECT_EQ(normalize_tag_long("<tag:yaml.org,2002:yaml>" ), "<tag:yaml.org,2002:yaml>");

EXPECT_EQ(normalize_tag_long("!<tag:yaml.org,2002:map>" ), "<tag:yaml.org,2002:map>");
EXPECT_EQ(normalize_tag_long("!<tag:yaml.org,2002:omap>" ), "<tag:yaml.org,2002:omap>");
Expand All @@ -241,6 +267,7 @@ TEST(normalize_tag_long, basic)
EXPECT_EQ(normalize_tag_long("!<tag:yaml.org,2002:str>" ), "<tag:yaml.org,2002:str>");
EXPECT_EQ(normalize_tag_long("!<tag:yaml.org,2002:timestamp>"), "<tag:yaml.org,2002:timestamp>");
EXPECT_EQ(normalize_tag_long("!<tag:yaml.org,2002:value>" ), "<tag:yaml.org,2002:value>");
EXPECT_EQ(normalize_tag_long("!<tag:yaml.org,2002:yaml>" ), "<tag:yaml.org,2002:yaml>");

EXPECT_EQ(normalize_tag_long("!!map" ), "<tag:yaml.org,2002:map>");
EXPECT_EQ(normalize_tag_long("!!omap" ), "<tag:yaml.org,2002:omap>");
Expand All @@ -256,6 +283,7 @@ TEST(normalize_tag_long, basic)
EXPECT_EQ(normalize_tag_long("!!str" ), "<tag:yaml.org,2002:str>");
EXPECT_EQ(normalize_tag_long("!!timestamp"), "<tag:yaml.org,2002:timestamp>");
EXPECT_EQ(normalize_tag_long("!!value" ), "<tag:yaml.org,2002:value>");
EXPECT_EQ(normalize_tag_long("!!yaml" ), "<tag:yaml.org,2002:yaml>");

EXPECT_EQ(normalize_tag_long("!!foo" ), "!!foo");
EXPECT_EQ(normalize_tag_long("!!light" ), "!!light");
Expand Down Expand Up @@ -283,7 +311,7 @@ TEST(normalize_tag_long, basic)
EXPECT_EQ(normalize_tag_long("!<!>"), "<!>");
}

TEST(is_custom_tag, basic)
TEST(tag_functions, is_custom_tag)
{
EXPECT_FALSE(is_custom_tag("<tag:yaml.org,2002:"));
EXPECT_FALSE(is_custom_tag("<tag:yaml.org,2002:."));
Expand All @@ -302,6 +330,7 @@ TEST(is_custom_tag, basic)
EXPECT_FALSE(is_custom_tag("<tag:yaml.org,2002:str>"));
EXPECT_FALSE(is_custom_tag("<tag:yaml.org,2002:timestamp>"));
EXPECT_FALSE(is_custom_tag("<tag:yaml.org,2002:value>"));
EXPECT_FALSE(is_custom_tag("<tag:yaml.org,2002:yaml>"));

EXPECT_FALSE(is_custom_tag("!<tag:yaml.org,2002:map>"));
EXPECT_FALSE(is_custom_tag("!<tag:yaml.org,2002:omap>"));
Expand All @@ -317,6 +346,7 @@ TEST(is_custom_tag, basic)
EXPECT_FALSE(is_custom_tag("!<tag:yaml.org,2002:str>"));
EXPECT_FALSE(is_custom_tag("!<tag:yaml.org,2002:timestamp>"));
EXPECT_FALSE(is_custom_tag("!<tag:yaml.org,2002:value>"));
EXPECT_FALSE(is_custom_tag("!<tag:yaml.org,2002:yaml>"));

EXPECT_FALSE(is_custom_tag("!!map"));
EXPECT_FALSE(is_custom_tag("!!omap"));
Expand All @@ -332,6 +362,7 @@ TEST(is_custom_tag, basic)
EXPECT_FALSE(is_custom_tag("!!str"));
EXPECT_FALSE(is_custom_tag("!!timestamp"));
EXPECT_FALSE(is_custom_tag("!!value"));
EXPECT_FALSE(is_custom_tag("!!yaml"));

EXPECT_FALSE(is_custom_tag("!!foo"));
EXPECT_FALSE(is_custom_tag("!!light"));
Expand Down

0 comments on commit b6dfa41

Please sign in to comment.