1 #include "Annotation_Test.h"
3 #include <global_extern.h>
5 unsigned Annotation_Test::failed_test_count_
= 0;
6 unsigned Annotation_Test::total_test_count_
= 0;
8 Annotation_Test::Annotation_Test (const std::string
&name
)
12 last_error_ (UTL_Error::EIDL_OK
),
13 last_warning_ (UTL_Error::EIDL_OK
),
15 disable_output_ (false)
20 Annotation_Test::~Annotation_Test ()
22 if (idl_global
->err ()->last_error
== UTL_Error::EIDL_SYNTAX_ERROR
)
25 ACE_TEXT ("Annotation Test: %C: ")
26 ACE_TEXT ("FAILED because of syntax error in:\n"),
29 print_idl_with_line_numbers ();
32 ACE_TEXT ("Check syntax error message above for more information.\n"),
33 ACE_TEXT ("Failures beyond this might be false positives.\n")));
39 ACE_TEXT ("Annotation Test: %C: PASSED\n"), name_
.c_str ()));
42 idl_global
->err ()->reset_last_error_and_warning ();
46 Annotation_Test::failed (const std::string
&message
)
48 if (message
.length ())
51 ACE_TEXT ("Annotation Test Error: %C: %C\n"),
52 name_
.c_str (), message
.c_str ()));
55 ACE_TEXT ("Annotation Test: %C: FAILED\nFailed IDL:\n"),
57 print_idl_with_line_numbers ();
64 Annotation_Test::error_count (int error_count
)
66 error_count_
= error_count
;
71 Annotation_Test::last_error (UTL_Error::ErrorCode last_error
)
73 last_error_
= last_error
;
78 Annotation_Test::last_warning (UTL_Error::ErrorCode last_warning
)
80 last_warning_
= last_warning
;
85 Annotation_Test::run (const std::string
&idl
)
88 idl_global
->set_err_count (0);
89 idl_global
->err ()->last_error
= UTL_Error::EIDL_OK
;
90 idl_global
->err ()->last_warning
= UTL_Error::EIDL_OK
;
94 idl_global
->eval (idl
.c_str (), disable_output_
);
97 if (idl_global
->err_count () != error_count_
)
100 ACE_ERROR ((LM_ERROR
,
101 ACE_TEXT ("Annotation Test Error: %C:\nError Count: expecting %d, got %d!\n"),
102 name_
.c_str (), error_count_
, idl_global
->err_count ()));
104 if (idl_global
->err ()->last_error
!= last_error_
)
107 ACE_ERROR ((LM_ERROR
,
108 ACE_TEXT ("Annotation Test Error: %C:\n")
109 ACE_TEXT ("Last Error Code (UTL_Error::ErrorCode): expecting "),
111 if (last_error_
== UTL_Error::EIDL_OK
)
113 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("OK")));
117 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%d"), last_error_
));
119 ACE_ERROR ((LM_ERROR
, ACE_TEXT (", got ")));
120 if (idl_global
->err ()->last_error
== UTL_Error::EIDL_OK
)
122 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("OK")));
126 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%d"),
127 idl_global
->err ()->last_error
));
129 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("!\n")));
131 if (idl_global
->err ()->last_warning
!= last_warning_
)
134 ACE_ERROR ((LM_ERROR
,
135 ACE_TEXT ("Annotation Test Error: %C:\n")
136 ACE_TEXT ("Last Warning Code (UTL_Error::ErrorCode): expecting "),
138 if (last_warning_
== UTL_Error::EIDL_OK
)
140 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("OK")));
144 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%d"), last_warning_
));
146 ACE_ERROR ((LM_ERROR
, ACE_TEXT (", got ")));
147 if (idl_global
->err ()->last_warning
== UTL_Error::EIDL_OK
)
149 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("OK")));
153 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("%d"),
154 idl_global
->err ()->last_warning
));
156 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("!\n")));
167 Annotation_Test::assert_node (const char *name
, UTL_Scope
*from
)
170 UTL_ScopedName
*scoped_name
= FE_Utils::string_to_scoped_name (name
);
172 UTL_Scope
*scope
= from
? from
: (scope_
? scope_
: idl_global
->scopes ().bottom ());
176 node
= scope
->lookup_by_name (scoped_name
);
181 ACE_ERROR ((LM_ERROR
,
182 ACE_TEXT ("Annotation Test Error: %C:\n")
183 ACE_TEXT ("Failed to Find AST Node named %C!\n"),
184 name_
.c_str (), name
));
190 scoped_name
->destroy ();
197 AST_Annotation_Decl
*
198 Annotation_Test::assert_annotation_decl (const char *name
)
200 AST_Annotation_Decl
*node
= assert_node
<AST_Annotation_Decl
> (name
);
202 if (node
->node_type () != AST_Decl::NT_annotation_decl
)
204 ACE_ERROR ((LM_ERROR
,
205 ACE_TEXT ("Annotation Test Error: %C:\n")
206 ACE_TEXT ("AST Node named %C is not an AST_Annotation_Decl!\n"),
207 name_
.c_str (), name
));
215 Annotation_Test::assert_annotation_appl_count (
216 AST_Decl
*node
, size_t count
)
218 AST_Annotation_Appls
&annotations
= node
->annotations ();
219 if (annotations
.size () != count
)
221 char *node_name
= node
->name ()->get_string_copy ();
222 ACE_ERROR ((LM_ERROR
,
223 ACE_TEXT ("Annotation Test Error: %C:\n")
224 ACE_TEXT ("asserted that %C has %d annotation(s), but there are %d!\n"),
225 name_
.c_str (), node_name
, count
, annotations
.size ()));
231 AST_Annotation_Appl
*
232 Annotation_Test::assert_annotation_appl (
233 AST_Decl
*node
, size_t index
, AST_Annotation_Decl
*anno_decl
)
237 ACE_ERROR ((LM_ERROR
,
238 ACE_TEXT ("Annotation Test Error: %C:\n")
239 ACE_TEXT ("assert_annotation_appl: annotation decl is null!\n"),
244 AST_Annotation_Appls
&annotations
= node
->annotations ();
245 if (!annotations
.size ())
247 char *node_name
= node
->name ()->get_string_copy ();
248 ACE_ERROR ((LM_ERROR
,
249 ACE_TEXT ("Annotation Test Error: %C:\n")
250 ACE_TEXT ("can not access %C annotation %d, ")
251 ACE_TEXT ("it has no annotations!\n"),
252 name_
.c_str (), node_name
, index
));
257 if (index
>= annotations
.size ())
259 char *node_name
= node
->name ()->get_string_copy ();
260 ACE_ERROR ((LM_ERROR
,
261 ACE_TEXT ("Annotation Test Error: %C:\n")
262 ACE_TEXT ("can not access %C annotation %d, ")
263 ACE_TEXT ("it only has %d annotation(s)!\n"),
264 name_
.c_str (), node_name
, index
, annotations
.size ()));
269 AST_Annotation_Appl
*anno_appl
= annotations
[index
];
272 char *node_name
= node
->name ()->get_string_copy ();
273 ACE_ERROR ((LM_ERROR
,
274 ACE_TEXT ("Annotation Test Error: %C:\n")
275 ACE_TEXT ("%C annotation %d is null!\n"),
276 name_
.c_str (), node_name
, index
));
280 if (anno_appl
->annotation_decl () != anno_decl
)
282 char *anno_appl_name
= anno_appl
->name ()->get_string_copy ();
283 char *anno_decl_name
= anno_decl
->name ()->get_string_copy ();
284 char *node_name
= node
->name ()->get_string_copy ();
285 ACE_ERROR ((LM_ERROR
,
286 ACE_TEXT ("Annotation Test Error: %C:\n")
287 ACE_TEXT ("%C annotation %d is a %C, looking for a %C!\n"),
288 name_
.c_str (), node_name
, index
, anno_appl_name
, anno_decl_name
));
289 delete [] anno_appl_name
;
290 delete [] anno_decl_name
;
299 Annotation_Test::assert_annotation_member_count (
300 AST_Annotation_Decl
*anno_decl
, size_t count
)
304 ACE_ERROR ((LM_ERROR
,
305 ACE_TEXT ("Annotation Test Error: %C:\n")
306 ACE_TEXT ("assert_annotation_member_count: annotation decl is null!\n"),
311 size_t actual_count
= anno_decl
->member_count ();
312 if (actual_count
!= count
)
314 char *anno_decl_name
= anno_decl
->name ()->get_string_copy ();
315 ACE_ERROR ((LM_ERROR
,
316 ACE_TEXT ("Annotation Test Error: %C:\n")
317 ACE_TEXT ("%C should have %d members, but it actually has %d!\n"),
318 name_
.c_str (), anno_decl_name
, count
, actual_count
));
319 delete [] anno_decl_name
;
325 Annotation_Test::assert_annotation_member_count (
326 AST_Annotation_Appl
*anno_appl
, size_t count
)
328 assert_annotation_member_count (
329 dynamic_cast<AST_Annotation_Decl
*> (anno_appl
), count
);
332 AST_Annotation_Member
*
333 Annotation_Test::get_annotation_member (
334 AST_Annotation_Decl
*anno_decl
, const char *name
)
336 AST_Decl
*decl
= (*anno_decl
)[name
];
337 AST_Annotation_Member
*member
= decl
?
338 dynamic_cast<AST_Annotation_Member
*> (decl
) : 0;
341 ACE_ERROR ((LM_ERROR
,
342 ACE_TEXT ("Annotation Test Error: %C:\n")
343 ACE_TEXT ("Could not get annotation member %C!\n"),
344 name_
.c_str (), name
));
350 AST_Annotation_Member
*
351 Annotation_Test::get_annotation_member (
352 AST_Annotation_Appl
*anno_appl
, const char *name
)
354 return get_annotation_member (
355 dynamic_cast<AST_Annotation_Decl
*> (anno_appl
), name
);
359 Annotation_Test::assert_annotation_member_type (
360 AST_Annotation_Member
*member
, AST_Expression::ExprType type
)
362 if (member
->expr_type () != type
)
364 char *member_name
= member
->name ()->get_string_copy ();
365 ACE_ERROR ((LM_ERROR
,
366 ACE_TEXT ("Annotation Test Error: %C:\n")
367 ACE_TEXT ("For Annotation Member %C, ")
368 ACE_TEXT ("expecting it to be a %C, but it is a %C!\n"),
369 name_
.c_str (), member_name
,
370 AST_Expression::exprtype_to_string (type
),
371 AST_Expression::exprtype_to_string (member
->expr_type ())));
372 delete [] member_name
;
378 Annotation_Test::assert_annotation_member_value (
379 AST_Annotation_Member
*member
, AST_Expression
*expected
)
381 AST_Expression
*member_value
= member
->value ();
384 char *member_name
= member
->name ()->get_string_copy ();
385 ACE_ERROR ((LM_ERROR
,
386 ACE_TEXT ("Annotation Test Error: %C:\n")
387 ACE_TEXT ("For Annotation Member %C, ")
388 ACE_TEXT ("expecting it to have a value, but it doesn't!\n"),
389 name_
.c_str (), member_name
));
390 delete [] member_name
;
396 char *member_name
= member
->name ()->get_string_copy ();
397 ACE_ERROR ((LM_ERROR
,
398 ACE_TEXT ("Annotation Test Error: %C:\n")
399 ACE_TEXT ("For Annotation Member %C, ")
400 ACE_TEXT ("expected value is null, can't compare!\n"),
401 name_
.c_str (), member_name
));
402 delete [] member_name
;
407 if (member_value
->ev ()->et
== AST_Expression::EV_ulong
&&
408 expected
->ev ()->et
== AST_Expression::EV_ulong
)
411 equal
= expected
->ev ()->u
.ulval
== member_value
->ev ()->u
.ulval
;
415 equal
= (*expected
) == member_value
;
420 char *member_name
= member
->name ()->get_string_copy ();
421 ACE_ERROR ((LM_ERROR
,
422 ACE_TEXT ("Annotation Test Error: %C:\n")
423 ACE_TEXT ("For Annotation Member %C, expecting "),
424 name_
.c_str (), member_name
));
425 delete [] member_name
;
426 expected
->dump (*ACE_DEFAULT_LOG_STREAM
);
427 ACE_ERROR ((LM_ERROR
, ACE_TEXT (", got ")));
428 member_value
->dump (*ACE_DEFAULT_LOG_STREAM
);
429 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("!\n")));
435 Annotation_Test::assert_annotation_member_no_value (AST_Annotation_Member
*member
)
437 AST_Expression
*member_value
= member
->value ();
440 char *member_name
= member
->name ()->get_string_copy ();
441 ACE_ERROR ((LM_ERROR
,
442 ACE_TEXT ("Annotation Test Error: %C:\n")
443 ACE_TEXT ("For Annotation Member %C, ")
444 ACE_TEXT ("expecting it to not have a value, but it does!\n"),
445 name_
.c_str (), member_name
));
446 delete [] member_name
;
452 Annotation_Test::set_scope (AST_Decl
*scope_node
)
454 scope_
= dynamic_cast<UTL_Scope
*> (scope_node
);
457 ACE_ERROR ((LM_ERROR
,
458 ACE_TEXT ("Annotation Test Error: %C:\n")
459 ACE_TEXT ("Node passed to set_scope isn't a valid UTL_Scope!\n"),
466 Annotation_Test::disable_output ()
468 disable_output_
= true;
472 Annotation_Test::results ()
474 if (Annotation_Test::failed_test_count_
)
476 ACE_ERROR ((LM_ERROR
,
477 ACE_TEXT ("Annotation Test: %d out of %d subtests failed!\n"),
478 failed_test_count_
, total_test_count_
));
482 ACE_DEBUG ((LM_DEBUG
,
483 ACE_TEXT ("Annotation Test: All %d subtests passed\n"),
486 idl_global
->set_err_count (failed_test_count_
);
490 Annotation_Test::print_idl_with_line_numbers ()
492 static const char* start_marker
=
497 static const char* end_marker
=
502 const long last_error_line
= idl_global
->err ()->last_error_lineno
;
503 const long marked_line
= last_error_line
!= -1 ?
504 last_error_line
: idl_global
->err ()->last_warning_lineno
;
505 const size_t char_count
= idl_
.length ();
507 long line_number
= 0;
508 for (size_t start
= 0; start
< char_count
;)
511 const size_t end
= idl_
.find ('\n', start
);
512 const std::string line
= idl_
.substr (start
, end
- start
);
513 const bool mark_line
= line_number
== marked_line
;
514 ACE_DEBUG ((LM_DEBUG
,
515 ACE_TEXT ("%C%4u: %C%C\n"),
516 mark_line
? start_marker
: " ",
517 line_number
, line
.c_str (),
518 mark_line
? end_marker
: ""));
519 if (end
== std::string::npos
) {