diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-01-13 17:31:00 +0330 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-01-14 11:35:40 +0330 |
commit | 9de33629daa95a0bb175824d189dc8ae1f65a77d (patch) | |
tree | 7ea32768430d167da75ea99858419b031e700c3e /Tests/AK/TestVariant.cpp | |
parent | d55c130df5a3137f044220a6ee18a9e4e246bddc (diff) | |
download | serenity-9de33629daa95a0bb175824d189dc8ae1f65a77d.zip |
AK+Everywhere: Make Variant::visit() respect the Variant's constness
...and fix all the instances of visit() taking non-const arguments.
Diffstat (limited to 'Tests/AK/TestVariant.cpp')
-rw-r--r-- | Tests/AK/TestVariant.cpp | 37 |
1 files changed, 25 insertions, 12 deletions
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<int, String, float> 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<int, String> 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<int, String, float> the_value { 42.0f }; auto& value = the_value.visit( - [&](const int&) -> RefPtr<Object>& { return ref; }, - [&](const String&) -> RefPtr<Object>& { return ref; }, - [&](const float&) -> RefPtr<Object>& { return ref; }); + [&](int const&) -> RefPtr<Object>& { return ref; }, + [&](String const&) -> RefPtr<Object>& { return ref; }, + [&](float const&) -> RefPtr<Object>& { return ref; }); EXPECT_EQ(ref, value); EXPECT_EQ(ref->ref_count(), 1u); |