summaryrefslogtreecommitdiff
path: root/Tests
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-01 10:12:53 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-01 11:38:17 +0200
commitf3fda59abd44fc34bbb07a6d6dc18cd23ab02c22 (patch)
treea32aae6ba8fe65ff04237db7a85f7c1752a601c6 /Tests
parent12a42edd13f6bd2def0ff045389ab289fbcb0b83 (diff)
downloadserenity-f3fda59abd44fc34bbb07a6d6dc18cd23ab02c22.zip
AK: Enable direct comparsion of Optional<T> and T
This patch introduces a new operator== to compare an Optional to its contained type directly. If the Optional does not contain a value, the comparison will always return false. This also adds a test case for the new behavior as well as comparison between Optional objects themselves.
Diffstat (limited to 'Tests')
-rw-r--r--Tests/AK/TestOptional.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/Tests/AK/TestOptional.cpp b/Tests/AK/TestOptional.cpp
index 583d11f0eb..427046eab8 100644
--- a/Tests/AK/TestOptional.cpp
+++ b/Tests/AK/TestOptional.cpp
@@ -53,3 +53,46 @@ TEST_CASE(short_notation)
EXPECT_EQ(value->length(), 3u);
EXPECT_EQ(*value, "foo");
}
+
+TEST_CASE(comparison_without_values)
+{
+ Optional<StringView> opt0;
+ Optional<StringView> opt1;
+ Optional<String> opt2;
+ EXPECT_EQ(opt0, opt1);
+ EXPECT_EQ(opt0, opt2);
+}
+
+TEST_CASE(comparison_with_values)
+{
+ Optional<StringView> opt0;
+ Optional<StringView> opt1 = "foo";
+ Optional<String> opt2 = "foo";
+ Optional<StringView> opt3 = "bar";
+ EXPECT_NE(opt0, opt1);
+ EXPECT_EQ(opt1, opt2);
+ EXPECT_NE(opt1, opt3);
+}
+
+TEST_CASE(comparison_to_underlying_types)
+{
+ Optional<String> opt0;
+ EXPECT_NE(opt0, String());
+ EXPECT_NE(opt0, "foo");
+
+ Optional<StringView> opt1 = "foo";
+ EXPECT_EQ(opt1, "foo");
+ EXPECT_NE(opt1, "bar");
+ EXPECT_EQ(opt1, String("foo"));
+}
+
+TEST_CASE(comparison_with_numeric_types)
+{
+ Optional<u8> opt0;
+ EXPECT_NE(opt0, 0);
+ Optional<u8> opt1 = 7;
+ EXPECT_EQ(opt1, 7);
+ EXPECT_EQ(opt1, 7.0);
+ EXPECT_EQ(opt1, 7u);
+ EXPECT_NE(opt1, -2);
+}