summaryrefslogtreecommitdiff
path: root/tomlplusplus/tests/visit.cpp
diff options
context:
space:
mode:
authorMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 18:44:05 +0300
committerMehmet Samet Duman <yongdohyun@projecttick.org>2026-04-02 18:44:05 +0300
commit0b24459ac12b6cf9fd5a401d647796ca254a8fa8 (patch)
treef2fd66e2476976a51e2a51330fd95dc6e87b24c1 /tomlplusplus/tests/visit.cpp
parentb85e90fc3480da0e6a48da73201a0b22488cc650 (diff)
parent1c8b7466e4946fcc3bf20484c0e1d001202cca5a (diff)
downloadProject-Tick-0b24459ac12b6cf9fd5a401d647796ca254a8fa8.tar.gz
Project-Tick-0b24459ac12b6cf9fd5a401d647796ca254a8fa8.zip
Add 'tomlplusplus/' from commit '1c8b7466e4946fcc3bf20484c0e1d001202cca5a'
git-subtree-dir: tomlplusplus git-subtree-mainline: b85e90fc3480da0e6a48da73201a0b22488cc650 git-subtree-split: 1c8b7466e4946fcc3bf20484c0e1d001202cca5a
Diffstat (limited to 'tomlplusplus/tests/visit.cpp')
-rw-r--r--tomlplusplus/tests/visit.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/tomlplusplus/tests/visit.cpp b/tomlplusplus/tests/visit.cpp
new file mode 100644
index 0000000000..efc3c5f30e
--- /dev/null
+++ b/tomlplusplus/tests/visit.cpp
@@ -0,0 +1,139 @@
+// This file is a part of toml++ and is subject to the the terms of the MIT license.
+// Copyright (c) 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.hpp"
+TOML_DISABLE_SPAM_WARNINGS;
+
+TEST_CASE("node::visit")
+{
+ value<int64_t> val{ 3 };
+
+ // check lvalue propagates correctly
+ static_cast<node&>(val).visit(
+ [](auto&& v) noexcept
+ {
+ using val_ref_type = decltype(v);
+ static_assert(std::is_lvalue_reference_v<val_ref_type>);
+
+ using val_type = std::remove_reference_t<val_ref_type>;
+ static_assert(!std::is_const_v<val_type>);
+ static_assert(!std::is_volatile_v<val_type>);
+ });
+
+ // check rvalue propagates correctly
+ static_cast<node&&>(val).visit(
+ [](auto&& v) noexcept
+ {
+ using val_ref_type = decltype(v);
+ static_assert(std::is_rvalue_reference_v<val_ref_type>);
+
+ using val_type = std::remove_reference_t<val_ref_type>;
+ static_assert(!std::is_const_v<val_type>);
+ static_assert(!std::is_volatile_v<val_type>);
+ });
+
+ // check const lvalue propagates correctly
+ static_cast<const node&>(val).visit(
+ [](auto&& v) noexcept
+ {
+ using val_ref_type = decltype(v);
+ static_assert(std::is_lvalue_reference_v<val_ref_type>);
+
+ using val_type = std::remove_reference_t<val_ref_type>;
+ static_assert(std::is_const_v<val_type>);
+ static_assert(!std::is_volatile_v<val_type>);
+ });
+
+ // check const rvalue propagates correctly
+ static_cast<const node&&>(val).visit(
+ [](auto&& v) noexcept
+ {
+ using val_ref_type = decltype(v);
+ static_assert(std::is_rvalue_reference_v<val_ref_type>);
+
+ using val_type = std::remove_reference_t<val_ref_type>;
+ static_assert(std::is_const_v<val_type>);
+ static_assert(!std::is_volatile_v<val_type>);
+ });
+
+ // check noexcept
+ static constexpr auto throwing_visitor = [](auto&&) noexcept(false) {};
+ static constexpr auto non_throwing_visitor = [](auto&&) noexcept(true) {};
+ static_assert(!noexcept(static_cast<node&>(val).visit(throwing_visitor)));
+ static_assert(!noexcept(static_cast<node&&>(val).visit(throwing_visitor)));
+ static_assert(!noexcept(static_cast<const node&>(val).visit(throwing_visitor)));
+ static_assert(!noexcept(static_cast<const node&&>(val).visit(throwing_visitor)));
+ static_assert(noexcept(static_cast<node&>(val).visit(non_throwing_visitor)));
+ static_assert(noexcept(static_cast<node&&>(val).visit(non_throwing_visitor)));
+ static_assert(noexcept(static_cast<const node&>(val).visit(non_throwing_visitor)));
+ static_assert(noexcept(static_cast<const node&&>(val).visit(non_throwing_visitor)));
+
+ // check return
+ static constexpr auto returns_boolean = [](auto& v) noexcept { return toml::is_integer<decltype(v)>; };
+ auto return_test = static_cast<node&>(val).visit(returns_boolean);
+ static_assert(std::is_same_v<decltype(return_test), bool>);
+ CHECK(return_test == true);
+}
+
+TEST_CASE("node_view::visit")
+{
+ value<int64_t> val{ 3 };
+
+ auto view = node_view{ val };
+ auto cview = node_view{ std::as_const(val) };
+ static_assert(!std::is_same_v<decltype(view), decltype(cview)>);
+
+ // check mutable views propagate correctly
+ view.visit(
+ [](auto&& v) noexcept
+ {
+ using val_ref_type = decltype(v);
+ static_assert(std::is_lvalue_reference_v<val_ref_type>);
+
+ using val_type = std::remove_reference_t<val_ref_type>;
+ static_assert(!std::is_const_v<val_type>);
+ static_assert(!std::is_volatile_v<val_type>);
+ });
+
+ // check const views propagate correctly
+ cview.visit(
+ [](auto&& v) noexcept
+ {
+ using val_ref_type = decltype(v);
+ static_assert(std::is_lvalue_reference_v<val_ref_type>);
+
+ using val_type = std::remove_reference_t<val_ref_type>;
+ static_assert(std::is_const_v<val_type>);
+ static_assert(!std::is_volatile_v<val_type>);
+ });
+
+ // check noexcept
+ static constexpr auto throwing_visitor = [](auto&&) noexcept(false) {};
+ static constexpr auto non_throwing_visitor = [](auto&&) noexcept(true) {};
+ static_assert(!noexcept(view.visit(throwing_visitor)));
+ static_assert(!noexcept(cview.visit(throwing_visitor)));
+ static_assert(noexcept(view.visit(non_throwing_visitor)));
+ static_assert(noexcept(cview.visit(non_throwing_visitor)));
+
+ // check return
+ static constexpr auto returns_boolean = [](auto&& v) noexcept { return toml::is_integer<decltype(v)>; };
+ auto return_test = view.visit(returns_boolean);
+ static_assert(std::is_same_v<decltype(return_test), bool>);
+ CHECK(return_test == true);
+
+ // check that null views don't invoke the visitor
+ // clang-format off
+ auto null_view = decltype(view){};
+ auto null_cview = decltype(cview){};
+ unsigned count{};
+ unsigned mask{};
+ view.visit([&](auto&&) noexcept { count++; mask |= 0b0001u; });
+ cview.visit([&](auto&&) noexcept { count++; mask |= 0b0010u; });
+ null_view.visit([&](auto&&) noexcept { count++; mask |= 0b0100u; });
+ null_cview.visit([&](auto&&) noexcept { count++; mask |= 0b1000u; });
+ CHECK(count == 2u);
+ CHECK(mask == 0b0011u);
+ // clang-format on
+}