/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include using apache::thrift::compiler::t_const_value; namespace { template std::unique_ptr val(Args&&... args) { return std::make_unique(std::forward(args)...); } template ::value>> std::unique_ptr val(Enum val) { return std::make_unique( static_cast>(val)); } } // namespace TEST(ConstUtilTest, HydrateConst) { auto outer = t_const_value::make_map(); auto dbl = val(); dbl->set_double(42.0); outer->add_map(val("floatField"), std::move(dbl)); auto list = t_const_value::make_list(); list->add_list(val(42)); auto inner = t_const_value::make_map(); inner->add_map(val("listField"), std::move(list)); outer->add_map(val("unionField"), std::move(inner)); outer->add_map(val("enumField"), val(cpp2::E::B)); cpp2::Outer s; hydrate_const(s, *outer); EXPECT_EQ(*s.floatField(), 42.0); EXPECT_EQ(s.unionField()->listField_ref()->at(0), 42); EXPECT_EQ(s.unionField()->listField_ref()->size(), 1); EXPECT_EQ(*s.enumField(), cpp2::E::B); } TEST(ConstUtilTest, ConstToValue) { auto str = val("foo"); EXPECT_EQ(const_to_value(*str).as_string(), "foo"); auto map = t_const_value::make_map(); map->add_map(val("answer"), val(42)); auto value = const_to_value(*map); EXPECT_EQ(value.as_map().at(const_to_value(*val("answer"))).as_i64(), 42); }