Skip to content

Commit c99f9a2

Browse files
committed
Refs #23128: Add remaining test cases
Signed-off-by: Carlosespicur <carlosespicur@proton.me>
1 parent 6c8f882 commit c99f9a2

File tree

9 files changed

+369
-12
lines changed

9 files changed

+369
-12
lines changed

src/cpp/fastdds/xtypes/dynamic_types/idl_parser/IdlAnnotations.hpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,16 +1909,6 @@ class ValueAnnotation final : public BuiltinAnnotation
19091909

19101910
descriptor->default_value(parameters.at(IDL_VALUE_TAG));
19111911

1912-
// Check that the default value is consistent with the member type
1913-
if (!descriptor->is_consistent())
1914-
{
1915-
EPROSIMA_LOG_ERROR(IDL_PARSER,
1916-
"Default value '" << parameters.at(IDL_VALUE_TAG)
1917-
<< "' is not consistent with the member type for annotation '"
1918-
<< IDL_BUILTIN_ANN_DEFAULT_TAG << "'.");
1919-
return false;
1920-
}
1921-
19221912
return true;
19231913
}
19241914

test/feature/idl_parser/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,9 @@ add_custom_command(
7878
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/mutable_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
7979
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/optional_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
8080
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/position_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
81+
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/bit_bound_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
82+
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/external_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
83+
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/nested_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
84+
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/try_construct_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
85+
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/test/feature/idl_parser/idl_extra_cases/value_annotation.idl ${CMAKE_CURRENT_BINARY_DIR}/IDL/
8186
)

test/feature/idl_parser/IdlParserTests.cpp

Lines changed: 218 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2585,7 +2585,7 @@ TEST_F(IdlParserTests, default_builtin_annotation)
25852585
std::vector<std::string> include_paths;
25862586
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
25872587

