From 9de33629daa95a0bb175824d189dc8ae1f65a77d Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 13 Jan 2022 17:31:00 +0330 Subject: AK+Everywhere: Make Variant::visit() respect the Variant's constness ...and fix all the instances of visit() taking non-const arguments. --- Tests/AK/TestVariant.cpp | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'Tests/AK/TestVariant.cpp') diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp index f05c629022..af87669671 100644 --- a/Tests/AK/TestVariant.cpp +++ b/Tests/AK/TestVariant.cpp @@ -31,9 +31,22 @@ TEST_CASE(visit) bool correct = false; Variant the_value { 42.0f }; the_value.visit( - [&](const int&) { correct = false; }, - [&](const String&) { correct = false; }, - [&](const float&) { correct = true; }); + [&](int const&) { correct = false; }, + [&](String const&) { correct = false; }, + [&](float const&) { correct = true; }); + EXPECT(correct); +} + +TEST_CASE(visit_const) +{ + bool correct = false; + Variant const the_value { "42"sv }; + + the_value.visit( + [&](String const&) { correct = true; }, + [&](auto&) {}, + [&](auto const&) {}); + EXPECT(correct); } @@ -139,9 +152,9 @@ TEST_CASE(return_values) MyVariant the_value { 42.0f }; float value = the_value.visit( - [&](const int&) { return 1.0f; }, - [&](const String&) { return 2.0f; }, - [&](const float& f) { return f; }); + [&](int const&) { return 1.0f; }, + [&](String const&) { return 2.0f; }, + [&](float const& f) { return f; }); EXPECT_EQ(value, 42.0f); } { @@ -157,9 +170,9 @@ TEST_CASE(return_values) const MyVariant the_value { "str" }; String value = the_value.visit( - [&](const int&) { return String { "wrong" }; }, - [&](const String& s) { return s; }, - [&](const float&) { return String { "wrong" }; }); + [&](int const&) { return String { "wrong" }; }, + [&](String const& s) { return s; }, + [&](float const&) { return String { "wrong" }; }); EXPECT_EQ(value, "str"); } } @@ -170,9 +183,9 @@ TEST_CASE(return_values_by_reference) Variant the_value { 42.0f }; auto& value = the_value.visit( - [&](const int&) -> RefPtr& { return ref; }, - [&](const String&) -> RefPtr& { return ref; }, - [&](const float&) -> RefPtr& { return ref; }); + [&](int const&) -> RefPtr& { return ref; }, + [&](String const&) -> RefPtr& { return ref; }, + [&](float const&) -> RefPtr& { return ref; }); EXPECT_EQ(ref, value); EXPECT_EQ(ref->ref_count(), 1u); -- cgit v1.2.3