@ -7,6 +7,9 @@
# include "google/protobuf/hpb/extension.h"
# include <cstdint>
# include <type_traits>
# include <gmock/gmock.h>
# include <gtest/gtest.h>
# include "google/protobuf/compiler/hpb/tests/child_model.upb.proto.h"
@ -23,11 +26,16 @@ using ::hpb_unittest::protos::other_ext;
using : : hpb_unittest : : protos : : TestModel ;
using : : hpb_unittest : : protos : : theme ;
using : : hpb_unittest : : protos : : ThemeExtension ;
using : : hpb_unittest : : someotherpackage : : protos : : bool_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : double_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : float_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : int32_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : int64_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : repeated_int32_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : repeated_int64_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : repeated_string_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : uint32_ext ;
using : : hpb_unittest : : someotherpackage : : protos : : uint64_ext ;
using : : testing : : status : : IsOkAndHolds ;
@ -55,7 +63,7 @@ TEST(CppGeneratedCode, ClearExtensionWithEmptyExtensionPtr) {
EXPECT_EQ ( false , : : hpb : : HasExtension ( recursive_child , theme ) ) ;
}
TEST ( CppGeneratedCode , SetExtensionInt32 ) {
TEST ( CppGeneratedCode , Get SetExtensionInt32) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , int32_ext ) ) ;
int32_t val = 55 ;
@ -64,7 +72,7 @@ TEST(CppGeneratedCode, SetExtensionInt32) {
EXPECT_THAT ( hpb : : GetExtension ( & model , int32_ext ) , IsOkAndHolds ( val ) ) ;
}
TEST ( CppGeneratedCode , SetExtensionInt64 ) {
TEST ( CppGeneratedCode , Get SetExtensionInt64) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , int64_ext ) ) ;
int64_t val = std : : numeric_limits < int32_t > : : max ( ) + int64_t { 1 } ;
@ -73,6 +81,50 @@ TEST(CppGeneratedCode, SetExtensionInt64) {
EXPECT_THAT ( hpb : : GetExtension ( & model , int64_ext ) , IsOkAndHolds ( val ) ) ;
}
TEST ( CppGeneratedCode , GetSetExtensionUInt32 ) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , uint32_ext ) ) ;
uint32_t val = std : : numeric_limits < int32_t > : : max ( ) + uint32_t { 5 } ;
auto x = hpb : : SetExtension ( & model , uint32_ext , val ) ;
EXPECT_EQ ( true , hpb : : HasExtension ( & model , uint32_ext ) ) ;
EXPECT_THAT ( hpb : : GetExtension ( & model , uint32_ext ) , IsOkAndHolds ( val ) ) ;
}
TEST ( CppGeneratedCode , GetSetExtensionUInt64 ) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , uint64_ext ) ) ;
uint64_t val = std : : numeric_limits < int64_t > : : max ( ) + uint64_t { 5 } ;
auto x = hpb : : SetExtension ( & model , uint64_ext , val ) ;
EXPECT_EQ ( true , hpb : : HasExtension ( & model , uint64_ext ) ) ;
EXPECT_THAT ( hpb : : GetExtension ( & model , uint64_ext ) , IsOkAndHolds ( val ) ) ;
}
TEST ( CppGeneratedCode , GetSetExtensionFloat ) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , float_ext ) ) ;
float val = 2.78 ;
auto x = hpb : : SetExtension ( & model , float_ext , val ) ;
EXPECT_EQ ( true , hpb : : HasExtension ( & model , float_ext ) ) ;
EXPECT_THAT ( hpb : : GetExtension ( & model , float_ext ) , IsOkAndHolds ( val ) ) ;
}
TEST ( CppGeneratedCode , GetSetExtensionDouble ) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , double_ext ) ) ;
double val = std : : numeric_limits < float > : : max ( ) + 1.23 ;
auto x = hpb : : SetExtension ( & model , double_ext , val ) ;
EXPECT_EQ ( true , hpb : : HasExtension ( & model , double_ext ) ) ;
EXPECT_THAT ( hpb : : GetExtension ( & model , double_ext ) , IsOkAndHolds ( val ) ) ;
}
TEST ( CppGeneratedCode , GetSetExtensionBool ) {
TestModel model ;
EXPECT_EQ ( false , hpb : : HasExtension ( & model , bool_ext ) ) ;
auto x = hpb : : SetExtension ( & model , bool_ext , true ) ;
EXPECT_EQ ( true , hpb : : HasExtension ( & model , bool_ext ) ) ;
EXPECT_THAT ( hpb : : GetExtension ( & model , bool_ext ) , IsOkAndHolds ( true ) ) ;
}
TEST ( CppGeneratedCode , SetExtension ) {
TestModel model ;
void * prior_message ;
@ -235,6 +287,38 @@ TEST(CppGeneratedCode, GetExtensionInt64WithDefault) {
EXPECT_EQ ( * res , expected ) ;
}
TEST ( CppGeneratedCode , GetExtensionUInt32WithDefault ) {
TestModel model ;
auto res = hpb : : GetExtension ( & model , uint32_ext ) ;
EXPECT_THAT ( res , IsOkAndHolds ( 12 ) ) ;
}
TEST ( CppGeneratedCode , GetExtensionUInt64WithDefault ) {
TestModel model ;
auto res = hpb : : GetExtension ( & model , uint64_ext ) ;
EXPECT_THAT ( res , IsOkAndHolds ( 4294967296 ) ) ;
}
TEST ( CppGeneratedCode , GetExtensionFloatWithDefault ) {
TestModel model ;
auto res = hpb : : GetExtension ( & model , float_ext ) ;
static_assert ( std : : is_same_v < decltype ( res ) , absl : : StatusOr < float > > ) ;
EXPECT_THAT ( res , IsOkAndHolds ( 3.14f ) ) ;
}
TEST ( CppGeneratedCode , GetExtensionDoubleWithDefault ) {
TestModel model ;
auto res = hpb : : GetExtension ( & model , double_ext ) ;
static_assert ( std : : is_same_v < decltype ( res ) , absl : : StatusOr < double > > ) ;
EXPECT_THAT ( res , IsOkAndHolds ( 340282000000000000000000000000000000001.23 ) ) ;
}
TEST ( CppGeneratedCode , GetExtensionBoolWithDefault ) {
TestModel model ;
auto res = hpb : : GetExtension ( & model , bool_ext ) ;
EXPECT_THAT ( res , IsOkAndHolds ( true ) ) ;
}
TEST ( CppGeneratedCode , GetExtensionOnMutableChild ) {
TestModel model ;
ThemeExtension extension1 ;