sled/src/any_test.cc
tqcq 3ca77d457b
Some checks failed
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m21s
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 21m45s
feat add attributes
2024-03-13 22:09:48 +08:00

49 lines
1.2 KiB
C++

#include <gtest/gtest.h>
#include <sled/any.h>
#include <sled/log/log.h>
TEST(Any, Assign)
{
sled::Any any1;
sled::Any any2(any1);
sled::Any any3(1);
sled::Any any4(any3);
sled::Any any5 = 1;
EXPECT_FALSE(any1.HasValue());
EXPECT_FALSE(any2.HasValue());
EXPECT_TRUE(any3.HasValue());
EXPECT_TRUE(any4.HasValue());
EXPECT_TRUE(any5.HasValue());
EXPECT_EQ(any3.Cast<int>(), 1);
EXPECT_EQ(any4.Cast<int>(), 1);
EXPECT_EQ(any5.Cast<int>(), 1);
EXPECT_EQ(any3.CastOr<std::string>("def"), "def");
EXPECT_EQ(any4.CastOr<std::string>("def"), "def");
EXPECT_EQ(any5.CastOr<std::string>("def"), "def");
EXPECT_EQ(any3.CastOr<int>(11), 1);
}
TEST(Any, std_swap)
{
sled::Any a;
sled::Any b = 2;
EXPECT_FALSE(a.HasValue());
EXPECT_TRUE(b.HasValue());
std::swap(a, b);
EXPECT_TRUE(a.HasValue());
EXPECT_FALSE(b.HasValue());
EXPECT_EQ(a.Cast<int>(), 2);
}
TEST(Any, custom_swap)
{
sled::Any a;
sled::Any b = 2;
EXPECT_FALSE(a.HasValue());
EXPECT_TRUE(b.HasValue());
a.swap(b);
EXPECT_TRUE(a.HasValue());
EXPECT_FALSE(b.HasValue());
EXPECT_EQ(a.Cast<int>(), 2);
}