tomlplusplus/tests/windows_compat.cpp
Mark Gillard de07ba7187 string value serialization now emits literals where possible (closes #43)
also added support for wide strings on Windows (closes #42):
- added wide-string path arg overloads of `parse()` and `parse_file()`
- added wide-string support to all relevant `table` and `array` ops
- added `std::wstring` support to `node::value()` and `node::value_or()`
- added `std::wstring` support to `node_view::value()` and `node_view::value_or()`
- added wide-string overloads of `table::operator[]`
- added wide-string overloads of `node_view::operator[]`
- added `source_region::wide_path()`
- added `TOML_WINDOWS_COMPAT` switch for explicitly enabling/disabling this stuff

also:
- fixed internal macro `assert_or_assume` leaking out of `toml_parser.hpp`
- deprecated `node_view::get()` in favour of `node_view::node()`
- minor documentation fixes
- minor cleanup
2020-07-14 20:50:24 +03:00

78 lines
2.6 KiB
C++

// This file is a part of toml++ and is subject to the the terms of the MIT license.
// Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT
#include "tests.h"
#if TOML_WINDOWS_COMPAT
TEST_CASE("windows compat")
{
static constexpr auto toml_text = R"(
[library]
name = "toml++"
authors = ["Mark Gillard <mark.gillard@outlook.com.au>"]
[dependencies]
cpp = 17
)"sv;
auto res = toml::parse(toml_text, L"kek.toml");
#if !TOML_EXCEPTIONS
REQUIRE(res.succeeded());
#endif
toml::table& tbl = res;
// source paths
REQUIRE(tbl.source().path != nullptr);
CHECK(*tbl.source().path == "kek.toml"sv);
CHECK(tbl.source().wide_path().has_value());
CHECK(tbl.source().wide_path().value() == L"kek.toml"sv);
// direct lookups from tables
REQUIRE(tbl.get(S("library")) != nullptr);
CHECK(tbl.get(S("library")) == tbl.get(S("library"sv)));
CHECK(tbl.get(S("library")) == tbl.get(S("library"s)));
CHECK(tbl.get(L"library") != nullptr);
CHECK(tbl.get(L"library") == tbl.get(L"library"sv));
CHECK(tbl.get(L"library") == tbl.get(L"library"s));
CHECK(tbl.get(L"library") == tbl.get(S("library")));
// node-view lookups
CHECK(tbl[L"library"].node() != nullptr);
CHECK(tbl[L"library"].node() == tbl.get(L"library"));
// value queries
REQUIRE(tbl[L"library"][L"name"].as_string() != nullptr);
CHECK(tbl[L"library"][L"name"].value<std::wstring>() == L"toml++"s);
CHECK(tbl[L"library"][L"name"].value_or(L""sv) == L"toml++"s);
CHECK(tbl[L"library"][L"name"].value_or(L""s) == L"toml++"s);
CHECK(tbl[L"library"][L"name"].value_or(L"") == L"toml++"s);
// node-view comparisons
CHECK(tbl[L"library"][L"name"] == S("toml++"sv));
CHECK(tbl[L"library"][L"name"] == S("toml++"s));
CHECK(tbl[L"library"][L"name"] == S("toml++"));
CHECK(tbl[L"library"][L"name"] == L"toml++"sv);
CHECK(tbl[L"library"][L"name"] == L"toml++"s);
CHECK(tbl[L"library"][L"name"] == L"toml++");
// table manipulation
tbl.insert(L"foo", L"bar");
REQUIRE(tbl.contains(S("foo")));
REQUIRE(tbl.contains(L"foo"));
CHECK(tbl[S("foo")] == S("bar"));
tbl.insert_or_assign(L"foo", L"kek");
CHECK(tbl[S("foo")] == S("kek"));
tbl.erase(L"foo");
REQUIRE(!tbl.contains(S("foo")));
REQUIRE(!tbl.contains(L"foo"));
}
static_assert(std::is_same_v<std::wstring, decltype(std::declval<toml::node>().value_or(L""s))>);
static_assert(std::is_same_v<std::wstring, decltype(std::declval<toml::node>().value_or(L""sv))>);
static_assert(std::is_same_v<std::wstring, decltype(std::declval<toml::node>().value_or(L""))>);
#endif // TOML_WINDOWS_COMPAT