mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:37:35 +00:00
AK+Everywhere: Make Variant::visit() respect the Variant's constness
...and fix all the instances of visit() taking non-const arguments.
This commit is contained in:
parent
d55c130df5
commit
9de33629da
7 changed files with 44 additions and 31 deletions
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue