Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / tests / IDLv4 / annotations / annotation_tests.cpp
blobbd874852c0e6c230afe0073491b333e4b6d16685
1 #include "Annotation_Test.h"
3 #include <ast_valuetype.h>
4 #include <ast_porttype.h>
5 #include <ast_eventtype.h>
6 #include <ast_component.h>
7 #include <ast_union_branch.h>
8 #include <ast_union_label.h>
9 #include <ast_expression.h>
10 #include <string>
12 namespace {
13 void assert_node_has_annotation (
14 Annotation_Test &t, const char *node_name, AST_Annotation_Decl *annotation)
16 AST_Decl *node = t.assert_node (node_name);
17 t.assert_annotation_appl_count (node, 1);
18 t.assert_annotation_appl (node, 0, annotation);
21 /**
22 * Common Test IDL for what the IDL4 grammer calls "attr_dcl"
24 const std::string common_attr_dcl_idl =
25 " @test_annotation_1\n"
26 " attribute short rw_attribute;\n"
27 " @test_annotation_1\n"
28 " readonly attribute short ro_attribute;\n";
30 void assert_common_attr_dcl_idl (
31 Annotation_Test &t, AST_Annotation_Decl *test_annotation_1)
33 assert_node_has_annotation (t, "rw_attribute", test_annotation_1);
34 assert_node_has_annotation (t, "ro_attribute", test_annotation_1);
37 /**
38 * Common Test IDL for what the IDL4 grammer calls "export"
40 const std::string common_export_idl =
41 // op_dcl
42 " @test_annotation_1\n"
43 " void operation();\n"
44 // attr_decl
45 + common_attr_dcl_idl +
46 // type_dcl
47 " @test_annotation_1\n"
48 " struct struct_in_export {\n"
49 " short value;\n"
50 " };\n"
51 // const_dcl
52 " @test_annotation_1\n"
53 " const short const_value = 3;\n"
54 // except_dcl
55 " @test_annotation_1\n"
56 " exception exception_in_export {\n"
57 " short value;\n"
58 " };\n"
59 // Use expection
60 " @test_annotation_1\n"
61 " void operation_with_exception() raises (exception_in_export);\n"
62 // type_id_dcl (Doesn't work)
63 // type_prefix_dcl (No grammar issues, but a type_prefix isn't something
64 // that's part of the AST, so I'm not sure how this would work).
65 // " @test_annotation_1\n"
66 // " typeprefix struct_in_export \"electric_plants\";\n"
67 // import_dcl (TAO_IDL has import as a keyword, but doesn't support it in the grammer)
68 // op_oneway_dcl
69 " @test_annotation_1\n"
70 " oneway void oneway_op();\n";
72 void assert_common_export_idl (
73 Annotation_Test &t, AST_Annotation_Decl *test_annotation_1)
75 assert_node_has_annotation (t, "operation", test_annotation_1);
76 assert_common_attr_dcl_idl (t, test_annotation_1);
77 assert_node_has_annotation (t, "struct_in_export", test_annotation_1);
78 assert_node_has_annotation (t, "const_value", test_annotation_1);
79 assert_node_has_annotation (t, "exception_in_export", test_annotation_1);
80 assert_node_has_annotation (t, "operation_with_exception", test_annotation_1);
81 assert_node_has_annotation (t, "oneway_op", test_annotation_1);
84 /**
85 * Common Test IDL for what the IDL4 grammer calls "value_element"
87 const std::string common_value_element_idl =
88 // export
89 common_export_idl +
91 // state_member
92 " @test_annotation_1\n"
93 " public short public_state_member;\n"
94 " @test_annotation_1\n"
95 " private short private_state_member;\n"
97 // init_dcl
98 " @test_annotation_1\n"
99 " factory factory_thing();\n";
101 void assert_common_value_element_idl (
102 Annotation_Test &t, AST_Annotation_Decl *test_annotation_1)
104 assert_common_export_idl (t, test_annotation_1);
105 assert_node_has_annotation (t, "public_state_member", test_annotation_1);
106 assert_node_has_annotation (t, "private_state_member", test_annotation_1);
107 assert_node_has_annotation (t, "factory_thing", test_annotation_1);
112 * Notes About These Tests
113 * =========================================================================
115 * - They are in the same IDL namespace, so they can conflict with each
116 * other.
118 * - You can't test for a syntax error really because tao_idl throws an
119 * exception for them. Even if the exception was caught, the AST might be
120 * in an invalid state afterwards.
122 * - Annotations local names internally start with @ so that they don't
123 * conflict with other non-annotation names. See below for examples.
125 * - Some of these tests intentionally cause errors.
128 void
129 annotation_tests ()
131 /* -------------------------------------------------------------------------
132 * Annotations Declarations
133 * -------------------------------------------------------------------------
134 * These tests assert that annotations can be declared.
137 AST_Annotation_Decl *test_annotation_1 = 0;
138 try {
139 Annotation_Test t ("Annotation Declaration with No Members");
140 test_annotation_1 = t.run (
141 "@annotation test_annotation_1 {\n"
142 "};\n"
143 ).assert_annotation_decl ("::@test_annotation_1");
144 t.assert_annotation_member_count (test_annotation_1, 0);
145 } catch (Failed const &) {}
147 try {
148 Annotation_Test t ("Annotation Declaration with Members");
149 AST_Annotation_Decl *test_annotation_2 = t.run (
150 "@annotation test_annotation_2 {\n"
151 " short short_value;\n"
152 " char char_value;\n"
153 " long long_value;\n"
154 " boolean boolean_value;\n"
155 "};\n"
156 ).assert_annotation_decl ("::@test_annotation_2");
157 t.assert_annotation_member_count (test_annotation_2, 4);
159 AST_Annotation_Member *short_value =
160 t.get_annotation_member (test_annotation_2, "short_value");
161 t.assert_annotation_member_type (short_value, AST_Expression::EV_short);
162 t.assert_annotation_member_no_value (short_value);
164 AST_Annotation_Member *char_value =
165 t.get_annotation_member (test_annotation_2, "char_value");
166 t.assert_annotation_member_type (char_value, AST_Expression::EV_char);
167 t.assert_annotation_member_no_value (char_value);
169 AST_Annotation_Member *long_value =
170 t.get_annotation_member (test_annotation_2, "long_value");
171 t.assert_annotation_member_type (long_value, AST_Expression::EV_long);
172 t.assert_annotation_member_no_value (long_value);
174 AST_Annotation_Member *boolean_value =
175 t.get_annotation_member (test_annotation_2, "boolean_value");
176 t.assert_annotation_member_type (boolean_value, AST_Expression::EV_bool);
177 t.assert_annotation_member_no_value (boolean_value);
178 } catch (Failed const &) {}
180 try {
181 Annotation_Test t ("Annotation Declaration with Defaulted Members");
182 AST_Annotation_Decl *test_annotation_3 = t.run (
183 "@annotation test_annotation_3 {\n"
184 " short short_value default 1;\n"
185 " char char_value default '&';\n"
186 " long long_value default -1;\n"
187 " boolean boolean_value default FALSE;\n"
188 "};\n"
189 ).assert_annotation_decl ("::@test_annotation_3");
190 t.assert_annotation_member_count (test_annotation_3, 4);
192 AST_Annotation_Member *short_value =
193 t.get_annotation_member (test_annotation_3, "short_value");
194 t.assert_annotation_member_type (short_value, AST_Expression::EV_short);
195 t.assert_annotation_member_value<short, ACE_CDR::Short> (short_value, 1);
197 AST_Annotation_Member *char_value =
198 t.get_annotation_member (test_annotation_3, "char_value");
199 t.assert_annotation_member_type (char_value, AST_Expression::EV_char);
200 t.assert_annotation_member_value<char, ACE_CDR::Char> (char_value, '&');
202 AST_Annotation_Member *long_value =
203 t.get_annotation_member (test_annotation_3, "long_value");
204 t.assert_annotation_member_type (long_value, AST_Expression::EV_long);
205 t.assert_annotation_member_value<int, ACE_CDR::Long> (long_value, -1);
207 AST_Annotation_Member *boolean_value =
208 t.get_annotation_member (test_annotation_3, "boolean_value");
209 t.assert_annotation_member_type (boolean_value, AST_Expression::EV_bool);
210 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (boolean_value, false);
211 } catch (Failed const &) {}
213 AST_Annotation_Decl *test_annotation_4 = 0;
214 try {
215 Annotation_Test t ("Annotation Declaration with Mixed Members");
216 test_annotation_4 = t.run (
217 "@annotation test_annotation_4 {\n"
218 " short x;\n"
219 " short y default 0;\n"
220 "};\n"
221 ).assert_annotation_decl ("::@test_annotation_4");
222 t.assert_annotation_member_count (test_annotation_4, 2);
224 AST_Annotation_Member *x =
225 t.get_annotation_member (test_annotation_4, "x");
226 t.assert_annotation_member_type (x, AST_Expression::EV_short);
227 t.assert_annotation_member_no_value (x);
229 AST_Annotation_Member *y =
230 t.get_annotation_member (test_annotation_4, "y");
231 t.assert_annotation_member_type (y, AST_Expression::EV_short);
232 t.assert_annotation_member_value<short, ACE_CDR::Short> (y, 0);
233 } catch (Failed const &) {}
235 AST_Annotation_Decl *test_annotation_in_module = 0;
236 try {
237 Annotation_Test t ("Annotation Declaration In Module");
238 test_annotation_in_module = t.run (
239 "module module_with_annotation_decl {\n"
240 " @annotation test_annotation {\n"
241 " };\n"
242 "};\n"
243 ).assert_annotation_decl (
244 "::module_with_annotation_decl::@test_annotation");
245 t.assert_annotation_member_count (test_annotation_in_module, 0);
246 } catch (Failed const &) {}
248 AST_Annotation_Decl *enum_annotation = 0;
249 AST_Expression *enum_annotation_a = 0;
250 AST_Expression *enum_annotation_b = 0;
251 AST_Expression *enum_annotation_c = 0;
252 try {
253 Annotation_Test t ("Annotation Declaration with Enum");
254 enum_annotation = t.run (
255 "@annotation enum_annotation {\n"
256 " enum Enum_t {\n"
257 " A,\n"
258 " B,\n"
259 " C\n"
260 " };\n"
261 " Enum_t value default A;\n"
262 "};\n"
263 ).assert_annotation_decl ("@enum_annotation");
264 t.assert_annotation_member_count (enum_annotation, 1);
265 t.set_scope (enum_annotation);
266 AST_Annotation_Member *value =
267 t.get_annotation_member (enum_annotation, "value");
269 AST_EnumVal *a = t.assert_node<AST_EnumVal> ("A");
270 enum_annotation_a = a->constant_value ();
272 AST_EnumVal *b = t.assert_node<AST_EnumVal> ("B");
273 enum_annotation_b = b->constant_value ();
275 AST_EnumVal *c = t.assert_node<AST_EnumVal> ("C");
276 enum_annotation_c = c->constant_value ();
278 t.assert_annotation_member_value (value, enum_annotation_a);
279 } catch (Failed const &) {}
281 AST_Annotation_Decl *string_annotation = 0;
282 try {
283 Annotation_Test t ("Annotation Declaration with String");
284 string_annotation = t.run (
285 "@annotation string_annotation {\n"
286 " string value default \"This is some text\";\n"
287 "};\n"
288 ).assert_annotation_decl ("@string_annotation");
289 t.assert_annotation_member_count (string_annotation, 1);
290 AST_Annotation_Member *value =
291 t.get_annotation_member (string_annotation, "value");
293 UTL_String test_string ("This is some text");
294 t.assert_annotation_member_value<UTL_String*, UTL_String*>
295 (value, &test_string);
296 } catch (Failed const &) {}
298 AST_Expression *constant_annotation_x = 0;
299 AST_Expression *constant_annotation_y = 0;
300 AST_Annotation_Decl *constant_annotation = 0;
301 try {
302 Annotation_Test t ("Annotation Declaration with Constant");
303 constant_annotation = t.run (
304 "@annotation constant_annotation {\n"
305 " const short X = 4;\n"
306 " const short Y = 5;\n"
307 " short value default X;\n"
308 "};\n"
309 ).assert_annotation_decl ("@constant_annotation");
310 t.assert_annotation_member_count (constant_annotation, 1);
311 t.set_scope (constant_annotation);
312 AST_Annotation_Member *value =
313 t.get_annotation_member (constant_annotation, "value");
315 constant_annotation_x = t.assert_node<AST_Constant> ("X")->constant_value ();
316 constant_annotation_y = t.assert_node<AST_Constant> ("Y")->constant_value ();
318 t.assert_annotation_member_value (value, constant_annotation_x);
319 } catch (Failed const &) {}
321 AST_Annotation_Decl *boolean_annotation = 0;
322 try {
323 Annotation_Test t ("Annotation Declaration with Single Boolean");
324 boolean_annotation = t.run (
325 "@annotation boolean_annotation {\n"
326 " boolean value default TRUE;\n"
327 "};\n"
328 ).assert_annotation_decl ("@boolean_annotation");
329 t.assert_annotation_member_count (boolean_annotation, 1);
330 AST_Annotation_Member *value =
331 t.get_annotation_member (boolean_annotation, "value");
332 t.assert_annotation_member_type (value, AST_Expression::EV_bool);
333 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (value, true);
334 } catch (Failed const &) {}
336 /* -------------------------------------------------------------------------
337 * Annotations Applications
338 * -------------------------------------------------------------------------
339 * These tests assert that annotations can be applied to various IDL
340 * constructs.
343 try {
344 Annotation_Test t ("Module Annotation Application");
345 AST_Decl *module1 = t.run (
346 "@test_annotation_1\n"
347 "module module1 {\n"
348 " struct struct_in_module1 {\n"
349 " short member;\n"
350 " };\n"
351 "};\n"
352 ).assert_node ("::module1");
353 t.assert_annotation_appl_count (module1, 1);
354 t.assert_annotation_appl (module1, 0, test_annotation_1);
355 } catch (Failed const &) {}
357 try {
358 Annotation_Test t ("Struct Annotation Application");
359 AST_Decl *struct1 = t.run (
360 "@test_annotation_1\n"
361 "struct struct1 {\n"
362 " short member;\n"
363 "};\n"
364 ).assert_node ("::struct1");
365 t.assert_annotation_appl_count (struct1, 1);
366 t.assert_annotation_appl (struct1, 0, test_annotation_1);
367 } catch (Failed const &) {}
369 try {
370 Annotation_Test t ("Typedef Annotation Application");
371 t.run (
372 "@enum_annotation\n"
373 "typedef short short_int;\n"
374 "@string_annotation\n"
375 "typedef short_int small_int;\n"
376 "@test_annotation_1\n"
377 "typedef small_int i16;\n"
378 "struct struct6 {\n"
379 " i16 member;\n"
380 "};\n"
383 // Assert short_int has enum_annotation
384 AST_Decl *short_int = t.assert_node ("short_int");
385 t.assert_annotation_appl_count (short_int, 1);
386 t.assert_annotation_appl (short_int, 0, enum_annotation);
388 // Get type of member
389 AST_Field *member= t.assert_node<AST_Field> ("struct6::member");
390 AST_Type* type = member->field_type ();
392 // Assert type has enum_annotation, string_annotation, and
393 // test_annotation_1.
394 t.assert_annotation_appl_count (type, 3);
395 t.assert_annotation_appl (type, 0, enum_annotation);
396 t.assert_annotation_appl (type, 1, string_annotation);
397 t.assert_annotation_appl (type, 2, test_annotation_1);
398 } catch (Failed const &) {}
400 try {
401 Annotation_Test t ("Sequence Type Parameter Annotation Application");
402 AST_Field *value = t.run (
403 "typedef sequence<@test_annotation_1 short, 5> test_seq_t;\n"
404 "struct struct7 {\n"
405 " test_seq_t value;\n"
406 "};\n"
407 ).assert_node<AST_Field> ("::struct7::value");
409 // Get Sequence
410 AST_Typedef *typedef_node = dynamic_cast<AST_Typedef *> (value->field_type ());
411 if (!typedef_node) t.failed ("Could not get AST_Typedef");
412 AST_Sequence *seq = dynamic_cast<AST_Sequence *> (typedef_node->base_type ());
413 if (!seq) t.failed ("Could get AST_Sequence");
415 // Verify Annotation on Base Type
416 AST_Annotation_Appls &annotations = seq->base_type_annotations ();
417 size_t count = annotations.size ();
418 if (count != 1)
420 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Annotation Test Error: %C:\n")
421 ACE_TEXT ("expected one annotation on test_seq_t base type, ")
422 ACE_TEXT ("it has %d annotations!\n"),
423 t.name_.c_str (), count));
424 t.failed ();
426 AST_Annotation_Appl *annotation = annotations[0];
427 if (!annotation)
429 t.failed ("annotation for test_seq_t base type is null!");
431 if (annotation->annotation_decl () != test_annotation_1)
433 UTL_ScopedName *scopedname = annotation->name ();
434 const char *name = scopedname ?
435 scopedname-> get_string_copy () : "UNKNOWN";
436 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Annotation Test Error: %C:\n")
437 ACE_TEXT ("expected annotation for test_seq_t base type to be ")
438 ACE_TEXT ("test_annotation_1, but it was %C\n"),
439 t.name_.c_str (), name));
440 if (scopedname)
442 delete [] name;
444 t.failed ();
446 } catch (Failed const &) {}
448 try {
449 Annotation_Test t ("Constant Declarations Annotation Application");
450 AST_Decl *test_const = t.run (
451 "@test_annotation_1\n"
452 "const short test_const = 5;\n"
453 ).assert_node ("test_const");
454 t.assert_annotation_appl_count (test_const, 1);
455 t.assert_annotation_appl (test_const, 0, test_annotation_1);
456 } catch (Failed const &) {}
458 try {
459 Annotation_Test t ("Multiple Annotation Applications");
460 AST_Decl *struct3 = t.run (
461 "@test_annotation_1\n"
462 "@test_annotation_1\n"
463 "struct struct3 {\n"
464 " short test_member_1;\n"
465 "};\n"
466 ).assert_node ("struct3");
467 t.assert_annotation_appl_count (struct3, 2);
468 t.assert_annotation_appl (struct3, 0, test_annotation_1);
469 t.assert_annotation_appl (struct3, 1, test_annotation_1);
470 } catch (Failed const &) {}
472 try {
473 Annotation_Test t ("Annotation Application with a Single Parameter");
474 AST_Decl *struct4 = t.run (
475 "@test_annotation_4 (100)\n"
476 "struct struct4 {\n"
477 " short test_member_1;\n"
478 "};\n"
479 ).assert_node ("struct4");
480 t.assert_annotation_appl_count (struct4, 1);
481 AST_Annotation_Appl *appl =
482 t.assert_annotation_appl (struct4, 0, test_annotation_4);
483 t.assert_annotation_member_count (appl, 2);
485 AST_Annotation_Member *x = t.get_annotation_member (appl, "x");
486 t.assert_annotation_member_value<short, ACE_CDR::Short> (x, 100);
488 AST_Annotation_Member *y = t.get_annotation_member (appl, "y");
489 t.assert_annotation_member_value<short, ACE_CDR::Short> (y, 0);
490 } catch (Failed const &) {}
492 try {
493 Annotation_Test t ("Annotation Application with Named Parameters");
494 AST_Decl *struct2 = t.run (
495 "@test_annotation_4 (x = 101, y = 102)\n"
496 "struct struct2 {\n"
497 " short test_member_1;\n"
498 "};\n"
499 ).assert_node ("struct2");
500 t.assert_annotation_appl_count (struct2, 1);
501 AST_Annotation_Appl *appl =
502 t.assert_annotation_appl (struct2, 0, test_annotation_4);
503 t.assert_annotation_member_count (appl, 2);
505 AST_Annotation_Member *x = t.get_annotation_member (appl, "x");
506 t.assert_annotation_member_value<short, ACE_CDR::Short> (x, 101);
508 AST_Annotation_Member *y = t.get_annotation_member (appl, "y");
509 t.assert_annotation_member_value<short, ACE_CDR::Short> (y, 102);
510 } catch (Failed const &) {}
512 try {
513 Annotation_Test t ("Annotation Applications with Scoped Names");
514 AST_Decl *struct5 = t.run (
515 "@module_with_annotation_decl::test_annotation\n"
516 "@::module_with_annotation_decl::test_annotation\n"
517 "struct struct5 {\n"
518 " short test_member_1;\n"
519 "};\n"
520 ).assert_node ("struct5");
521 t.assert_annotation_appl_count (struct5, 2);
522 t.assert_annotation_appl (struct5, 0, test_annotation_in_module);
523 t.assert_annotation_appl (struct5, 1, test_annotation_in_module);
524 } catch (Failed const &) {}
526 try {
527 Annotation_Test t ("Annotation Applications on/in Unions");
528 AST_Union *test_union = t.run (
529 /* Annotations on the union and the discriminator */
530 "@test_annotation_1\n"
531 "union test_union switch (@test_annotation_1 short) {\n"
532 "case 0:\n"
533 "case 1:\n"
534 /* Annotation on a Union Member */
535 " @test_annotation_1 short union_member_1;\n"
536 "default:\n"
537 " short union_member_2;\n"
538 "};\n"
539 ).assert_node<AST_Union> ("test_union");
541 // Annotation On Union
542 t.assert_annotation_appl_count (test_union, 1);
543 t.assert_annotation_appl (test_union, 0, test_annotation_1);
545 // Annotation On Discriminator
546 AST_Annotation_Appls &annotations = test_union->disc_annotations ();
547 size_t count = annotations.size ();
548 if (count != 1)
550 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Annotation Test Error: %C:\n")
551 ACE_TEXT ("expected one annotation on test_union discriminator, ")
552 ACE_TEXT ("it has %d annotations!\n"),
553 t.name_.c_str (), count));
554 t.failed ();
556 AST_Annotation_Appl *annotation = annotations[0];
557 if (!annotation)
559 t.failed ("annotation for test_seq_t base type is null!");
561 if (annotation->annotation_decl () != test_annotation_1)
563 UTL_ScopedName *scopedname = annotation->name ();
564 const char *name = scopedname ?
565 scopedname-> get_string_copy () : "UNKNOWN";
566 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Annotation Test Error: %C:\n")
567 ACE_TEXT ("expected annotation for test_union discriminator to be ")
568 ACE_TEXT ("test_annotation_1, but it was %C\n"),
569 t.name_.c_str (), name));
570 if (scopedname)
572 delete [] name;
574 t.failed ();
577 // Annotation on Union Member
578 AST_Decl *union_member_1 =
579 t.assert_node ("test_union::union_member_1");
580 t.assert_annotation_appl_count (union_member_1, 1);
581 t.assert_annotation_appl (union_member_1, 0, test_annotation_1);
582 } catch (Failed const &) {}
584 try {
585 Annotation_Test t ("Annotation Applications on/in Enums");
586 AST_Decl *Test_Enum = t.run (
587 /* Annotation on the enum */
588 "@test_annotation_1\n"
589 "enum Test_Enum {\n"
590 " TEST_ENUM_MEMBER_1,\n"
591 /* Annotation on a enumerator */
592 " @test_annotation_1 TEST_ENUM_MEMBER_2,\n"
593 " TEST_ENUM_MEMBER_3\n"
594 "};\n"
595 ).assert_node ("Test_Enum");
597 // Annotation on Enum
598 t.assert_annotation_appl_count (Test_Enum, 1);
599 t.assert_annotation_appl (Test_Enum, 0, test_annotation_1);
601 // Annotation on Enum Member
602 AST_Decl *TEST_ENUM_MEMBER_2 =
603 t.assert_node ("Test_Enum::TEST_ENUM_MEMBER_2");
604 t.assert_annotation_appl_count (TEST_ENUM_MEMBER_2, 1);
605 t.assert_annotation_appl (TEST_ENUM_MEMBER_2, 0, test_annotation_1);
606 } catch (Failed const &) {}
608 try {
609 Annotation_Test t ("By Default, Unknown Annotation Application Causes Warning");
610 t.last_warning (UTL_Error::EIDL_LOOKUP_ERROR);
611 t.disable_output ();
612 t.run (
613 "struct struct11 {\n"
614 " @fake_annotation(fake_param=FAKE_CONSTANT)\n"
615 " short member;\n"
616 "};\n"
618 } catch (Failed const &) {}
620 try {
621 idl_global->unknown_annotations_ =
622 IDL_GlobalData::UNKNOWN_ANNOTATIONS_ERROR;
623 Annotation_Test t ("Optionally, Unknown Annotation Application Causes Err0r");
624 // Any mention of "Error" will be picked up by scoreboard ^^^
625 t.last_error (UTL_Error::EIDL_LOOKUP_ERROR).error_count (1);
626 t.disable_output ();
627 t.run (
628 "struct struct10 {\n"
629 " @fake_annotation(fake_param=FAKE_CONSTANT)\n"
630 " short member;\n"
631 "};\n"
633 // Restore Default Behaivor
634 idl_global->unknown_annotations_ =
635 IDL_GlobalData::UNKNOWN_ANNOTATIONS_WARN_ONCE;
636 } catch (Failed const &) {}
638 try {
639 Annotation_Test t ("Annotation Application with Enum");
640 AST_Decl *value = t.run (
641 "struct struct8 {\n"
642 " @enum_annotation\n" // A
643 " @enum_annotation(B)\n"
644 " @enum_annotation(value=C)\n"
645 " short value;\n"
646 "};\n"
647 ).assert_node ("struct8::value");
648 t.assert_annotation_appl_count (value, 3);
649 AST_Annotation_Member *member;
651 AST_Annotation_Appl *first =
652 t.assert_annotation_appl (value, 0, enum_annotation);
653 member = t.get_annotation_member (first, "value");
654 t.assert_annotation_member_value (member, enum_annotation_a);
656 AST_Annotation_Appl *second =
657 t.assert_annotation_appl (value, 1, enum_annotation);
658 member = t.get_annotation_member (second, "value");
659 t.assert_annotation_member_value (member, enum_annotation_b);
661 AST_Annotation_Appl *third =
662 t.assert_annotation_appl (value, 2, enum_annotation);
663 member = t.get_annotation_member (third, "value");
664 t.assert_annotation_member_value (member, enum_annotation_c);
665 } catch (Failed const &) {}
667 try {
668 Annotation_Test t ("Annotation Application with String");
669 AST_Decl *value = t.run (
670 "struct struct9 {\n"
671 " @string_annotation\n" // A
672 " @string_annotation(\"Something else\")\n"
673 " @string_annotation(value=\"One last thing\")\n"
674 " short value;\n"
675 "};\n"
676 ).assert_node ("struct9::value");
677 t.assert_annotation_appl_count (value, 3);
678 AST_Annotation_Member *member;
679 AST_Annotation_Appl *annotation;
681 UTL_String first_string ("This is some text");
682 annotation = t.assert_annotation_appl (value, 0, string_annotation);
683 member = t.get_annotation_member (annotation, "value");
684 t.assert_annotation_member_value <UTL_String *, UTL_String *>
685 (member, &first_string);
687 UTL_String second_string ("Something else");
688 annotation = t.assert_annotation_appl (value, 1, string_annotation);
689 member = t.get_annotation_member (annotation, "value");
690 t.assert_annotation_member_value <UTL_String *, UTL_String *>
691 (member, &second_string);
693 UTL_String third_string ("One last thing");
694 annotation = t.assert_annotation_appl (value, 2, string_annotation);
695 member = t.get_annotation_member (annotation, "value");
696 t.assert_annotation_member_value <UTL_String *, UTL_String *>
697 (member, &third_string);
698 } catch (Failed const &) {}
700 try {
701 Annotation_Test t ("Annotation Application with Constant");
702 AST_Decl *value = t.run (
703 "struct struct12 {\n"
704 " @constant_annotation\n" // A
705 " @constant_annotation(Y)\n"
706 " @constant_annotation(100)\n"
707 " short value;\n"
708 "};\n"
709 ).assert_node ("struct12::value");
710 t.assert_annotation_appl_count (value, 3);
711 AST_Annotation_Member *member;
712 AST_Annotation_Appl *annotation;
714 annotation = t.assert_annotation_appl (value, 0, constant_annotation);
715 member = t.get_annotation_member (annotation, "value");
716 t.assert_annotation_member_value (member, constant_annotation_x);
718 annotation = t.assert_annotation_appl (value, 1, constant_annotation);
719 member = t.get_annotation_member (annotation, "value");
720 t.assert_annotation_member_value (member, constant_annotation_y);
722 annotation = t.assert_annotation_appl (value, 2, constant_annotation);
723 member = t.get_annotation_member (annotation, "value");
724 t.assert_annotation_member_value<short, ACE_CDR::Short> (member, 100);
725 } catch (Failed const &) {}
727 try {
728 Annotation_Test t ("Annotate Array Base Type");
729 AST_Typedef *thetypedef = t.run (
730 "typedef struct12 struct12Array @test_annotation_1 [12];\n"
731 ).assert_node<AST_Typedef> ("::struct12Array");
732 AST_Array *struct12Array =
733 dynamic_cast<AST_Array *> (thetypedef->base_type ());
734 if (!struct12Array) t.failed ("Could not get AST_Array");
736 // Verify Annotation on Base Type
737 AST_Annotation_Appls &annotations =
738 struct12Array->base_type_annotations ();
739 size_t count = annotations.size ();
740 if (count != 1)
742 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Annotation Test Error: %C:\n")
743 ACE_TEXT ("expected one annotation on struct12Array base type, ")
744 ACE_TEXT ("it has %d annotations!\n"),
745 t.name_.c_str (), count));
746 t.failed ();
748 AST_Annotation_Appl *annotation = annotations[0];
749 if (!annotation)
751 t.failed ("annotation for struct12Array base type is null!");
753 if (annotation->annotation_decl () != test_annotation_1)
755 UTL_ScopedName *scopedname = annotation->name ();
756 const char *name = scopedname ?
757 scopedname-> get_string_copy () : "UNKNOWN";
758 ACE_ERROR ((LM_ERROR, ACE_TEXT ("Annotation Test Error: %C:\n")
759 ACE_TEXT ("expected annotation for struct12Array base type to be ")
760 ACE_TEXT ("test_annotation_1, but it was %C\n"),
761 t.name_.c_str (), name));
762 if (scopedname)
764 delete [] name;
766 t.failed ();
768 } catch (Failed const &) {}
770 try {
771 Annotation_Test t ("Annotation Application with Single Boolean");
772 t.run (
773 "struct struct13 {\n"
774 " @boolean_annotation\n"
775 " short test_member_1;\n"
776 " @boolean_annotation (TRUE)\n"
777 " short test_member_2;\n"
778 " @boolean_annotation (FALSE)\n"
779 " short test_member_3;\n"
780 " @boolean_annotation (value = TRUE)\n"
781 " short test_member_4;\n"
782 " @boolean_annotation (value = FALSE)\n"
783 " short test_member_5;\n"
784 "};\n"
787 AST_Decl *struct_member = 0;
788 AST_Annotation_Appl *appl = 0;
790 struct_member = t.assert_node ("struct13::test_member_1");
791 t.assert_annotation_appl_count (struct_member, 1);
792 appl = t.assert_annotation_appl (struct_member, 0, boolean_annotation);
793 t.assert_annotation_member_count (appl, 1);
794 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (
795 t.get_annotation_member (appl, "value"), true);
797 struct_member = t.assert_node ("struct13::test_member_2");
798 t.assert_annotation_appl_count (struct_member, 1);
799 appl = t.assert_annotation_appl (struct_member, 0, boolean_annotation);
800 t.assert_annotation_member_count (appl, 1);
801 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (
802 t.get_annotation_member (appl, "value"), true);
804 struct_member = t.assert_node ("struct13::test_member_3");
805 t.assert_annotation_appl_count (struct_member, 1);
806 appl = t.assert_annotation_appl (struct_member, 0, boolean_annotation);
807 t.assert_annotation_member_count (appl, 1);
808 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (
809 t.get_annotation_member (appl, "value"), false);
811 struct_member = t.assert_node ("struct13::test_member_4");
812 t.assert_annotation_appl_count (struct_member, 1);
813 appl = t.assert_annotation_appl (struct_member, 0, boolean_annotation);
814 t.assert_annotation_member_count (appl, 1);
815 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (
816 t.get_annotation_member (appl, "value"), true);
818 struct_member = t.assert_node ("struct13::test_member_5");
819 t.assert_annotation_appl_count (struct_member, 1);
820 appl = t.assert_annotation_appl (struct_member, 0, boolean_annotation);
821 t.assert_annotation_member_count (appl, 1);
822 t.assert_annotation_member_value<bool, ACE_CDR::Boolean> (
823 t.get_annotation_member (appl, "value"), false);
824 } catch (Failed const &) {}
826 try {
827 Annotation_Test t ("Annotations on and in Interfaces");
828 t.run ((std::string () +
829 "@test_annotation_1\n"
830 "interface interface1 {\n"
831 // export
832 + common_export_idl +
833 "};\n"
834 ).c_str ());
836 AST_Interface *interface1 = t.assert_node<AST_Interface> ("interface1");
837 t.assert_annotation_appl_count (interface1, 1);
838 t.assert_annotation_appl (interface1, 0, test_annotation_1);
839 t.set_scope (interface1);
840 assert_common_export_idl (t, test_annotation_1);
841 } catch (Failed const &) {}
843 try {
844 Annotation_Test t ("Annotations on and in Valuetypes");
845 t.run ((std::string () +
846 "@test_annotation_1\n"
847 "valuetype valuetype1 {\n"
848 // value_element
849 + common_value_element_idl +
850 "};\n"
851 ).c_str ());
853 AST_ValueType *valuetype1 = t.assert_node<AST_ValueType> ("valuetype1");
854 t.assert_annotation_appl_count (valuetype1, 1);
855 t.assert_annotation_appl (valuetype1, 0, test_annotation_1);
856 t.set_scope (valuetype1);
857 assert_common_value_element_idl (t, test_annotation_1);
858 } catch (Failed const &) {}
860 try {
861 Annotation_Test t ("Annotations on and in Porttypes");
862 t.run ((std::string () +
863 "@test_annotation_1\n"
864 "porttype port_with_provides {\n"
865 // port_ref
866 " @test_annotation_1\n"
867 " provides interface1 provides_value;\n"
868 // port_export
869 + common_attr_dcl_idl +
870 "};\n"
871 "\n"
872 "@test_annotation_1\n"
873 "porttype port_with_uses {\n"
874 // port_ref
875 " @test_annotation_1\n"
876 " uses interface1 uses_value;\n"
877 // port_export
878 + common_attr_dcl_idl +
879 "};\n"
880 ).c_str ());
882 AST_PortType *port_with_provides =
883 t.assert_node<AST_PortType> ("port_with_provides");
884 t.assert_annotation_appl_count (port_with_provides, 1);
885 t.assert_annotation_appl (port_with_provides, 0, test_annotation_1);
886 t.set_scope (port_with_provides);
887 assert_node_has_annotation (t, "provides_value", test_annotation_1);
888 assert_common_attr_dcl_idl (t, test_annotation_1);
890 AST_PortType *port_with_uses =
891 t.assert_node<AST_PortType> ("port_with_uses");
892 t.assert_annotation_appl_count (port_with_uses, 1);
893 t.assert_annotation_appl (port_with_uses, 0, test_annotation_1);
894 t.set_scope (port_with_uses);
895 assert_node_has_annotation (t, "uses_value", test_annotation_1);
896 assert_common_attr_dcl_idl (t, test_annotation_1);
897 } catch (Failed const &) {}
899 try {
900 Annotation_Test t ("Annotations on and in Eventtypes");
901 t.run ((std::string () +
902 "@test_annotation_1\n"
903 "eventtype event1 {\n"
904 + common_value_element_idl +
905 "};\n"
906 ).c_str ());
907 AST_EventType *event1 = t.assert_node<AST_EventType> ("event1");
908 t.assert_annotation_appl_count (event1, 1);
909 t.assert_annotation_appl (event1, 0, test_annotation_1);
910 t.set_scope (event1);
911 assert_common_value_element_idl (t, test_annotation_1);
912 } catch (Failed const &) {}
914 try {
915 Annotation_Test t ("Annotations on and in Components");
916 t.run ((std::string () +
917 "@test_annotation_1\n"
918 "component component1 {\n"
919 // provides_dcl
920 " @test_annotation_1\n"
921 " provides interface1 provides_value;\n"
922 // uses_dcl
923 " @test_annotation_1\n"
924 " uses interface1 uses_value;\n"
925 // attr_dcl
926 + common_attr_dcl_idl +
927 // port_dcl
928 " @test_annotation_1\n"
929 " port port_with_uses port_value;\n"
930 // emits_dcl
931 " @test_annotation_1\n"
932 " emits event1 emits_value;\n"
933 // publishes_dcl
934 " @test_annotation_1\n"
935 " publishes event1 publishes_value;\n"
936 // consumes_dcl
937 " @test_annotation_1\n"
938 " consumes event1 consumes_value;\n"
939 "};\n"
940 ).c_str ());
941 AST_Component *component1 = t.assert_node<AST_Component> ("component1");
942 t.assert_annotation_appl_count (component1, 1);
943 t.assert_annotation_appl (component1, 0, test_annotation_1);
944 t.set_scope (component1);
945 assert_node_has_annotation (t, "provides_value", test_annotation_1);
946 assert_node_has_annotation (t, "uses_value", test_annotation_1);
947 assert_common_attr_dcl_idl (t, test_annotation_1);
948 assert_node_has_annotation (t, "port_value", test_annotation_1);
949 assert_node_has_annotation (t, "emits_value", test_annotation_1);
950 assert_node_has_annotation (t, "publishes_value", test_annotation_1);
951 assert_node_has_annotation (t, "consumes_value", test_annotation_1);
952 } catch (Failed const &) {}
955 * Test for https://github.com/DOCGroup/ACE_TAO/issues/997
957 * When the original annotation work (https://github.com/DOCGroup/ACE_TAO/pull/723)
958 * was done it was assumed that when annotations didn't define the symbol
959 * being used, the lookup would go up the scope stack to the current scope.
960 * This turned out not the case, so this functionality was implemented just
961 * for annotation parameters.
963 try {
964 Annotation_Test t ("Passing Constant from Module");
965 t.run (
966 "@annotation range_test_annotation {\n"
967 " float min;\n"
968 " float max;\n"
969 "};\n"
970 "\n"
971 "module range_test_annoation_module {\n"
972 " const float f1 = 1.;\n"
973 " const float f2 = 2.;\n"
974 "\n"
975 " @range_test_annotation(min = f1, max = f2)\n"
976 " @range_test_annotation(\n"
977 " min = range_test_annoation_module::f1,\n"
978 " max = range_test_annoation_module::f2)\n"
979 " @range_test_annotation(\n"
980 " min = ::range_test_annoation_module::f1,\n"
981 " max = ::range_test_annoation_module::f2)\n"
982 " typedef float RangedFloat;\n"
983 "};\n"
986 AST_Annotation_Decl *range_like_test_annotation =
987 t.assert_annotation_decl ("::@range_test_annotation");
988 AST_Decl *RangedFloat = t.assert_node (
989 "::range_test_annoation_module::RangedFloat");
990 t.assert_annotation_appl_count (RangedFloat, 3);
991 t.assert_annotation_appl (RangedFloat, 0, range_like_test_annotation);
992 t.assert_annotation_appl (RangedFloat, 1, range_like_test_annotation);
993 t.assert_annotation_appl (RangedFloat, 2, range_like_test_annotation);
994 } catch (Failed const &) {}
996 /* -------------------------------------------------------------------------
997 * Annotation Names
998 * -------------------------------------------------------------------------
999 * These tests assert various aspects of how annotations work in regards to
1000 * naming.
1003 try {
1004 Annotation_Test t ("Annotation and Non-Annotation Names Can't Clash");
1005 t.run (
1006 "@annotation samename {\n"
1007 "};\n"
1008 "struct samename {\n"
1009 " short member;\n"
1010 "};"
1012 } catch (Failed const &) {}
1014 try {
1015 Annotation_Test t ("Annotation Names Can't Be \"annotation\"");
1016 t.last_error (UTL_Error::EIDL_MISC).error_count (1);
1017 t.disable_output ();
1018 t.run (
1019 "@annotation annotation {\n"
1020 "};\n"
1022 } catch (Failed const &) {}
1024 try {
1025 Annotation_Test t ("Annotation Names Can Start with \"annotation\"");
1026 t.run (
1027 "@annotation annotationannotation {\n"
1028 "};\n"
1029 "@annotationannotation\n"
1030 "struct annotationannotation_struct {\n"
1031 " short member;\n"
1032 "};\n"
1033 ).assert_annotation_decl ("::@annotationannotation");
1034 } catch (Failed const &) {}
1036 /* -------------------------------------------------------------------------
1037 * Struct Field Visibility Must be vis_NA
1038 * -------------------------------------------------------------------------
1039 * Test for: https://github.com/DOCGroup/ACE_TAO/issues/784
1041 * In the bison file, visibility for valuetype state members (which are the
1042 * same class as normal fields) was being passed through the bison stack.
1043 * When adding support for annotations, the grammar was changed and it was
1044 * broken, causing bogus data to be passed to regular struct field as their
1045 * visibility.
1047 * This is a test to assert that struct fields have vis_NA. This can't be put
1048 * anywhere else at the moment because this is the only test that's an
1049 * instance of the idl compiler.
1051 try {
1052 Annotation_Test t ("Struct Field Visibility Must be vis_NA");
1053 AST_Field *member = t.assert_node<AST_Field> ("struct1::member");
1054 if (member->visibility () != AST_Field::vis_NA)
1056 char buffer[100];
1057 ACE_OS::snprintf (&buffer[0], 100,
1058 "struct field visibility is %u, which is not equal to vis_NA",
1059 static_cast<unsigned> (member->visibility ()));
1060 t.failed (&buffer[0]);
1062 } catch (Failed const &) {}
1064 /* -------------------------------------------------------------------------
1065 * Empty union cases aliasing the default case must always be evaluated
1066 * -------------------------------------------------------------------------
1067 * When the union has an enum discriminator, and one or more empty cases
1068 * acting as an alias to the default case the IDL compiler was failing to
1069 * resolve the ordinal value for these empty labels and this causes trouble
1070 * for at least OpenDDS.
1072 * This test is designed to verify that the condition is corrected by
1073 * parsing a specially crafted union and validating the value of the
1074 * label aliasing the default case.
1076 try {
1077 Annotation_Test t ("empty union branch label");
1078 AST_Union *test_union = t.run (
1079 "enum disc {A, B, C};\n"
1080 "union empty_union switch (disc) {\n"
1081 "case A: long along;\n"
1082 "case B: short bshort;\n"
1083 "case C:\n"
1084 "default: float cfloat;\n"
1085 "};\n").assert_node<AST_Union>("::empty_union");
1086 AST_Field **af = 0;
1087 test_union->field(af, 2);
1088 AST_UnionBranch *ub = dynamic_cast<AST_UnionBranch *>(*af);
1089 if (ub != nullptr)
1091 AST_UnionLabel *ul = ub->label ();
1092 if (ul != nullptr)
1094 if (ul->label_val()->ev()->u.ulval != 2)
1096 t.failed("did not get the correct label value");
1100 } catch (Failed const &) {}
1102 // Done, Print Overall Results
1103 Annotation_Test::results ();