Skip to content

Commit

Permalink
HybridNode: add type tests for set, list, union, struct members
Browse files Browse the repository at this point in the history
Summary:
In TestStruct add hybrid members with set, list, union and struct to start
building UTs for recurse visitor handling of hybrid nodes.

Note that in this diff allow_skip_thrift_cow annotation on these members is not
enabled. We will enable this after RecurseVisitor is updated to handle all
these types of hybrid members.

Reviewed By: wilsonwinhi

Differential Revision: D63838249

fbshipit-source-id: bb95ba200d830c5b833276000a76bd97412ee3d5
  • Loading branch information
Priyank Warkhede authored and facebook-github-bot committed Oct 4, 2024
1 parent ef4fe45 commit d02afd8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
40 changes: 40 additions & 0 deletions fboss/thrift_cow/nodes/tests/ThriftHybridStructNodeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,46 @@ TEST(ThriftHybridStructNodeTests, ThriftStructNodeHybridMemberToFromThrift) {
auto thrift = val->toThrift();
ASSERT_EQ(thrift, *data);
}

TEST(ThriftHybridStructNodeTests, TestHybridNodeMemberTypes) {
ThriftStructNode<TestStruct> node;

// list
ASSERT_EQ(node.isSkipThriftCowEnabled<k::hybridList>(), true);
auto v_list = node.get<k::hybridList>();
using underlying_type_l =
folly::remove_cvref_t<decltype(*v_list)>::ThriftType;
static_assert(std::is_same_v<underlying_type_l, std::vector<int>>);
using ref_type_l = folly::remove_cvref_t<decltype(v_list->ref())>;
static_assert(std::is_same_v<ref_type_l, std::vector<int>>);

// set
ASSERT_EQ(node.isSkipThriftCowEnabled<k::hybridSet>(), true);
auto v_set = node.get<k::hybridSet>();
using underlying_type_s = folly::remove_cvref_t<decltype(*v_set)>::ThriftType;
static_assert(std::is_same_v<underlying_type_s, std::set<int>>);
using ref_type_s = folly::remove_cvref_t<decltype(v_set->ref())>;
static_assert(std::is_same_v<ref_type_s, std::set<int>>);

// union
ASSERT_EQ(node.isSkipThriftCowEnabled<k::hybridUnion>(), true);
auto v_union = node.get<k::hybridUnion>();
using underlying_type_u =
folly::remove_cvref_t<decltype(*v_union)>::ThriftType;
static_assert(std::is_same_v<underlying_type_u, TestUnion>);
using ref_type_u = folly::remove_cvref_t<decltype(v_union->ref())>;
static_assert(std::is_same_v<ref_type_u, TestUnion>);

// child struct
ASSERT_EQ(node.isSkipThriftCowEnabled<k::hybridStruct>(), true);
auto v_struct = node.get<k::hybridStruct>();
using underlying_type_c =
folly::remove_cvref_t<decltype(*v_struct)>::ThriftType;
static_assert(std::is_same_v<underlying_type_c, ChildStruct>);
using ref_type_c = folly::remove_cvref_t<decltype(v_struct->ref())>;
static_assert(std::is_same_v<ref_type_c, ChildStruct>);
}

#endif // __ENABLE_HYBRID_THRIFT_COW_TESTS__

#ifdef ENABLE_DYNAMIC_APIS
Expand Down
10 changes: 9 additions & 1 deletion fboss/thrift_cow/nodes/tests/test.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ union TestUnion {
20: set<string> setOfString;
}

struct ChildStruct {
1: map<i32, bool> childMap;
}

struct TestStruct {
1: bool inlineBool;
2: i32 inlineInt;
Expand All @@ -55,10 +59,14 @@ struct TestStruct {
22: optional string optionalString;
@cpp.Type{name = "uint64_t"}
23: i64 unsigned_int64;
24: map<string, TestStruct> mapA; // (allow_skip_thrift_cow = true)
24: map<string, TestStruct> mapA;
25: map<string, TestStruct> mapB;
26: map<i32, bool> cowMap;
27: map<i32, bool> hybridMap; // (allow_skip_thrift_cow = true);
28: list<i32> hybridList; // (allow_skip_thrift_cow = true);
29: set<i32> hybridSet; // (allow_skip_thrift_cow = true);
30: TestUnion hybridUnion; // (allow_skip_thrift_cow = true);
31: ChildStruct hybridStruct; // (allow_skip_thrift_cow = true);
} // (allow_skip_thrift_cow = true)

struct ParentTestStruct {
Expand Down
9 changes: 8 additions & 1 deletion fboss/thrift_cow/visitors/tests/RecurseVisitorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ folly::dynamic createTestDynamic() {
"setOfI32", dynamic::array())("setOfString", dynamic::array())(
"unsigned_int64", 123)("mapA", dynamic::object())(
"mapB", dynamic::object())("cowMap", dynamic::object())(
"hybridMap", dynamic::object());
"hybridMap", dynamic::object())("hybridList", dynamic::array())(
"hybridSet", dynamic::array())("hybridUnion", dynamic::object())(
"hybridStruct", dynamic::object("childMap", dynamic::object()));
}

TestStruct createTestStruct(folly::dynamic testDyn) {
Expand Down Expand Up @@ -65,6 +67,11 @@ TEST(RecurseVisitorTests, TestFullRecurse) {
{{}, testDyn},
{{"cowMap"}, dynamic::object()},
{{"hybridMap"}, dynamic::object()},
{{"hybridList"}, dynamic::array()},
{{"hybridSet"}, dynamic::array()},
{{"hybridUnion"}, dynamic::object()},
{{"hybridStruct"}, testDyn["hybridStruct"]},
{{"hybridStruct", "childMap"}, testDyn["hybridStruct"]["childMap"]},
{{"inlineBool"}, testDyn["inlineBool"]},
{{"inlineInt"}, testDyn["inlineInt"]},
{{"inlineString"}, testDyn["inlineString"]},
Expand Down

0 comments on commit d02afd8

Please sign in to comment.