Skip to content

Commit 0b67fd7

Browse files
Mizuchimeta-codesync[bot]
authored andcommitted
Add annotation unit-test
Reviewed By: praihan Differential Revision: D85978391 fbshipit-source-id: 0cc45c52f8baaef29d1e125ff2e8b5cf3d48de87
1 parent b197d9d commit 0b67fd7

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package "apache.org/thrift/test"
18+
19+
struct Bar {
20+
1: string baz;
21+
}
22+
23+
struct Foo {
24+
1: Bar bar;
25+
}
26+
27+
struct MyStruct {
28+
1: string stringField;
29+
}
30+
31+
struct MyUnion {
32+
1: i32 i32Field;
33+
2: string stringField;
34+
}
35+
36+
enum MyEnum {
37+
first = 1,
38+
second = 2,
39+
}
40+
41+
struct Annotation {
42+
1: bool boolField;
43+
2: i16 i16Field;
44+
3: i32 i32Field;
45+
4: float floatField;
46+
5: double doubleField;
47+
6: binary binaryField;
48+
7: MyStruct structField;
49+
8: MyUnion unionField;
50+
9: MyEnum enumField;
51+
10: list<i32> listField;
52+
11: set<i32> setField;
53+
12: map<i32, string> mapField;
54+
}
55+
56+
@Annotation{
57+
boolField = true,
58+
i16Field = 16,
59+
i32Field = 32,
60+
floatField = 10.0,
61+
doubleField = 20.0,
62+
binaryField = "binary",
63+
structField = MyStruct{stringField = "struct"},
64+
unionField = MyUnion{stringField = "union"},
65+
enumField = MyEnum.second,
66+
listField = [2, 1, 2],
67+
setField = [2, 1],
68+
mapField = {2: "20", 1: "10"},
69+
}
70+
@Foo{bar = Bar{baz = "123"}}
71+
enum TestEnum {
72+
foo = 1,
73+
bar = 2,
74+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <gtest/gtest.h>
18+
#include <thrift/lib/cpp2/test/metadata/gen-cpp2/annotations_metadata.h>
19+
20+
namespace apache::thrift::test {
21+
22+
std::vector<metadata::ThriftConstStruct> expectedAnnotations() {
23+
std::vector<metadata::ThriftConstStruct> ret{2};
24+
ret[0].type()->name() = "annotations.Annotation";
25+
ret[0].fields()["boolField"].cv_bool() = true;
26+
ret[0].fields()["i16Field"].cv_integer() = 16;
27+
ret[0].fields()["i32Field"].cv_integer() = 32;
28+
ret[0].fields()["floatField"].cv_double() = 10;
29+
ret[0].fields()["binaryField"].cv_string() = "binary";
30+
ret[0].fields()["doubleField"].cv_double() = 20;
31+
32+
ret[0].fields()["structField"].cv_struct().emplace().type()->name() =
33+
"annotations.MyStruct";
34+
ret[0]
35+
.fields()["structField"]
36+
.cv_struct()
37+
->fields()["stringField"]
38+
.cv_string() = "struct";
39+
ret[0].fields()["unionField"].cv_struct().emplace().type()->name() =
40+
"annotations.MyUnion";
41+
ret[0]
42+
.fields()["unionField"]
43+
.cv_struct()
44+
->fields()["stringField"]
45+
.cv_string() = "union";
46+
47+
ret[0].fields()["enumField"].cv_integer() = 2;
48+
ret[0].fields()["listField"].cv_list().emplace();
49+
ret[0].fields()["listField"].cv_list()->emplace_back().cv_integer() = 2;
50+
ret[0].fields()["listField"].cv_list()->emplace_back().cv_integer() = 1;
51+
ret[0].fields()["listField"].cv_list()->emplace_back().cv_integer() = 2;
52+
ret[0].fields()["setField"].cv_list().emplace();
53+
ret[0].fields()["setField"].cv_list()->emplace_back().cv_integer() = 2;
54+
ret[0].fields()["setField"].cv_list()->emplace_back().cv_integer() = 1;
55+
ret[0].fields()["mapField"].cv_map().emplace();
56+
ret[0].fields()["mapField"].cv_map()->emplace_back();
57+
ret[0].fields()["mapField"].cv_map()->back().key()->cv_integer() = 2;
58+
ret[0].fields()["mapField"].cv_map()->back().value()->cv_string() = "20";
59+
ret[0].fields()["mapField"].cv_map()->emplace_back();
60+
ret[0].fields()["mapField"].cv_map()->back().key()->cv_integer() = 1;
61+
ret[0].fields()["mapField"].cv_map()->back().value()->cv_string() = "10";
62+
63+
ret[1].type()->name() = "annotations.Foo";
64+
ret[1].fields()["bar"].cv_struct().emplace().type()->name() =
65+
"annotations.Bar";
66+
ret[1].fields()["bar"].cv_struct()->fields()["baz"].cv_string() = "123";
67+
68+
return ret;
69+
}
70+
71+
metadata::ThriftEnum expectedEnum() {
72+
metadata::ThriftEnum ret;
73+
ret.name() = "annotations.TestEnum";
74+
ret.elements()[1] = "foo";
75+
ret.elements()[2] = "bar";
76+
auto annotations = expectedAnnotations();
77+
ret.structured_annotations().emplace().assign(
78+
annotations.begin(), annotations.end());
79+
return ret;
80+
}
81+
82+
TEST(Annotations, Enum) {
83+
metadata::ThriftMetadata md;
84+
detail::md::EnumMetadata<TestEnum>::gen(md);
85+
EXPECT_EQ(md.enums()["annotations.TestEnum"], expectedEnum());
86+
}
87+
88+
} // namespace apache::thrift::test

0 commit comments

Comments
 (0)