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"
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
bool parsing_should_succeed(std::string_view test_file,
|
|
|
|
uint32_t test_line,
|
|
|
|
std::string_view toml_str,
|
|
|
|
pss_func&& func,
|
|
|
|
std::string_view source_path)
|
2020-06-28 22:57:59 +00:00
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
INFO("["sv << test_file << ", line "sv << test_line << "] "sv
|
|
|
|
<< "parsing_should_succeed(\""sv << toml_str << "\")"sv)
|
2020-06-28 22:57:59 +00:00
|
|
|
|
|
|
|
constexpr auto validate_table = [](table&& tabl, std::string_view path) -> table&&
|
|
|
|
{
|
|
|
|
INFO("Validating table source information"sv)
|
|
|
|
CHECK(tabl.source().begin != source_position{});
|
|
|
|
CHECK(tabl.source().end != source_position{});
|
|
|
|
if (path.empty())
|
|
|
|
CHECK(tabl.source().path == nullptr);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
REQUIRE(tabl.source().path != nullptr);
|
|
|
|
CHECK(*tabl.source().path == path);
|
|
|
|
}
|
|
|
|
return std::move(tabl);
|
|
|
|
};
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
#if TOML_EXCEPTIONS
|
2020-06-28 22:57:59 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
INFO("Parsing string directly"sv)
|
|
|
|
if (func)
|
|
|
|
func(validate_table(toml::parse(toml_str, source_path), source_path));
|
|
|
|
else
|
|
|
|
validate_table(toml::parse(toml_str, source_path), source_path);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
INFO("Parsing from a string stream"sv)
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.write(toml_str.data(), static_cast<std::streamsize>(toml_str.length()));
|
|
|
|
if (func)
|
|
|
|
func(validate_table(toml::parse(ss, source_path), source_path));
|
|
|
|
else
|
|
|
|
validate_table(toml::parse(ss, source_path), source_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const parse_error& err)
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Parse error on line "sv << err.source().begin.line << ", column "sv << err.source().begin.column
|
|
|
|
<< ":\n"sv << err.description());
|
2020-06-28 22:57:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
#else
|
2020-06-28 22:57:59 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
INFO("Parsing string directly"sv)
|
|
|
|
parse_result result = toml::parse(toml_str, source_path);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
if (func)
|
|
|
|
func(validate_table(std::move(result), source_path));
|
|
|
|
else
|
|
|
|
validate_table(std::move(result), source_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Parse error on line "sv << result.error().source().begin.line << ", column "sv
|
|
|
|
<< result.error().source().begin.column << ":\n"sv
|
|
|
|
<< result.error().description());
|
2020-06-28 22:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
INFO("Parsing from a string stream"sv)
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.write(toml_str.data(), static_cast<std::streamsize>(toml_str.length()));
|
|
|
|
parse_result result = toml::parse(ss, source_path);
|
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
if (func)
|
|
|
|
func(validate_table(std::move(result), source_path));
|
|
|
|
else
|
|
|
|
validate_table(std::move(result), source_path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Parse error on line "sv << result.error().source().begin.line << ", column "sv
|
|
|
|
<< result.error().source().begin.column << ":\n"sv
|
|
|
|
<< result.error().description());
|
2020-06-28 22:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
#endif
|
2020-06-28 22:57:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
bool parsing_should_fail(std::string_view test_file,
|
|
|
|
uint32_t test_line,
|
|
|
|
std::string_view toml_str,
|
|
|
|
source_index expected_failure_line,
|
|
|
|
source_index expected_failure_column)
|
2020-06-28 22:57:59 +00:00
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
INFO("["sv << test_file << ", line "sv << test_line << "] "sv
|
|
|
|
<< "parsing_should_fail(\""sv << toml_str << "\")"sv)
|
2020-06-28 22:57:59 +00:00
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
#if TOML_EXCEPTIONS
|
2020-06-28 22:57:59 +00:00
|
|
|
|
2021-09-04 09:24:47 +00:00
|
|
|
static constexpr auto run_tests = [](source_index ex_line, source_index ex_col, auto&& fn)
|
2020-06-28 22:57:59 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
fn();
|
|
|
|
}
|
2021-09-04 09:24:47 +00:00
|
|
|
catch (const parse_error& err)
|
2020-06-28 22:57:59 +00:00
|
|
|
{
|
2021-09-04 09:24:47 +00:00
|
|
|
if (ex_line != static_cast<source_index>(-1) && err.source().begin.line != ex_line)
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Expected parse_error at line "sv << ex_line << ", actually occured at line "sv
|
|
|
|
<< err.source().begin.line);
|
2021-09-04 09:24:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ex_col != static_cast<source_index>(-1) && err.source().begin.column != ex_col)
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Expected parse_error at column "sv << ex_col << ", actually occured at column "sv
|
|
|
|
<< err.source().begin.column);
|
2021-09-04 09:24:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-28 22:57:59 +00:00
|
|
|
SUCCEED("parse_error thrown OK"sv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
FORCE_FAIL("Expected parsing failure, saw exception: "sv << exc.what());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
FORCE_FAIL("Expected parsing failure, saw unspecified exception"sv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_FAIL("Expected parsing failure"sv);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
auto result = run_tests(expected_failure_line,
|
|
|
|
expected_failure_column,
|
|
|
|
[=]() { [[maybe_unused]] auto res = toml::parse(toml_str); });
|
|
|
|
result = result
|
|
|
|
&& run_tests(expected_failure_line,
|
|
|
|
expected_failure_column,
|
|
|
|
[=]()
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.write(toml_str.data(), static_cast<std::streamsize>(toml_str.length()));
|
|
|
|
[[maybe_unused]] auto res = toml::parse(ss);
|
|
|
|
});
|
2020-06-28 22:57:59 +00:00
|
|
|
return result;
|
|
|
|
|
2021-10-26 13:49:23 +00:00
|
|
|
#else
|
2020-06-28 22:57:59 +00:00
|
|
|
|
2021-09-04 09:24:47 +00:00
|
|
|
static constexpr auto run_tests = [](source_index ex_line, source_index ex_col, auto&& fn)
|
2020-06-28 22:57:59 +00:00
|
|
|
{
|
|
|
|
if (parse_result result = fn(); !result)
|
|
|
|
{
|
2021-09-04 09:24:47 +00:00
|
|
|
if (ex_line != static_cast<source_index>(-1) && result.error().source().begin.line != ex_line)
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Expected parse_error at line "sv << ex_line << ", actually occured at line "sv
|
|
|
|
<< result.error().source().begin.line);
|
2021-09-04 09:24:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ex_col != static_cast<source_index>(-1) && result.error().source().begin.column != ex_col)
|
|
|
|
{
|
2021-10-26 13:49:23 +00:00
|
|
|
FORCE_FAIL("Expected parse_error at column "sv << ex_col << ", actually occured at column "sv
|
|
|
|
<< result.error().source().begin.column);
|
2021-09-04 09:24:47 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 22:57:59 +00:00
|
|
|
SUCCEED("parse_error generated OK"sv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_FAIL("Expected parsing failure"sv);
|
|
|
|
};
|
|
|
|
|
2021-09-04 09:24:47 +00:00
|
|
|
return run_tests(expected_failure_line, expected_failure_column, [=]() { return toml::parse(toml_str); })
|
2021-10-26 13:49:23 +00:00
|
|
|
&& run_tests(expected_failure_line,
|
|
|
|
expected_failure_column,
|
|
|
|
[=]()
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss.write(toml_str.data(), static_cast<std::streamsize>(toml_str.length()));
|
|
|
|
return toml::parse(ss);
|
|
|
|
});
|
|
|
|
|
|
|
|
#endif
|
2020-06-28 22:57:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 11:17:52 +00:00
|
|
|
template bool parse_expected_value(std::string_view, uint32_t, std::string_view, const int&);
|
|
|
|
template bool parse_expected_value(std::string_view, uint32_t, std::string_view, const unsigned int&);
|
|
|
|
template bool parse_expected_value(std::string_view, uint32_t, std::string_view, const bool&);
|
|
|
|
template bool parse_expected_value(std::string_view, uint32_t, std::string_view, const float&);
|
|
|
|
template bool parse_expected_value(std::string_view, uint32_t, std::string_view, const double&);
|
2020-07-18 12:10:19 +00:00
|
|
|
template bool parse_expected_value(std::string_view, uint32_t, std::string_view, const std::string_view&);
|
2020-03-03 08:10:07 +00:00
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
namespace std
|
2020-03-03 08:10:07 +00:00
|
|
|
{
|
2020-04-02 21:39:21 +00:00
|
|
|
template class unique_ptr<const Catch::IExceptionTranslator>;
|
2020-03-03 08:10:07 +00:00
|
|
|
}
|
|
|
|
namespace Catch
|
|
|
|
{
|
|
|
|
template struct StringMaker<node_view<node>>;
|
|
|
|
template struct StringMaker<node_view<const node>>;
|
2021-10-26 13:49:23 +00:00
|
|
|
template ReusableStringStream& ReusableStringStream::operator<<(toml::node_view<toml::node> const&);
|
|
|
|
template ReusableStringStream& ReusableStringStream::operator<<(toml::node_view<const toml::node> const&);
|
2020-03-03 08:10:07 +00:00
|
|
|
namespace Detail
|
|
|
|
{
|
|
|
|
template std::string stringify(const node_view<node>&);
|
|
|
|
template std::string stringify(const node_view<const node>&);
|
|
|
|
}
|
|
|
|
}
|