forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_internal_test.cc
More file actions
117 lines (96 loc) · 3.72 KB
/
Copy pathvalue_internal_test.cc
File metadata and controls
117 lines (96 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "internal/value_internal.h"
#include "gtest/gtest.h"
#include "absl/memory/memory.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "testutil/util.h"
namespace google {
namespace api {
namespace expr {
namespace internal {
using testutil::ExpectSameType;
struct RefOnlyType : public RefCountable {
public:
RefOnlyType() = default;
RefOnlyType(const RefOnlyType&) = delete;
RefOnlyType(RefOnlyType&&) = delete;
RefOnlyType& operator=(const RefOnlyType&) = delete;
RefOnlyType& operator=(RefOnlyType&&) = delete;
bool operator==(const RefOnlyType& rhs) const { return this == &rhs; }
};
class ValueAdapterTest : public ::testing::Test {
public:
using ValueData = BaseValue::ValueData;
using ValueAdapter = BaseValue::ValueAdapter;
template <typename... Types>
using BaseTypeHelper = BaseValue::BaseTypeHelper<Types...>;
template <typename T>
using TypeHelper = BaseValue::TypeHelper<T>;
using UnownedStr = BaseValue::UnownedStr;
using OwnedStr = BaseValue::OwnedStr;
template <typename ExpectedType, typename T, typename E>
void TestAdapter(T&& value, E&& expected) {
// Verify that the adapter produces the expected type.
ExpectSameType<ExpectedType,
decltype(
MaybeAdapt(ValueAdapter(), std::forward<T>(value)))>();
EXPECT_EQ(expected, MaybeAdapt(ValueAdapter(), std::forward<T>(value)));
}
template <typename ExpectedType, typename T, typename E>
void TestValueAdapter(T&& value, E&& expected) {
ExpectSameType<ExpectedType,
decltype(ValueAdapter()(std::forward<T>(value)))>();
EXPECT_EQ(expected, ValueAdapter()(value));
TestAdapter<ExpectedType>(std::forward<T>(value),
std::forward<E>(expected));
}
};
TEST_F(ValueAdapterTest, Bool) {
ExpectSameType<BaseTypeHelper<bool>, BaseTypeHelper<bool, bool>>();
ExpectSameType<const bool*, decltype(BaseTypeHelper<bool>::get_if(
inst_of<const ValueData*>()))>();
ExpectSameType<const bool*, decltype(TypeHelper<bool>::get_if(
inst_of<const ValueData*>()))>();
static_assert(!is_numeric<bool>::value, "blah");
}
TEST_F(ValueAdapterTest, NullPtr) {
// nullptr passes through by value, unchanged
TestValueAdapter<std::nullptr_t&&>(nullptr, nullptr);
}
TEST_F(ValueAdapterTest, RefPtr) {
// Smart pointer is dereferenced.
auto ptr = RefPtrHolder<RefOnlyType>(new RefOnlyType());
TestAdapter<RefOnlyType&>(ptr, *ptr);
auto cptr = RefPtrHolder<const RefOnlyType>(new RefOnlyType());
TestAdapter<const RefOnlyType&>(cptr, *cptr);
}
TEST_F(ValueAdapterTest, RefCopy) {
auto i = RefCopyHolder<int>(1);
TestAdapter<int&>(i, 1);
const auto& const_ptr_int = i;
TestAdapter<const int&>(const_ptr_int, 1);
auto ptr_const_int = RefCopyHolder<const int>(2);
TestAdapter<const int&>(ptr_const_int, 2);
}
TEST_F(ValueAdapterTest, String) {
// Strings are normalized to string_view.
absl::string_view view("hi");
TestAdapter<absl::string_view>(view, "hi");
std::string value = "hi";
TestAdapter<absl::string_view>(value, "hi");
const std::string& cvalue = value;
TestAdapter<absl::string_view>(cvalue, "hi");
UnownedStr unowned(cvalue);
TestAdapter<absl::string_view>(unowned, "hi");
using ParentOwnedStrPolicy = Ref<ParentOwned<ReffedPtr<RefOnlyType>, Copy>>;
using ParentOwnedStr = Holder<absl::string_view, ParentOwnedStrPolicy>;
auto parent = MakeReffed<RefOnlyType>();
ParentOwnedStr parent_owned(parent, cvalue);
TestValueAdapter<absl::string_view>(parent_owned, "hi");
OwnedStr owned("hi");
TestAdapter<absl::string_view>(owned, "hi");
}
} // namespace internal
} // namespace expr
} // namespace api
} // namespace google