tomlplusplus/tests/parsing_strings.cpp
Mark Gillard 40ffee43fb added json_formatter
also:
- added `toml_version.h`
- added version number to toml.hpp
- added `node::visit()`
- added `table::empty()`
- added `array::empty()`
- added version number to `toml.hpp`
- added ostream operator overload for `source_position`
- added tests for string escape sequences
- added tests for + in bare keys (toml/issues/687)
- added tests for hexfloat literals (toml/issues/562)
- added c++20 char8_t detection to meson
- moved third party submodules to `/extern`
- refactored noexcept version of `parse_result` to be more useful
- refactored all code to 'mostly' stick to a 120 column limit
- fixed some minor stuff picked up by gcc9
2020-01-06 22:24:53 +02:00

142 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "tests.h"
TEST_CASE("parsing strings")
{
parsing_should_succeed(S(R"(
str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."
str1 = """
Roses are red
Violets are blue"""
)"sv),
[](table&& tbl) noexcept
{
CHECK(tbl[S("str")] == S("I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."sv));
CHECK(tbl[S("str1")] == S("Roses are red\nViolets are blue"sv));
}
);
parsing_should_succeed(S(R"(
# The following strings are byte-for-byte equivalent:
str1 = "The quick brown fox jumps over the lazy dog."
str2 = """
The quick brown \
fox jumps over \
the lazy dog."""
str3 = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""
str4 = """Here are two quotation marks: "". Simple enough."""
# str5 = """Here are three quotation marks: """.""" # INVALID
str5 = """Here are three quotation marks: ""\"."""
str6 = """Here are fifteen quotation marks: ""\"""\"""\"""\"""\"."""
# "This," she said, "is just a pointless statement."
str7 = """"This," she said, "is just a pointless statement.""""
)"sv),
[](table&& tbl) noexcept
{
static constexpr auto quick_brown_fox = S("The quick brown fox jumps over the lazy dog."sv);
CHECK(tbl[S("str1")] == quick_brown_fox);
CHECK(tbl[S("str2")] == quick_brown_fox);
CHECK(tbl[S("str3")] == quick_brown_fox);
CHECK(tbl[S("str4")] == S(R"(Here are two quotation marks: "". Simple enough.)"sv));
CHECK(tbl[S("str5")] == S(R"(Here are three quotation marks: """.)"sv));
CHECK(tbl[S("str6")] == S(R"(Here are fifteen quotation marks: """"""""""""""".)"sv));
CHECK(tbl[S("str7")] == S(R"("This," she said, "is just a pointless statement.")"sv));
}
);
parsing_should_fail(S(R"(str5 = """Here are three quotation marks: """.""")"sv));
parsing_should_succeed(S(R"(
# What you see is what you get.
winpath = 'C:\Users\nodejs\templates'
winpath2 = '\\ServerX\admin$\system32\'
quoted = 'Tom "Dubs" Preston-Werner'
regex = '<\i\c*\s*>'
regex2 = '''I [dw]on't need \d{2} apples'''
lines = '''
The first newline is
trimmed in raw strings.
All other whitespace
is preserved.
'''
)"sv),
[](table&& tbl) noexcept
{
CHECK(tbl[S("winpath")] == S(R"(C:\Users\nodejs\templates)"sv));
CHECK(tbl[S("winpath2")] == S(R"(\\ServerX\admin$\system32\)"sv));
CHECK(tbl[S("quoted")] == S(R"(Tom "Dubs" Preston-Werner)"sv));
CHECK(tbl[S("regex")] == S(R"(<\i\c*\s*>)"sv));
CHECK(tbl[S("regex2")] == S(R"(I [dw]on't need \d{2} apples)"sv));
CHECK(tbl[S("lines")] == S(R"(The first newline is
trimmed in raw strings.
All other whitespace
is preserved.
)"sv));
}
);
parsing_should_succeed(S(R"(
quot15 = '''Here are fifteen quotation marks: """""""""""""""'''
# apos15 = '''Here are fifteen apostrophes: '''''''''''''''''' # INVALID
apos15 = "Here are fifteen apostrophes: '''''''''''''''"
# 'That's still pointless', she said.
str = ''''That's still pointless', she said.'''
)"sv),
[](table&& tbl) noexcept
{
CHECK(tbl[S("quot15")] == S(R"(Here are fifteen quotation marks: """"""""""""""")"sv));
CHECK(tbl[S("apos15")] == S(R"(Here are fifteen apostrophes: ''''''''''''''')"sv));
CHECK(tbl[S("str")] == S(R"('That's still pointless', she said.)"sv));
}
);
parsing_should_fail(S(R"(apos15 = '''Here are fifteen apostrophes: '''''''''''''''''' # INVALID)"sv));
//value tests
parse_expected_value(
R"("The quick brown fox jumps over the lazy dog")"sv,
S("The quick brown fox jumps over the lazy dog"sv));
parse_expected_value(
R"('The quick brown fox jumps over the lazy dog')"sv,
S("The quick brown fox jumps over the lazy dog"sv));
parse_expected_value(
R"("""The quick brown fox jumps over the lazy dog""")"sv,
S("The quick brown fox jumps over the lazy dog"sv));
parse_expected_value(
R"('''The quick brown fox jumps over the lazy dog''')"sv,
S("The quick brown fox jumps over the lazy dog"sv));
parse_expected_value(
R"("Ýôú' λáƭè è áƒƭèř ƭλïƨ - #")"sv,
S(R"(Ýôú' λáƭè ₥è áƒƭèř ƭλïƨ - #)"sv));
parse_expected_value(
R"(" Âñδ ωλèñ \"'ƨ ářè ïñ ƭλè ƨƭřïñϱ, áℓôñϱ ωïƭλ # \"")"sv,
S(R"( Âñδ ωλèñ "'ƨ ářè ïñ ƭλè ƨƭřïñϱ, áôñϱ ωïƭλ # ")"sv));
parse_expected_value(
R"("Ýôú δôñ'ƭ ƭλïñƙ ƨôè úƨèř ωôñ'ƭ δô ƭλáƭ?")"sv,
S(R"(Ýôú δôñ'ƭ ƭλïñƙ ƨô₥è úƨèř ωôñ'ƭ δô ƭλáƭ?)"sv));
parse_expected_value(
R"("\"\u03B1\u03B2\u03B3\"")"sv,
S("\"\u03B1\u03B2\u03B3\""sv));
// toml/issues/622 - escaping alias for spaces
#if TOML_LANG_HIGHER_THAN(0, 5, 0)
parse_expected_value(
R"("The\squick\sbrown\sfox\sjumps\sover\sthe\slazy\sdog")"sv,
S("The quick brown fox jumps over the lazy dog"sv));
#else
parsing_should_fail(R"(str = "The\squick\sbrown\sfox\sjumps\sover\sthe\slazy\sdog")"sv);
#endif
}