2020-06-26 18:01:27 +00:00
|
|
|
|
// This file is a part of toml++ and is subject to the the terms of the MIT license.
|
2021-01-02 15:48:47 +00:00
|
|
|
|
// Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
2020-06-26 18:01:27 +00:00
|
|
|
|
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
|
#include "tests.h"
|
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
|
TEST_CASE("parsing - strings")
|
2020-01-04 14:21:38 +00:00
|
|
|
|
{
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_succeed(
|
|
|
|
|
FILE_LINE_ARGS,
|
2020-06-28 22:57:59 +00:00
|
|
|
|
R"(
|
2020-01-04 14:21:38 +00:00
|
|
|
|
str = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."
|
|
|
|
|
|
|
|
|
|
str1 = """
|
2020-04-13 20:23:11 +00:00
|
|
|
|
Roses are red
|
|
|
|
|
Violets are blue"""
|
|
|
|
|
|
|
|
|
|
str2 = """
|
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
|
Roses are red
|
|
|
|
|
Violets are blue"""
|
2020-06-28 22:57:59 +00:00
|
|
|
|
)"sv,
|
2020-04-18 13:14:07 +00:00
|
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
|
{
|
2020-07-18 12:10:19 +00:00
|
|
|
|
CHECK(tbl["str"] == "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."sv);
|
|
|
|
|
CHECK(tbl["str1"] == "Roses are red\nViolets are blue"sv);
|
|
|
|
|
CHECK(tbl["str2"] == "\nRoses are red\nViolets are blue"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_succeed(
|
|
|
|
|
FILE_LINE_ARGS,
|
2020-06-28 22:57:59 +00:00
|
|
|
|
R"(
|
2020-01-04 14:21:38 +00:00
|
|
|
|
# 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.""""
|
2020-06-28 22:57:59 +00:00
|
|
|
|
)"sv,
|
2020-04-18 13:14:07 +00:00
|
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
|
{
|
2020-07-18 12:10:19 +00:00
|
|
|
|
static constexpr auto quick_brown_fox = "The quick brown fox jumps over the lazy dog."sv;
|
|
|
|
|
CHECK(tbl["str1"] == quick_brown_fox);
|
|
|
|
|
CHECK(tbl["str2"] == quick_brown_fox);
|
|
|
|
|
CHECK(tbl["str3"] == quick_brown_fox);
|
|
|
|
|
CHECK(tbl["str4"] == R"(Here are two quotation marks: "". Simple enough.)"sv);
|
|
|
|
|
CHECK(tbl["str5"] == R"(Here are three quotation marks: """.)"sv);
|
|
|
|
|
CHECK(tbl["str6"] == R"(Here are fifteen quotation marks: """"""""""""""".)"sv);
|
|
|
|
|
CHECK(tbl["str7"] == R"("This," she said, "is just a pointless statement.")"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-28 22:57:59 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(str5 = """Here are three quotation marks: """.""")"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_succeed(
|
|
|
|
|
FILE_LINE_ARGS,
|
2020-06-28 22:57:59 +00:00
|
|
|
|
R"(
|
2020-01-04 14:21:38 +00:00
|
|
|
|
# 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 = '''
|
2020-04-13 20:23:11 +00:00
|
|
|
|
The first newline is
|
|
|
|
|
trimmed in raw strings.
|
|
|
|
|
All other whitespace
|
|
|
|
|
is preserved.
|
|
|
|
|
'''
|
|
|
|
|
lines2 = '''
|
|
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
|
The first newline is
|
|
|
|
|
trimmed in raw strings.
|
|
|
|
|
All other whitespace
|
|
|
|
|
is preserved.
|
|
|
|
|
'''
|
2020-06-28 22:57:59 +00:00
|
|
|
|
)"sv,
|
2020-04-18 13:14:07 +00:00
|
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
|
{
|
2020-07-18 12:10:19 +00:00
|
|
|
|
CHECK(tbl["winpath"] == R"(C:\Users\nodejs\templates)"sv);
|
|
|
|
|
CHECK(tbl["winpath2"] == R"(\\ServerX\admin$\system32\)"sv);
|
|
|
|
|
CHECK(tbl["quoted"] == R"(Tom "Dubs" Preston-Werner)"sv);
|
|
|
|
|
CHECK(tbl["regex"] == R"(<\i\c*\s*>)"sv);
|
|
|
|
|
CHECK(tbl["regex2"] == R"(I [dw]on't need \d{2} apples)"sv);
|
|
|
|
|
CHECK(tbl["lines"] == R"(The first newline is
|
2020-04-13 20:23:11 +00:00
|
|
|
|
trimmed in raw strings.
|
|
|
|
|
All other whitespace
|
|
|
|
|
is preserved.
|
2020-07-18 12:10:19 +00:00
|
|
|
|
)"sv);
|
|
|
|
|
CHECK(tbl["lines2"] == R"(
|
2020-04-13 20:23:11 +00:00
|
|
|
|
The first newline is
|
2020-01-04 14:21:38 +00:00
|
|
|
|
trimmed in raw strings.
|
|
|
|
|
All other whitespace
|
|
|
|
|
is preserved.
|
2020-07-18 12:10:19 +00:00
|
|
|
|
)"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_succeed(
|
|
|
|
|
FILE_LINE_ARGS,
|
2020-06-28 22:57:59 +00:00
|
|
|
|
R"(
|
2020-01-04 14:21:38 +00:00
|
|
|
|
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.'''
|
2020-06-28 22:57:59 +00:00
|
|
|
|
)"sv,
|
2020-04-18 13:14:07 +00:00
|
|
|
|
[](table&& tbl)
|
2020-01-04 14:21:38 +00:00
|
|
|
|
{
|
2020-07-18 12:10:19 +00:00
|
|
|
|
CHECK(tbl["quot15"] == R"(Here are fifteen quotation marks: """"""""""""""")"sv);
|
|
|
|
|
CHECK(tbl["apos15"] == R"(Here are fifteen apostrophes: ''''''''''''''')"sv);
|
|
|
|
|
CHECK(tbl["str"] == R"('That's still pointless', she said.)"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-28 22:57:59 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(apos15 = '''Here are fifteen apostrophes: '''''''''''''''''' # INVALID)"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
|
// value tests
|
2020-01-04 14:21:38 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-04 14:21:38 +00:00
|
|
|
|
R"("The quick brown fox jumps over the lazy dog")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
"The quick brown fox jumps over the lazy dog"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-04 14:21:38 +00:00
|
|
|
|
R"('The quick brown fox jumps over the lazy dog')"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
"The quick brown fox jumps over the lazy dog"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-04 14:21:38 +00:00
|
|
|
|
R"("""The quick brown fox jumps over the lazy dog""")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
"The quick brown fox jumps over the lazy dog"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-04 14:21:38 +00:00
|
|
|
|
R"('''The quick brown fox jumps over the lazy dog''')"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
"The quick brown fox jumps over the lazy dog"sv);
|
2020-08-08 17:51:33 +00:00
|
|
|
|
|
|
|
|
|
#if UNICODE_LITERALS_OK
|
2020-01-06 18:21:16 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-06 18:21:16 +00:00
|
|
|
|
R"("Ýôú'ℓℓ λáƭè ₥è áƒƭèř ƭλïƨ - #")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
R"(Ýôú'ℓℓ λáƭè ₥è áƒƭèř ƭλïƨ - #)"sv);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-06 18:21:16 +00:00
|
|
|
|
R"(" Âñδ ωλèñ \"'ƨ ářè ïñ ƭλè ƨƭřïñϱ, áℓôñϱ ωïƭλ # \"")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
R"( Âñδ ωλèñ "'ƨ ářè ïñ ƭλè ƨƭřïñϱ, áℓôñϱ ωïƭλ # ")"sv);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-06 18:21:16 +00:00
|
|
|
|
R"("Ýôú δôñ'ƭ ƭλïñƙ ƨô₥è úƨèř ωôñ'ƭ δô ƭλáƭ?")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
R"(Ýôú δôñ'ƭ ƭλïñƙ ƨô₥è úƨèř ωôñ'ƭ δô ƭλáƭ?)"sv);
|
2020-08-08 17:51:33 +00:00
|
|
|
|
#endif // UNICODE_LITERALS_OK
|
|
|
|
|
|
2020-01-06 18:21:16 +00:00
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-01-06 18:21:16 +00:00
|
|
|
|
R"("\"\u03B1\u03B2\u03B3\"")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
"\"\u03B1\u03B2\u03B3\""sv);
|
2020-01-06 18:21:16 +00:00
|
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
|
// toml/pull/709 (\xHH unicode scalars)
|
|
|
|
|
#if TOML_LANG_UNRELEASED
|
|
|
|
|
parse_expected_value(
|
2020-04-06 12:57:49 +00:00
|
|
|
|
FILE_LINE_ARGS,
|
2020-04-02 21:39:21 +00:00
|
|
|
|
R"("\x00\x10\x20\x30\x40\x50\x60\x70\x80\x90\x11\xFF\xEE")"sv,
|
2020-07-18 12:10:19 +00:00
|
|
|
|
"\u0000\u0010\u0020\u0030\u0040\u0050\u0060\u0070\u0080\u0090\u0011\u00FF\u00EE"sv);
|
2020-03-12 15:23:25 +00:00
|
|
|
|
#else
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(str = "\x00\x10\x20\x30\x40\x50\x60\x70\x80\x90\x11\xFF\xEE")"sv);
|
2020-03-12 15:23:25 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
|
// check 8-digit \U scalars with insufficient digits
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U1234567")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U123456")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U12345")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U1234")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U123")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U12")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\U1")"sv);
|
2020-03-12 15:23:25 +00:00
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
|
// check 4-digit \u scalars with insufficient digits
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\u123")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\u12")"sv);
|
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS,R"(str = "\u1")"sv);
|
2020-03-12 15:23:25 +00:00
|
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
|
// check 2-digit \x scalars with insufficient digits
|
2020-04-06 12:57:49 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(str = "\x1")"sv);
|
2020-04-13 20:23:11 +00:00
|
|
|
|
|
|
|
|
|
// ML string examples from https://github.com/toml-lang/toml/issues/725
|
2020-07-18 12:10:19 +00:00
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """ """ )"sv, R"( )"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """ """" )"sv, R"( ")"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """ """"" )"sv, R"( "")"sv);
|
2020-04-13 20:23:11 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(v= """ """""" )"sv);
|
2020-07-18 12:10:19 +00:00
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( ''' ''' )"sv, R"( )"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( ''' '''' )"sv, R"( ')"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( ''' ''''' )"sv, R"( '')"sv);
|
2020-04-13 20:23:11 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(v= ''' '''''' )"sv);
|
2020-07-18 12:10:19 +00:00
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """""" )"sv, R"()"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """" """ )"sv, R"(" )"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """"" """ )"sv, R"("" )"sv);
|
2020-04-13 20:23:11 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(v= """""" """ )"sv);
|
2020-07-18 12:10:19 +00:00
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( '''''' )"sv, R"()"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( '''' ''' )"sv, R"(' )"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( ''''' ''' )"sv, R"('' )"sv);
|
2020-04-13 20:23:11 +00:00
|
|
|
|
parsing_should_fail(FILE_LINE_ARGS, R"(v= '''''' ''' )"sv);
|
2020-07-18 12:10:19 +00:00
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """""\"""""" )"sv, R"(""""")"sv);
|
|
|
|
|
parse_expected_value(FILE_LINE_ARGS, R"( """""\"""\"""""" )"sv, R"("""""""")"sv);
|
2020-01-04 14:21:38 +00:00
|
|
|
|
}
|