2588-
// Set default values for struct members using positional parameters
2588+
// Set default values for struct members and check that they are correctly parsed
25892589
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/default_annotation.idl", "default_ann_valid_struct",
25902590
include_paths);
25912591
DynamicTypeMember::_ref_type member;
@@ -2596,6 +2596,223 @@ TEST_F(IdlParserTests, default_builtin_annotation)
25962596
EXPECT_EQ(builder->get_member(member, 1), RETCODE_OK);
25972597
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
25982598
EXPECT_EQ(member_descriptor->default_value(), "foo");
2599+
2600+
// Set default values for union members and check that they are correctly parsed
2601+
builder = factory->create_type_w_uri("IDL/default_annotation.idl", "default_ann_valid_union",
2602+
include_paths);
2603+
ASSERT_TRUE(builder);
2604+
EXPECT_EQ(builder->get_member_by_name(member, "first"), RETCODE_OK);
2605+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2606+
EXPECT_EQ(member_descriptor->default_value(), "1");
2607+
EXPECT_EQ(builder->get_member_by_name(member, "second"), RETCODE_OK);
2608+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2609+
EXPECT_EQ(member_descriptor->default_value(), "foo");
2610+
EXPECT_EQ(builder->get_member_by_name(member, "third"), RETCODE_OK);
2611+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2612+
EXPECT_EQ(member_descriptor->default_value(), "3.14");
2613+
2614+
// Negative case: Trying to annotate using @default with invalid value type
2615+
builder = factory->create_type_w_uri("IDL/default_annotation.idl", "default_ann_invalid_value_type",
2616+
include_paths);
2617+
ASSERT_FALSE(builder);
2618+
2619+
// Negative case: Trying to annotate using @default with additional parameters
2620+
builder = factory->create_type_w_uri("IDL/default_annotation.idl", "default_ann_extra_parameter",
2621+
include_paths);
2622+
ASSERT_FALSE(builder);
2623+
2624+
// Negative case: Trying to annotate a constructed type with @default
2625+
builder = factory->create_type_w_uri("IDL/default_annotation.idl", "default_ann_on_struct",
2626+
include_paths);
2627+
ASSERT_FALSE(builder);
2628+
}
2629+
2630+
TEST_F(IdlParserTests, bit_bound_builtin_annotation)
2631+
{
2632+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
2633+
MemberDescriptor::_ref_type member_descriptor{traits<MemberDescriptor>::make_shared()};
2634+
std::vector<std::string> include_paths;
2635+
2636+
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
2637+
2638+
// TODO: Add positive tests for bitset/bitmask when bitset/bitmask parsing is supported.
2639+
2640+
// Set bit_bound annotation on enum members and check that they are correctly parsed
2641+
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/bit_bound_annotation.idl", "bit_bound_ann_valid_enum",
2642+
include_paths);
2643+
DynamicTypeMember::_ref_type member;
2644+
ASSERT_TRUE(builder);
2645+
EXPECT_EQ(builder->get_member_by_name(member, "ENUM_VALUE_1"), RETCODE_OK);
2646+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2647+
EXPECT_EQ(member_descriptor->type(), factory->get_primitive_type(TK_INT8));
2648+
EXPECT_EQ(builder->get_member_by_name(member, "ENUM_VALUE_2"), RETCODE_OK);
2649+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2650+
EXPECT_EQ(member_descriptor->type(), factory->get_primitive_type(TK_INT8));
2651+
2652+
// Negative case: Trying to annotate using @bit_bound with invalid bound value
2653+
builder = factory->create_type_w_uri("IDL/bit_bound_annotation.idl", "bit_bound_ann_invalid_bound_value_enum",
2654+
include_paths);
2655+
// ASSERT_FALSE(builder);
2656+
EXPECT_FALSE(builder);
2657+
2658+
// Negative case: Trying to annotate using @bit_bound with invalid value type
2659+
builder = factory->create_type_w_uri("IDL/bit_bound_annotation.idl", "bit_bound_ann_invalid_value_type_enum",
2660+
include_paths);
2661+
ASSERT_FALSE(builder);
2662+
2663+
// Negative case: Trying to annotate using @bit_bound with additional parameters
2664+
builder = factory->create_type_w_uri("IDL/bit_bound_annotation.idl", "bit_bound_ann_extra_parameter_enum",
2665+
include_paths);
2666+
ASSERT_FALSE(builder);
2667+
2668+
// Negative case: Trying to annotate a non-primitive type different from bitset/bitmask with @bit_bound
2669+
builder = factory->create_type_w_uri("IDL/bit_bound_annotation.idl", "bit_bound_ann_on_struct",
2670+
include_paths);
2671+
ASSERT_FALSE(builder);
2672+
}
2673+
2674+
TEST_F(IdlParserTests, external_builtin_annotation)
2675+
{
2676+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
2677+
MemberDescriptor::_ref_type member_descriptor{traits<MemberDescriptor>::make_shared()};
2678+
std::vector<std::string> include_paths;
2679+
2680+
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
2681+
2682+
// Set external annotation on struct members and check that they are correctly parsed
2683+
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/external_annotation.idl", "external_ann_struct_valid",
2684+
include_paths);
2685+
DynamicTypeMember::_ref_type member;
2686+
ASSERT_TRUE(builder);
2687+
EXPECT_EQ(builder->get_member(member, 0), RETCODE_OK);
2688+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2689+
EXPECT_TRUE(member_descriptor->is_shared());
2690+
EXPECT_EQ(builder->get_member(member, 1), RETCODE_OK);
2691+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2692+
EXPECT_FALSE(member_descriptor->is_shared());
2693+
EXPECT_EQ(builder->get_member(member, 2), RETCODE_OK);
2694+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2695+
EXPECT_FALSE(member_descriptor->is_shared());
2696+
2697+
// Negative case: Trying to annotate using @external with invalid value type
2698+
builder = factory->create_type_w_uri("IDL/external_annotation.idl", "external_ann_invalid_value_type",
2699+
include_paths);
2700+
ASSERT_FALSE(builder);
2701+
2702+
// Negative case: Trying to annotate using @external with additional parameters
2703+
builder = factory->create_type_w_uri("IDL/external_annotation.idl", "external_ann_extra_parameter",
2704+
include_paths);
2705+
ASSERT_FALSE(builder);
2706+
2707+
// Negative case: Trying to annotate a constructed type with @external
2708+
builder = factory->create_type_w_uri("IDL/external_annotation.idl", "external_ann_on_struct",
2709+
include_paths);
2710+
ASSERT_FALSE(builder);
2711+
}
2712+
2713+
TEST_F(IdlParserTests, nested_builtin_annotation)
2714+
{
2715+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
2716+
TypeDescriptor::_ref_type type_descriptor{traits<TypeDescriptor>::make_shared()};
2717+
std::vector<std::string> include_paths;
2718+
2719+
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
2720+
2721+
// Set nested annotations on struct (constructed type) and check that they are correctly parsed
2722+
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/nested_annotation.idl", "nested_ann_struct_valid",
2723+
include_paths);
2724+
ASSERT_TRUE(builder);
2725+
EXPECT_EQ(builder->get_descriptor(type_descriptor), RETCODE_OK);
2726+
EXPECT_EQ(type_descriptor->is_nested(), true);
2727+
builder = factory->create_type_w_uri("IDL/nested_annotation.idl", "nested_ann_struct_keyword_valid",
2728+
include_paths);
2729+
ASSERT_TRUE(builder);
2730+
EXPECT_EQ(builder->get_descriptor(type_descriptor), RETCODE_OK);
2731+
EXPECT_EQ(type_descriptor->is_nested(), true);
2732+
2733+
// Negative case: Trying to annotate using @nested with invalid value type
2734+
builder = factory->create_type_w_uri("IDL/nested_annotation.idl", "nested_ann_struct_invalid_value_type",
2735+
include_paths);
2736+
ASSERT_FALSE(builder);
2737+
2738+
// Negative case: Trying to annotate using @nested with extra parameters
2739+
builder = factory->create_type_w_uri("IDL/nested_annotation.idl", "nested_ann_struct_extra_parameter",
2740+
include_paths);
2741+
ASSERT_FALSE(builder);
2742+
2743+
// Negative case: Trying to annotate a member with @nested
2744+
builder = factory->create_type_w_uri("IDL/nested_annotation.idl", "nested_ann_on_member",
2745+
include_paths);
2746+
ASSERT_FALSE(builder);
2747+
}
2748+
2749+
TEST_F(IdlParserTests, try_construct_builtin_annotation)
2750+
{
2751+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
2752+
MemberDescriptor::_ref_type member_descriptor{traits<MemberDescriptor>::make_shared()};
2753+
std::vector<std::string> include_paths;
2754+
2755+
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
2756+
2757+
// Set try_construct annotation on struct members and check that they are correctly parsed
2758+
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/try_construct_annotation.idl", "try_construct_ann_struct_valid",
2759+
include_paths);
2760+
ASSERT_TRUE(builder);
2761+
DynamicTypeMember::_ref_type member;
2762+
EXPECT_EQ(builder->get_member(member, 0), RETCODE_OK);
2763+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2764+
EXPECT_EQ(member_descriptor->try_construct_kind(), TryConstructKind::USE_DEFAULT);
2765+
EXPECT_EQ(builder->get_member(member, 1), RETCODE_OK);
2766+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2767+
EXPECT_EQ(member_descriptor->try_construct_kind(), TryConstructKind::USE_DEFAULT);
2768+
EXPECT_EQ(builder->get_member(member, 2), RETCODE_OK);
2769+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2770+
EXPECT_EQ(member_descriptor->try_construct_kind(), TryConstructKind::TRIM);
2771+
// TODO (For some reason, maybe a PEGTL bug, "DISCARD" identifier is not parsed correctly, matching only "D"). Uncomment when fixed.
2772+
// EXPECT_EQ(builder->get_member(member, 3), RETCODE_OK);
2773+
// EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2774+
// EXPECT_EQ(member_descriptor->try_construct_kind(), TryConstructKind::DISCARD);
2775+
2776+
// Negative case: Trying to annotate using @try_construct with invalid value type
2777+
builder = factory->create_type_w_uri("IDL/try_construct_annotation.idl", "try_construct_ann_invalid_value_type",
2778+
include_paths);
2779+
ASSERT_FALSE(builder);
2780+
}
2781+
2782+
TEST_F(IdlParserTests, value_builtin_annotation)
2783+
{
2784+
DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()};
2785+
MemberDescriptor::_ref_type member_descriptor{traits<MemberDescriptor>::make_shared()};
2786+
std::vector<std::string> include_paths;
2787+
2788+
include_paths.push_back("IDL/helpers/basic_inner_types.idl");
2789+
2790+
// Set value annotation on struct members and check that they are correctly parsed
2791+
DynamicTypeBuilder::_ref_type builder = factory->create_type_w_uri("IDL/value_annotation.idl", "value_ann_valid_struct",
2792+
include_paths);
2793+
ASSERT_TRUE(builder);
2794+
DynamicTypeMember::_ref_type member;
2795+
EXPECT_EQ(builder->get_member(member, 0), RETCODE_OK);
2796+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2797+
EXPECT_EQ(member_descriptor->default_value(), "foo");
2798+
EXPECT_EQ(builder->get_member(member, 1), RETCODE_OK);
2799+
EXPECT_EQ(member->get_descriptor(member_descriptor), RETCODE_OK);
2800+
EXPECT_EQ(member_descriptor->default_value(), "8");
2801+
2802+
// Negative case: Trying to annotate using @value with invalid value type
2803+
builder = factory->create_type_w_uri("IDL/value_annotation.idl", "value_ann_invalid_value_type_struct",
2804+
include_paths);
2805+
ASSERT_FALSE(builder);
2806+
2807+
// Negative case: Trying to annotate using @value with additional parameters
2808+
builder = factory->create_type_w_uri("IDL/value_annotation.idl", "value_ann_extra_parameter_struct",
2809+
include_paths);
2810+
ASSERT_FALSE(builder);
2811+
2812+
// Negative case: Trying to annotate a constructed type with @value
2813+
builder = factory->create_type_w_uri("IDL/value_annotation.idl", "value_ann_on_struct",
2814+
include_paths);
2815+
ASSERT_FALSE(builder);
25992816
}
26002817

