Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / IDLv4 / annotations / Annotation_Test.cpp
blobe12dc670b930a8480cd302057ec716453f48fa97
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)
9 : name_ (name),
10 failed_ (false),
11 error_count_ (0),
12 last_error_ (UTL_Error::EIDL_OK),
13 last_warning_ (UTL_Error::EIDL_OK),
14 scope_ (0),
15 disable_output_ (false)
17 total_test_count_++;
20 Annotation_Test::~Annotation_Test ()
22 if (idl_global->err ()->last_error == UTL_Error::EIDL_SYNTAX_ERROR)
24 ACE_DEBUG ((LM_DEBUG,
25 ACE_TEXT ("Annotation Test: %C: ")
26 ACE_TEXT ("FAILED because of syntax error in:\n"),
27 name_.c_str ()));
29 print_idl_with_line_numbers ();
31 ACE_DEBUG ((LM_DEBUG,
32 ACE_TEXT ("Check syntax error message above for more information.\n"),
33 ACE_TEXT ("Failures beyond this might be false positives.\n")));
34 ++failed_test_count_;
36 else if (!failed_)
38 ACE_DEBUG ((LM_DEBUG,
39 ACE_TEXT ("Annotation Test: %C: PASSED\n"), name_.c_str ()));
42 idl_global->err ()->reset_last_error_and_warning ();
45 void
46 Annotation_Test::failed (const std::string &message)
48 if (message.length ())
50 ACE_ERROR ((LM_ERROR,
51 ACE_TEXT ("Annotation Test Error: %C: %C\n"),
52 name_.c_str (), message.c_str ()));
54 ACE_DEBUG ((LM_DEBUG,
55 ACE_TEXT ("Annotation Test: %C: FAILED\nFailed IDL:\n"),
56 name_.c_str ()));
57 print_idl_with_line_numbers ();
58 failed_test_count_++;
59 failed_ = true;
60 throw Failed ();
63 Annotation_Test &
64 Annotation_Test::error_count (int error_count)
66 error_count_ = error_count;
67 return *this;
70 Annotation_Test &
71 Annotation_Test::last_error (UTL_Error::ErrorCode last_error)
73 last_error_ = last_error;
74 return *this;
77 Annotation_Test &
78 Annotation_Test::last_warning (UTL_Error::ErrorCode last_warning)
80 last_warning_ = last_warning;
81 return *this;
84 Annotation_Test &
85 Annotation_Test::run (const std::string &idl)
87 // Reset Error State
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;
92 // Eval IDL
93 idl_ = idl;
94 idl_global->eval (idl.c_str (), disable_output_);
96 // Look at Results
97 if (idl_global->err_count () != error_count_)
99 failed_ = true;
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_)
106 failed_ = true;
107 ACE_ERROR ((LM_ERROR,
108 ACE_TEXT ("Annotation Test Error: %C:\n")
109 ACE_TEXT ("Last Error Code (UTL_Error::ErrorCode): expecting "),
110 name_.c_str ()));
111 if (last_error_ == UTL_Error::EIDL_OK)
113 ACE_ERROR ((LM_ERROR, ACE_TEXT ("OK")));
115 else
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")));
124 else
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_)
133 failed_ = true;
134 ACE_ERROR ((LM_ERROR,
135 ACE_TEXT ("Annotation Test Error: %C:\n")
136 ACE_TEXT ("Last Warning Code (UTL_Error::ErrorCode): expecting "),
137 name_.c_str ()));
138 if (last_warning_ == UTL_Error::EIDL_OK)
140 ACE_ERROR ((LM_ERROR, ACE_TEXT ("OK")));
142 else
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")));
151 else
153 ACE_ERROR ((LM_ERROR, ACE_TEXT ("%d"),
154 idl_global->err ()->last_warning));
156 ACE_ERROR ((LM_ERROR, ACE_TEXT ("!\n")));
158 if (failed_)
160 failed ();
163 return *this;
166 AST_Decl *
167 Annotation_Test::assert_node (const char *name, UTL_Scope *from)
169 AST_Decl *node = 0;
170 UTL_ScopedName *scoped_name = FE_Utils::string_to_scoped_name (name);
172 UTL_Scope *scope = from ? from : (scope_ ? scope_ : idl_global->scopes ().bottom ());
174 if (scoped_name)
176 node = scope->lookup_by_name (scoped_name);
179 if (!node)
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));
185 failed ();
188 if (scoped_name)
190 scoped_name->destroy ();
191 delete scoped_name;
194 return node;
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));
208 failed ();
211 return node;
214 void
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 ()));
226 delete [] node_name;
227 failed ();
231 AST_Annotation_Appl *
232 Annotation_Test::assert_annotation_appl (
233 AST_Decl *node, size_t index, AST_Annotation_Decl *anno_decl)
235 if (!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"),
240 name_.c_str ()));
241 failed ();
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));
253 delete [] node_name;
254 failed ();
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 ()));
265 delete [] node_name;
266 failed ();
269 AST_Annotation_Appl *anno_appl = annotations[index];
270 if (!anno_appl)
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));
277 delete [] node_name;
278 failed ();
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;
291 delete [] node_name;
292 failed ();
295 return anno_appl;
298 void
299 Annotation_Test::assert_annotation_member_count (
300 AST_Annotation_Decl *anno_decl, size_t count)
302 if (!anno_decl)
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"),
307 name_.c_str ()));
308 failed ();
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;
320 failed ();
324 void
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;
339 if (!member)
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));
345 failed ();
347 return member;
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);
358 void
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;
373 failed ();
377 void
378 Annotation_Test::assert_annotation_member_value (
379 AST_Annotation_Member *member, AST_Expression *expected)
381 AST_Expression *member_value = member->value ();
382 if (!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;
391 failed ();
394 if (!expected)
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;
403 failed ();
406 bool equal;
407 if (member_value->ev ()->et == AST_Expression::EV_ulong &&
408 expected->ev ()->et == AST_Expression::EV_ulong)
410 // For Enums
411 equal = expected->ev ()->u.ulval == member_value->ev ()->u.ulval;
413 else
415 equal = (*expected) == member_value;
418 if (!equal)
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")));
430 failed ();
434 void
435 Annotation_Test::assert_annotation_member_no_value (AST_Annotation_Member *member)
437 AST_Expression *member_value = member->value ();
438 if (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;
447 failed ();
451 void
452 Annotation_Test::set_scope (AST_Decl *scope_node)
454 scope_ = dynamic_cast<UTL_Scope *> (scope_node);
455 if (!scope_)
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"),
460 name_.c_str ()));
461 failed ();
465 void
466 Annotation_Test::disable_output ()
468 disable_output_ = true;
471 void
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_));
480 else
482 ACE_DEBUG ((LM_DEBUG,
483 ACE_TEXT ("Annotation Test: All %d subtests passed\n"),
484 total_test_count_));
486 idl_global->set_err_count (failed_test_count_);
489 void
490 Annotation_Test::print_idl_with_line_numbers ()
492 static const char* start_marker =
493 #ifndef ACE_WIN32
494 "\x1b[31m"
495 #endif
496 ">";
497 static const char* end_marker =
498 #ifndef ACE_WIN32
499 "\x1b[0m"
500 #endif
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;)
510 ++line_number;
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) {
520 break;
522 start = end + 1;