1#include <gtest/gtest.h>
5#include <unordered_map>
8#include <userver/proto-structs/any.hpp>
9#include <userver/proto-structs/io/std/chrono/duration.hpp>
10#include <userver/proto-structs/io/userver/utils/box.hpp>
11#include <userver/proto-structs/io/userver/utils/strong_typedef.hpp>
12#include <userver/proto-structs/oneof.hpp>
13#include <userver/utest/assert_macros.hpp>
14#include <userver/utils/box.hpp>
15#include <userver/utils/strong_typedef.hpp>
17#include "messages.pb.h"
20USERVER_NAMESPACE_BEGIN
22namespace proto_structs::tests {
30using RawOneof = proto_structs::Oneof<int32_t, int32_t, std::string, TestEnum, structs::Simple>;
32struct CustomOneof : RawOneof {};
34void CheckAlternativeSet(
const RawOneof& oneof, std::size_t set_index) {
35 ASSERT_EQ(oneof.GetIndex(), set_index);
39 for (std::size_t i = 0; i < RawOneof::kSize; ++i) {
41 ASSERT_FALSE(oneof.Contains(i));
43 ASSERT_TRUE(oneof.Contains(i));
48 ASSERT_EQ(oneof.ContainsAny(), found);
57TEST(OneofTest, Traits) {
60 static_assert(traits::Oneof<RawOneof>);
61 static_assert(traits::Oneof<
const volatile RawOneof>);
62 static_assert(traits::Oneof<CustomOneof>);
63 static_assert(traits::Oneof<
const volatile CustomOneof>);
65 static_assert(RawOneof::kSize == 5);
66 static_assert(CustomOneof::kSize == 5);
68 static_assert(!traits::Oneof<RawOneof&>);
69 static_assert(!traits::Oneof<CustomOneof&>);
70 static_assert(!traits::Oneof<int32_t>);
71 static_assert(!traits::Oneof<std::pair<int32_t, int32_t>>);
73 static_assert(std::is_same_v<int32_t, OneofAlternativeType<0, RawOneof>>);
74 static_assert(std::is_same_v<int32_t, OneofAlternativeType<1, RawOneof>>);
75 static_assert(std::is_same_v<std::string, OneofAlternativeType<2, RawOneof>>);
76 static_assert(std::is_same_v<TestEnum, OneofAlternativeType<3, RawOneof>>);
77 static_assert(std::is_same_v<structs::Simple, OneofAlternativeType<4, RawOneof>>);
79 static_assert(std::is_same_v<int32_t, OneofAlternativeType<0, CustomOneof>>);
80 static_assert(std::is_same_v<int32_t, OneofAlternativeType<1, CustomOneof>>);
81 static_assert(std::is_same_v<std::string, OneofAlternativeType<2, CustomOneof>>);
82 static_assert(std::is_same_v<TestEnum, OneofAlternativeType<3, CustomOneof>>);
83 static_assert(std::is_same_v<structs::Simple, OneofAlternativeType<4, CustomOneof>>);
86TEST(OneofTest, Ctor) {
87 RawOneof default_oneof;
89 CheckAlternativeSet(default_oneof, kOneofNpos);
91 RawOneof oneof(std::in_place_index<2>,
"hello world");
93 CheckAlternativeSet(oneof, 2);
94 EXPECT_EQ(oneof.Get<2>(),
"hello world");
96 RawOneof oneof_copy(oneof);
98 CheckAlternativeSet(oneof_copy, 2);
99 EXPECT_EQ(oneof_copy.Get<2>(),
"hello world");
101 oneof.Get<2>() =
"test1";
104 CheckAlternativeSet(oneof_copy, 2);
105 EXPECT_EQ(oneof_copy.Get<2>(),
"test1");
107 oneof.Get<2>() =
"test2";
108 RawOneof oneof_move(std::move(oneof));
110 CheckAlternativeSet(oneof_move, 2);
111 EXPECT_EQ(oneof_move.Get<2>(),
"test2");
113 oneof_copy = std::move(oneof_move);
115 CheckAlternativeSet(oneof_copy, 2);
116 EXPECT_EQ(oneof_copy.Get<2>(),
"test2");
119TEST(OneofTest, GetSetEmplace) {
124 CheckAlternativeSet(oneof, 0);
125 EXPECT_EQ(oneof.Get<0>(), 0);
126 UEXPECT_THROW_MSG(
static_cast<
void>(oneof.Get<1>()), OneofAccessError,
"index = 1");
130 CheckAlternativeSet(oneof, 0);
131 EXPECT_EQ(oneof.Get<0>(), 42);
133 EXPECT_EQ(oneof.Emplace<1>(1001), 1001);
134 CheckAlternativeSet(oneof, 1);
135 EXPECT_EQ(oneof.Get<1>(), 1001);
139 CheckAlternativeSet(oneof, 1);
140 EXPECT_EQ(oneof.Get<1>(), 1002);
142 oneof.Set<2>(
"hello world");
144 CheckAlternativeSet(oneof, 2);
145 EXPECT_EQ(oneof.Get<2>(),
"hello world");
147 std::string str =
"some string";
149 EXPECT_EQ(oneof.Emplace<2>(str.begin() + 5, str.end()),
"string");
150 CheckAlternativeSet(oneof, 2);
153 oneof.Set<2>(std::move(str));
155 CheckAlternativeSet(oneof, 2);
156 EXPECT_EQ(oneof.Get<2>(),
"some string!");
158 oneof.Set<3>(kValue1);
160 CheckAlternativeSet(oneof, 3);
161 EXPECT_EQ(oneof.Get<3>(), kValue1);
163 oneof.Set<4>({.f1 = 11});
165 CheckAlternativeSet(oneof, 4);
166 EXPECT_EQ(std::move(oneof).Get<4>().f1, 11);
168 oneof.GetMutable<0>() = 6;
170 CheckAlternativeSet(oneof, 0);
171 EXPECT_EQ(oneof.Get<0>(), 6);
174TEST(OneofTest, Clear) {
177 EXPECT_NO_THROW(oneof.ClearOneof());
178 CheckAlternativeSet(oneof, kOneofNpos);
182 EXPECT_NO_THROW(oneof.Clear(1));
183 CheckAlternativeSet(oneof, 0);
184 EXPECT_EQ(oneof.Get<0>(), 1);
186 EXPECT_NO_THROW(oneof.Clear(0));
187 CheckAlternativeSet(oneof, kOneofNpos);
191 EXPECT_NO_THROW(oneof.ClearOneof());
192 CheckAlternativeSet(oneof, kOneofNpos);