26012818
int main(
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
enum bit_bound_ann_valid_enum
2+
{
3+
@bit_bound(8) ENUM_VALUE_1,
4+
@bit_bound(value = 8) ENUM_VALUE_2
5+
};
6+
7+
enum bit_bound_ann_invalid_bound_value_enum
8+
{
9+
@bit_bound(100) ENUM_VALUE_1,
10+
@bit_bound(100) ENUM_VALUE_2
11+
};
12+
13+
enum bit_bound_ann_invalid_value_type_enum
14+
{
15+
@bit_bound("invalid") ENUM_VALUE_1,
16+
@bit_bound("invalid") ENUM_VALUE_2
17+
};
18+
19+
enum bit_bound_ann_extra_parameter_enum
20+
{
21+
@bit_bound(value = 8, value2 = "extra") ENUM_VALUE_1,
22+
@bit_bound(value = 8, value2 = "extra") ENUM_VALUE_2
23+
};
24+
25+
@bit_bound(8) struct bit_bound_ann_on_struct
26+
{
27+
long first;
28+
string second;
29+
};
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
struct default_ann_valid_struct
22
{
33
@default(2) long first;
4-
@default("foo") string second;
4+
@default(value = "foo") string second;
5+
};
6+
7+
union default_ann_valid_union switch(long)
8+
{
9+
case 0:
10+
@default(1) long first;
11+
case 1:
12+
@default("foo") string second;
13+
default:
14+
@default(3.14) float third;
15+
};
16+
17+
struct default_ann_missing_value
18+
{
19+
@default long invalid_member;
20+
};
21+
22+
struct default_ann_invalid_value_type
23+
{
24+
@default("invalid") long invalid_member;
25+
};
26+
27+
struct default_ann_extra_parameter
28+
{
29+
@default(value = 2, value2 = "extra") long invalid_member;
30+
};
31+
32+
@default("foo") struct default_ann_on_struct
33+
{
34+
long first;
535
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct external_ann_struct_valid
2+
{
3+
@external long first;
4+
@external(FALSE) string second;
5+
@external(value = FALSE) short third;
6+
};
7+
8+
struct external_ann_invalid_value_type
9+
{
10+
@external("invalid") long invalid_member;
11+
};
12+
13+
struct external_ann_extra_parameter
14+
{
15+
@external(value = FALSE, value2 = "extra") long invalid_member;
16+
};
17+
18+
@external struct external_ann_on_struct
19+
{
20+
long first;
21+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@nested struct nested_ann_struct_valid
2+
{
3+
long first;
4+
};
5+
6+
@nested(value = TRUE) struct nested_ann_struct_keyword_valid
7+
{
8+
long first;
9+
};
10+
11+
@nested("invalid") struct nested_ann_struct_invalid_value_type
12+
{
13+
long invalid_member;
14+
};
15+
16+
@nested(value = TRUE, value2 = "extra") struct nested_ann_struct_extra_parameter
17+
{
18+
long invalid_member;
19+
};
20+
21+
struct nested_ann_on_member
22+
{
23+
@nested long invalid_member;
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct try_construct_ann_struct_valid
2+
{
3+
@try_construct long first;
4+
@try_construct(USE_DEFAULT) string second;
5+
@try_construct(TRIM) string third;
6+
};
7+
8+
struct try_construct_ann_invalid_value_type
9+
{
10+
@try_construct("invalid") long invalid_member;
11+
};
12+
13+
struct try_construct_ann_extra_parameter
14+
{
15+
@try_construct(value = TRIM, value2 = "extra") long invalid_member;
16+
};
17+
18+
@try_construct struct try_construct_ann_on_struct
19+
{
20+
long first;
21+
};

0 commit comments

Comments
 (0)