tomlplusplus/tests/parsing_arrays.cpp

141 lines
5.4 KiB
C++
Raw Normal View History

new file: .circleci/config.yml new file: .editorconfig new file: .gitattributes new file: .gitignore new file: .gitmodules new file: LICENSE new file: README.md new file: examples/example.cpp new file: examples/example.toml new file: examples/meson.build new file: include/toml++/toml.h new file: include/toml++/toml_array.h new file: include/toml++/toml_common.h new file: include/toml++/toml_formatter.h new file: include/toml++/toml_node.h new file: include/toml++/toml_node_view.h new file: include/toml++/toml_parser.h new file: include/toml++/toml_table.h new file: include/toml++/toml_utf8.h new file: include/toml++/toml_utf8_generated.h new file: include/toml++/toml_value.h new file: meson.build new file: python/ci_single_header_check.py new file: python/generate_single_header.py new file: python/generate_unicode_functions.py new file: tests/catch2 new file: tests/catch2.h new file: tests/lifetimes.cpp new file: tests/main.cpp new file: tests/meson.build new file: tests/parsing_arrays.cpp new file: tests/parsing_booleans.cpp new file: tests/parsing_comments.cpp new file: tests/parsing_dates_and_times.cpp new file: tests/parsing_floats.cpp new file: tests/parsing_integers.cpp new file: tests/parsing_key_value_pairs.cpp new file: tests/parsing_spec_example.cpp new file: tests/parsing_strings.cpp new file: tests/parsing_tables.cpp new file: tests/tests.cpp new file: tests/tests.h new file: toml.hpp new file: vs/.runsettings new file: vs/example.vcxproj new file: vs/test_char.vcxproj new file: vs/test_char8.vcxproj new file: vs/test_char8_noexcept.vcxproj new file: vs/test_char_noexcept.vcxproj new file: vs/test_strict_char.vcxproj new file: vs/test_strict_char8.vcxproj new file: vs/test_strict_char8_noexcept.vcxproj new file: vs/test_strict_char_noexcept.vcxproj new file: vs/toml++.natvis new file: vs/toml++.props new file: vs/toml++.sln new file: vs/toml++.vcxproj new file: vs/toml++.vcxproj.filters
2020-01-04 14:21:38 +00:00
#include "tests.h"
TEST_CASE("parsing - arrays")
new file: .circleci/config.yml new file: .editorconfig new file: .gitattributes new file: .gitignore new file: .gitmodules new file: LICENSE new file: README.md new file: examples/example.cpp new file: examples/example.toml new file: examples/meson.build new file: include/toml++/toml.h new file: include/toml++/toml_array.h new file: include/toml++/toml_common.h new file: include/toml++/toml_formatter.h new file: include/toml++/toml_node.h new file: include/toml++/toml_node_view.h new file: include/toml++/toml_parser.h new file: include/toml++/toml_table.h new file: include/toml++/toml_utf8.h new file: include/toml++/toml_utf8_generated.h new file: include/toml++/toml_value.h new file: meson.build new file: python/ci_single_header_check.py new file: python/generate_single_header.py new file: python/generate_unicode_functions.py new file: tests/catch2 new file: tests/catch2.h new file: tests/lifetimes.cpp new file: tests/main.cpp new file: tests/meson.build new file: tests/parsing_arrays.cpp new file: tests/parsing_booleans.cpp new file: tests/parsing_comments.cpp new file: tests/parsing_dates_and_times.cpp new file: tests/parsing_floats.cpp new file: tests/parsing_integers.cpp new file: tests/parsing_key_value_pairs.cpp new file: tests/parsing_spec_example.cpp new file: tests/parsing_strings.cpp new file: tests/parsing_tables.cpp new file: tests/tests.cpp new file: tests/tests.h new file: toml.hpp new file: vs/.runsettings new file: vs/example.vcxproj new file: vs/test_char.vcxproj new file: vs/test_char8.vcxproj new file: vs/test_char8_noexcept.vcxproj new file: vs/test_char_noexcept.vcxproj new file: vs/test_strict_char.vcxproj new file: vs/test_strict_char8.vcxproj new file: vs/test_strict_char8_noexcept.vcxproj new file: vs/test_strict_char_noexcept.vcxproj new file: vs/toml++.natvis new file: vs/toml++.props new file: vs/toml++.sln new file: vs/toml++.vcxproj new file: vs/toml++.vcxproj.filters
2020-01-04 14:21:38 +00:00
{
parsing_should_succeed(S(R"(
integers = [ 1, 2, 3 ]
integers2 = [
1, 2, 3
]
integers3 = [
1,
2, # this is ok
]
colors = [ "red", "yellow", "green" ]
nested_array_of_int = [ [ 1, 2 ], [3, 4, 5] ]
nested_mixed_array = [ [ 1, 2 ], ["a", "b", "c"] ]
string_array = [ "all", 'strings', """are the same""", '''type''' ]
)"sv),
[](table&& tbl) noexcept
{
REQUIRE(tbl[S("integers")].as<array>());
CHECK(tbl[S("integers")].as<array>()->is_homogeneous());
CHECK(tbl[S("integers")].as<array>()->size() == 3);
CHECK(tbl[S("integers")][0] == 1);
CHECK(tbl[S("integers")][1] == 2);
CHECK(tbl[S("integers")][2] == 3);
REQUIRE(tbl[S("integers2")].as<array>());
CHECK(tbl[S("integers2")].as<array>()->is_homogeneous());
CHECK(tbl[S("integers2")].as<array>()->size() == 3);
CHECK(tbl[S("integers2")][0] == 1);
CHECK(tbl[S("integers2")][1] == 2);
CHECK(tbl[S("integers2")][2] == 3);
REQUIRE(tbl[S("integers3")].as<array>());
CHECK(tbl[S("integers3")].as<array>()->is_homogeneous());
CHECK(tbl[S("integers3")].as<array>()->size() == 2);
CHECK(tbl[S("integers3")][0] == 1);
CHECK(tbl[S("integers3")][1] == 2);
REQUIRE(tbl[S("colors")].as<array>());
CHECK(tbl[S("colors")].as<array>()->is_homogeneous());
CHECK(tbl[S("colors")].as<array>()->size() == 3);
CHECK(tbl[S("colors")][0] == S("red"sv));
CHECK(tbl[S("colors")][1] == S("yellow"sv));
CHECK(tbl[S("colors")][2] == S("green"sv));
REQUIRE(tbl[S("nested_array_of_int")].as<array>());
CHECK(tbl[S("nested_array_of_int")].as<array>()->is_homogeneous());
CHECK(tbl[S("nested_array_of_int")].as<array>()->size() == 2);
REQUIRE(tbl[S("nested_array_of_int")][0].as<array>());
CHECK(tbl[S("nested_array_of_int")][0].as<array>()->is_homogeneous());
CHECK(tbl[S("nested_array_of_int")][0].as<array>()->size() == 2);
CHECK(tbl[S("nested_array_of_int")][0][0] == 1);
CHECK(tbl[S("nested_array_of_int")][0][1] == 2);
REQUIRE(tbl[S("nested_array_of_int")][1].as<array>());
CHECK(tbl[S("nested_array_of_int")][1].as<array>()->is_homogeneous());
CHECK(tbl[S("nested_array_of_int")][1].as<array>()->size() == 3);
CHECK(tbl[S("nested_array_of_int")][1][0] == 3);
CHECK(tbl[S("nested_array_of_int")][1][1] == 4);
CHECK(tbl[S("nested_array_of_int")][1][2] == 5);
REQUIRE(tbl[S("nested_mixed_array")].as<array>());
CHECK(tbl[S("nested_mixed_array")].as<array>()->is_homogeneous());
CHECK(tbl[S("nested_mixed_array")].as<array>()->size() == 2);
REQUIRE(tbl[S("nested_mixed_array")][0].as<array>());
CHECK(tbl[S("nested_mixed_array")][0].as<array>()->is_homogeneous());
CHECK(tbl[S("nested_mixed_array")][0].as<array>()->size() == 2);
CHECK(tbl[S("nested_mixed_array")][0][0] == 1);
CHECK(tbl[S("nested_mixed_array")][0][1] == 2);
REQUIRE(tbl[S("nested_mixed_array")][1].as<array>());
CHECK(tbl[S("nested_mixed_array")][1].as<array>()->is_homogeneous());
CHECK(tbl[S("nested_mixed_array")][1].as<array>()->size() == 3);
CHECK(tbl[S("nested_mixed_array")][1][0] == S("a"sv));
CHECK(tbl[S("nested_mixed_array")][1][1] == S("b"sv));
CHECK(tbl[S("nested_mixed_array")][1][2] == S("c"sv));
REQUIRE(tbl[S("string_array")].as<array>());
CHECK(tbl[S("string_array")].as<array>()->is_homogeneous());
CHECK(tbl[S("string_array")].as<array>()->size() == 4);
CHECK(tbl[S("string_array")][0] == S("all"sv));
CHECK(tbl[S("string_array")][1] == S("strings"sv));
CHECK(tbl[S("string_array")][2] == S("are the same"sv));
CHECK(tbl[S("string_array")][3] == S("type"sv));
REQUIRE(tbl[S("integers")].as<array>());
CHECK(tbl[S("integers")].as<array>()->is_homogeneous());
CHECK(tbl[S("integers")].as<array>()->size() == 3);
CHECK(tbl[S("integers")][0] == 1);
CHECK(tbl[S("integers")][1] == 2);
CHECK(tbl[S("integers")][2] == 3);
}
);
// toml/issues/665 - heterogeneous arrays
new file: .circleci/config.yml new file: .editorconfig new file: .gitattributes new file: .gitignore new file: .gitmodules new file: LICENSE new file: README.md new file: examples/example.cpp new file: examples/example.toml new file: examples/meson.build new file: include/toml++/toml.h new file: include/toml++/toml_array.h new file: include/toml++/toml_common.h new file: include/toml++/toml_formatter.h new file: include/toml++/toml_node.h new file: include/toml++/toml_node_view.h new file: include/toml++/toml_parser.h new file: include/toml++/toml_table.h new file: include/toml++/toml_utf8.h new file: include/toml++/toml_utf8_generated.h new file: include/toml++/toml_value.h new file: meson.build new file: python/ci_single_header_check.py new file: python/generate_single_header.py new file: python/generate_unicode_functions.py new file: tests/catch2 new file: tests/catch2.h new file: tests/lifetimes.cpp new file: tests/main.cpp new file: tests/meson.build new file: tests/parsing_arrays.cpp new file: tests/parsing_booleans.cpp new file: tests/parsing_comments.cpp new file: tests/parsing_dates_and_times.cpp new file: tests/parsing_floats.cpp new file: tests/parsing_integers.cpp new file: tests/parsing_key_value_pairs.cpp new file: tests/parsing_spec_example.cpp new file: tests/parsing_strings.cpp new file: tests/parsing_tables.cpp new file: tests/tests.cpp new file: tests/tests.h new file: toml.hpp new file: vs/.runsettings new file: vs/example.vcxproj new file: vs/test_char.vcxproj new file: vs/test_char8.vcxproj new file: vs/test_char8_noexcept.vcxproj new file: vs/test_char_noexcept.vcxproj new file: vs/test_strict_char.vcxproj new file: vs/test_strict_char8.vcxproj new file: vs/test_strict_char8_noexcept.vcxproj new file: vs/test_strict_char_noexcept.vcxproj new file: vs/toml++.natvis new file: vs/toml++.props new file: vs/toml++.sln new file: vs/toml++.vcxproj new file: vs/toml++.vcxproj.filters
2020-01-04 14:21:38 +00:00
#if TOML_LANG_HIGHER_THAN(0, 5, 0)
parsing_should_succeed(S(R"(
# Mixed-type arrays are allowed
numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
contributors = [
"Foo Bar <foo@example.com>",
{ name = "Baz Qux", email = "bazqux@example.com", url = "https://example.com/bazqux" }
]
)"sv),
[](table&& tbl) noexcept
{
REQUIRE(tbl[S("numbers")].as<array>());
CHECK(!tbl[S("numbers")].as<array>()->is_homogeneous());
CHECK(tbl[S("numbers")].as<array>()->size() == 6);
CHECK(tbl[S("numbers")][0].as<double>());
CHECK(tbl[S("numbers")][1].as<double>());
CHECK(tbl[S("numbers")][2].as<double>());
CHECK(tbl[S("numbers")][3].as<int64_t>());
CHECK(tbl[S("numbers")][4].as<int64_t>());
CHECK(tbl[S("numbers")][5].as<int64_t>());
CHECK(tbl[S("numbers")][0] == 0.1);
CHECK(tbl[S("numbers")][1] == 0.2);
CHECK(tbl[S("numbers")][2] == 0.5);
CHECK(tbl[S("numbers")][3] == 1);
CHECK(tbl[S("numbers")][4] == 2);
CHECK(tbl[S("numbers")][5] == 5);
REQUIRE(tbl[S("contributors")].as<array>());
CHECK(!tbl[S("contributors")].as<array>()->is_homogeneous());
CHECK(tbl[S("contributors")].as<array>()->size() == 2);
CHECK(tbl[S("contributors")][0].as<string>());
CHECK(tbl[S("contributors")][1].as<table>());
CHECK(tbl[S("contributors")][0] == S("Foo Bar <foo@example.com>"sv));
CHECK(tbl[S("contributors")][1][S("name")] == S("Baz Qux"sv));
CHECK(tbl[S("contributors")][1][S("email")] == S("bazqux@example.com"sv));
CHECK(tbl[S("contributors")][1][S("url")] == S("https://example.com/bazqux"sv));
}
);
#else
parsing_should_fail(S("numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]"sv));
#endif
}