Skip to content

Commit

Permalink
re #442: fix style heuristics for numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Jun 21, 2024
1 parent d28ba0b commit 7e1b8fe
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/c4/yml/node_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ bool scalar_style_query_plain(csubstr s) noexcept
else if(s.sub(2).is_number())
return true;
}
else if(s.begins_with_any("0123456789.-+") && s.is_number())
{
return true;
}
return s != ':'
&& ( ! s.begins_with_any("-:?*&,'\"{}[]|>%#@`\r")) // @ and ` are reserved characters
&& ( ! s.ends_with_any(":#"))
Expand Down
115 changes: 109 additions & 6 deletions test/test_node_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,132 @@ TEST(NodeType, type_str)
#undef teststr
}

TEST(NodeType, scalar_style_choose)
{
EXPECT_EQ(scalar_style_choose(" \n\t"), SCALAR_DQUO);
EXPECT_EQ(scalar_style_choose("01"), SCALAR_PLAIN);
}

TEST(NodeType, scalar_style_choose_json)
{
EXPECT_EQ(scalar_style_json_choose("true"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("false"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("null"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("0.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("-0.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("+0.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose(".1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("+.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("-.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_json_choose("01"), SCALAR_DQUO);
EXPECT_EQ(scalar_style_json_choose("foo"), SCALAR_DQUO);
EXPECT_EQ(scalar_style_json_choose("bar"), SCALAR_DQUO);
}

TEST(NodeType, scalar_style_choose)
{
EXPECT_EQ(scalar_style_choose(" \n\t"), SCALAR_DQUO);
EXPECT_EQ(scalar_style_choose("-.inf"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-.INF"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-.034"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-.034x"), SCALAR_SQUO);
EXPECT_EQ(scalar_style_choose("2.35e-10"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-2.35e-10"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+2.35e-10"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0.1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("01"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0x1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0o1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0b1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0x1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0o1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0b1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-01"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0x1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0o1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0b1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0x1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0o1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0b1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+01"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0x1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0o1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0b1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0x1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0o1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0b1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("01"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0X1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0O1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0B1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0X1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0O1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("0B1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-01"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0X1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0O1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0B1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0X1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0O1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("-0B1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+01"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0X1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0O1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0B1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0X1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0O1"), SCALAR_PLAIN);
EXPECT_EQ(scalar_style_choose("+0B1"), SCALAR_PLAIN);
}

TEST(NodeType, scalar_style_query_plain)
{
EXPECT_TRUE(scalar_style_query_plain("-.inf"));
EXPECT_TRUE(scalar_style_query_plain("-.INF"));
EXPECT_TRUE(scalar_style_query_plain("-.034"));
EXPECT_FALSE(scalar_style_query_plain("-.034x"));
EXPECT_TRUE(scalar_style_query_plain("2.35e-10"));
EXPECT_TRUE(scalar_style_query_plain("-2.35e-10"));
EXPECT_TRUE(scalar_style_query_plain("+2.35e-10"));
EXPECT_TRUE(scalar_style_query_plain("0.1"));
EXPECT_TRUE(scalar_style_query_plain("-0.1"));
EXPECT_TRUE(scalar_style_query_plain("+0.1"));
EXPECT_TRUE(scalar_style_query_plain("01"));
EXPECT_TRUE(scalar_style_query_plain("0x1"));
EXPECT_TRUE(scalar_style_query_plain("0o1"));
EXPECT_TRUE(scalar_style_query_plain("0b1"));
EXPECT_TRUE(scalar_style_query_plain("0x1"));
EXPECT_TRUE(scalar_style_query_plain("0o1"));
EXPECT_TRUE(scalar_style_query_plain("0b1"));
EXPECT_TRUE(scalar_style_query_plain("+01"));
EXPECT_TRUE(scalar_style_query_plain("+0x1"));
EXPECT_TRUE(scalar_style_query_plain("+0o1"));
EXPECT_TRUE(scalar_style_query_plain("+0b1"));
EXPECT_TRUE(scalar_style_query_plain("+0x1"));
EXPECT_TRUE(scalar_style_query_plain("+0o1"));
EXPECT_TRUE(scalar_style_query_plain("+0b1"));
EXPECT_TRUE(scalar_style_query_plain("-01"));
EXPECT_TRUE(scalar_style_query_plain("-0x1"));
EXPECT_TRUE(scalar_style_query_plain("-0o1"));
EXPECT_TRUE(scalar_style_query_plain("-0b1"));
EXPECT_TRUE(scalar_style_query_plain("-0x1"));
EXPECT_TRUE(scalar_style_query_plain("-0o1"));
EXPECT_TRUE(scalar_style_query_plain("-0b1"));
EXPECT_TRUE(scalar_style_query_plain("0X1"));
EXPECT_TRUE(scalar_style_query_plain("0O1"));
EXPECT_TRUE(scalar_style_query_plain("0B1"));
EXPECT_TRUE(scalar_style_query_plain("0X1"));
EXPECT_TRUE(scalar_style_query_plain("0O1"));
EXPECT_TRUE(scalar_style_query_plain("0B1"));
EXPECT_TRUE(scalar_style_query_plain("+01"));
EXPECT_TRUE(scalar_style_query_plain("+0X1"));
EXPECT_TRUE(scalar_style_query_plain("+0O1"));
EXPECT_TRUE(scalar_style_query_plain("+0B1"));
EXPECT_TRUE(scalar_style_query_plain("+0X1"));
EXPECT_TRUE(scalar_style_query_plain("+0O1"));
EXPECT_TRUE(scalar_style_query_plain("+0B1"));
EXPECT_TRUE(scalar_style_query_plain("-01"));
EXPECT_TRUE(scalar_style_query_plain("-0X1"));
EXPECT_TRUE(scalar_style_query_plain("-0O1"));
EXPECT_TRUE(scalar_style_query_plain("-0B1"));
EXPECT_TRUE(scalar_style_query_plain("-0X1"));
EXPECT_TRUE(scalar_style_query_plain("-0O1"));
EXPECT_TRUE(scalar_style_query_plain("-0B1"));
}


Expand Down

0 comments on commit 7e1b8fe

Please sign in to comment.