1 //: Mu scenarios. This will get long, but these are the tests we want to
  2 //: support in this layer.
  3 
  4 //: We avoid raw numeric locations in Mu -- except in scenarios, where they're
  5 //: handy to check the values of specific variables
  6 :(scenarios run_mu_scenario)
  7 :(scenario scenario_block)
  8 scenario foo [
  9   run [
 10     1:num <- copy 13
 11   ]
 12   memory-should-contain [
 13     1 <- 13
 14   ]
 15 ]
 16 # checks are inside scenario
 17 
 18 :(scenario scenario_multiple_blocks)
 19 scenario foo [
 20   run [
 21     1:num <- copy 13
 22   ]
 23   memory-should-contain [
 24     1 <- 13
 25   ]
 26   run [
 27     2:num <- copy 13
 28   ]
 29   memory-should-contain [
 30     1 <- 13
 31     2 <- 13
 32   ]
 33 ]
 34 # checks are inside scenario
 35 
 36 :(scenario scenario_check_memory_and_trace)
 37 scenario foo [
 38   run [
 39     1:num <- copy 13
 40     trace 1, [a], [a b c]
 41   ]
 42   memory-should-contain [
 43     1 <- 13
 44   ]
 45   trace-should-contain [
 46     a: a b c
 47   ]
 48   trace-should-not-contain [
 49     a: x y z
 50   ]
 51 ]
 52 # checks are inside scenario
 53 
 54 //:: Core data structure
 55 
 56 :(before "End Types")
 57 struct scenario {
 58   string name;
 59   string to_run;
 60   void clear() {
 61     name.clear();
 62     to_run.clear();
 63   }
 64 };
 65 
 66 :(before "End Globals")
 67 vector<scenario> Scenarios, Scenarios_snapshot;
 68 set<string> Scenario_names, Scenario_names_snapshot;
 69 :(before "End save_snapshots")
 70 Scenarios_snapshot = Scenarios;
 71 Scenario_names_snapshot = Scenario_names;
 72 :(before "End restore_snapshots")
 73 Scenarios = Scenarios_snapshot;
 74 Scenario_names = Scenario_names_snapshot;
 75 
 76 //:: Parse the 'scenario' form.
 77 //: Simply store the text of the scenario.
 78 
 79 :(before "End Command Handlers")
 80 else if (command == "scenario") {
 81   scenario result = parse_scenario(in);
 82   if (!result.name.empty())
 83     Scenarios.push_back(result);
 84 }
 85 else if (command == "pending-scenario") {
 86   // for temporary use only
 87   parse_scenario(in);  // discard
 88 }
 89 
 90 :(code)
 91 scenario parse_scenario(istream& in) {
 92   scenario result;
 93   result.name = next_word(in);
 94   if (contains_key(Scenario_names, result.name))
 95     raise << "duplicate scenario name: '" << result.name << "'\n" << end();
 96   Scenario_names.insert(result.name);
 97   if (result.name.empty()) {
 98     assert(!has_data(in));
 99     raise << "incomplete scenario at end of file\n" << end();
100     return result;
101   }
102   skip_whitespace_and_comments(in);
103   if (in.peek() != '[') {
104     raise << "Expected '[' after scenario '" << result.name << "'\n" << end();
105     exit(0);
106   }
107   // scenarios are take special 'code' strings so we need to ignore brackets
108   // inside comments
109   result.to_run = slurp_quoted(in);
110   // delete [] delimiters
111   if (!starts_with(result.to_run, "[")) {
112     raise << "scenario " << result.name << " should start with '['\n" << end();
113     result.clear();
114     return result;
115   }
116   result.to_run.erase(0, 1);
117   if (result.to_run.at(SIZE(result.to_run)-1) != ']') {
118     raise << "scenario " << result.name << " has an unbalanced '['\n" << end();
119     result.clear();
120     return result;
121   }
122   result.to_run.erase(SIZE(result.to_run)-1);
123   return result;
124 }
125 
126 :(scenario read_scenario_with_bracket_in_comment)
127 scenario foo [
128   # ']' in comment
129   1:num <- copy 0
130 ]
131 +run: {1: "number"} <- copy {0: "literal"}
132 
133 :(scenario read_scenario_with_bracket_in_comment_in_nested_string)
134 scenario foo [
135   1:text <- new [# not a comment]
136 ]
137 +run: {1: ("address" "array" "character")} <- new {"# not a comment": "literal-string"}
138 
139 :(scenarios run)
140 :(scenario duplicate_scenarios)
141 % Hide_errors = true;
142 scenario foo [
143   1:num <- copy 0
144 ]
145 scenario foo [
146   2:num <- copy 0
147 ]
148 +error: duplicate scenario name: 'foo'
149 
150 //:: Run scenarios when we run './mu test'.
151 //: Treat the text of the scenario as a regular series of instructions.
152 
153 :(before "End Globals")
154 int Num_core_mu_scenarios = 0;
155 :(after "Check For .mu Files")
156 Num_core_mu_scenarios = SIZE(Scenarios);
157 :(before "End Tests")
158 Hide_missing_default_space_errors = false;
159 if (Num_core_mu_scenarios > 0) {
160   time(&t);
161   cerr << "Mu tests: " << ctime(&t);
162   for (int i = 0;  i < Num_core_mu_scenarios;  ++i) {
163 //?     cerr << '\n' << i << ": " << Scenarios.at(i).name;
164     run_mu_scenario(Scenarios.at(i));
165     if (Passed) cerr << ".";
166     else ++num_failures;
167   }
168   cerr << "\n";
169 }
170 run_app_scenarios:
171 if (Num_core_mu_scenarios != SIZE(Scenarios)) {
172   time(&t);
173   cerr << "App tests: " << ctime(&t);
174   for (int i = Num_core_mu_scenarios;  i < SIZE(Scenarios);  ++i) {
175 //?     cerr << '\n' << i << ": " << Scenarios.at(i).name;
176     run_mu_scenario(Scenarios.at(i));
177     if (Passed) cerr << ".";
178     else ++num_failures;
179   }
180   cerr << "\n";
181 }
182 
183 //: For faster debugging, support running tests for just the Mu app(s) we are
184 //: loading.
185 :(before "End Globals")
186 bool Test_only_app = false;
187 :(before "End Commandline Options(*arg)")
188 else if (is_equal(*arg, "--test-only-app")) {
189   Test_only_app = true;
190 }
191 :(after "End Test Run Initialization")
192 if (Test_only_app && Num_core_mu_scenarios < SIZE(Scenarios)) {
193   goto run_app_scenarios;
194 }
195 
196 //: Convenience: run a single named scenario.
197 :(after "Test Runs")
198 for (int i = 0;  i < SIZE(Scenarios);  ++i) {
199   if (Scenarios.at(i).name == argv[argc-1]) {
200     if (Start_tracing) {
201       Trace_stream = new trace_stream;
202       Save_trace = true;
203     }
204     run_mu_scenario(Scenarios.at(i));
205     if (Passed) cerr << ".\n";
206     return 0;
207   }
208 }
209 
210 :(before "End Globals")
211 // this isn't a constant, just a global of type const*
212 const scenario* Current_scenario = NULL;
213 :(code)
214 void run_mu_scenario(const scenario& s) {
215   Current_scenario = &s;
216   bool not_already_inside_test = !Trace_stream;
217 //?   cerr << s.name << '\n';
218   if (not_already_inside_test) {
219     Trace_stream = new trace_stream;
220     reset();
221   }
222   vector<recipe_ordinal> tmp = load("recipe scenario_"+s.name+" [ "+s.to_run+" ]");
223   mark_autogenerated(tmp.at(0));
224   bind_special_scenario_names(tmp.at(0));
225   transform_all();
226   if (!trace_contains_errors())
227     run(tmp.front());
228   // End Mu Test Teardown
229   if (!Hide_errors && trace_contains_errors() && !Scenario_testing_scenario)
230     Passed = false;
231   if (not_already_inside_test && Trace_stream) {
232     if (Save_trace) Trace_stream->dump();
233     delete Trace_stream;
234     Trace_stream = NULL;
235   }
236   Current_scenario = NULL;
237 }
238 
239 //: Permit numeric locations to be accessed in scenarios.
240 :(before "End check_default_space Special-cases")
241 // user code should never create recipes with underscores in their names
242 if (starts_with(caller.name, "scenario_")) return;  // skip Mu scenarios which will use raw memory locations
243 if (starts_with(caller.name, "run_")) return;  // skip calls to 'run', which should be in scenarios and will also use raw memory locations
244 
245 :(before "End maybe(recipe_name) Special-cases")
246 if (starts_with(recipe_name, "scenario_"))
247   return recipe_name.substr(strlen("scenario_")) + ": ";
248 
249 //: Some variables for fake resources always get special /raw addresses in scenarios.
250 
251 :(code)
252 // Should contain everything passed by is_special_name but failed by is_disqualified.
253 void bind_special_scenario_names(const recipe_ordinal r) {
254   // Special Scenario Variable Names(r)
255   // End Special Scenario Variable Names(r)
256 }
257 :(before "Done Placing Ingredient(ingredient, inst, caller)")
258 maybe_make_raw(ingredient, caller);
259 :(before "Done Placing Product(product, inst, caller)")
260 maybe_make_raw(product, caller);
261 :(code)
262 void maybe_make_raw(reagent& r, const recipe& caller) {
263   if (!is_special_name(r.name)) return;
264   if (starts_with(caller.name, "scenario_"))
265     r.properties.push_back(pair<string, string_tree*>("raw", NULL));
266   // End maybe_make_raw
267 }
268 
269 //: Test.
270 :(before "End is_special_name Special-cases")
271 if (s == "__maybe_make_raw_test__") return true;
272 :(before "End Special Scenario Variable Names(r)")
273 //: ugly: we only need this for this one test, but need to define it for all time
274 Name[r]["__maybe_make_raw_test__"] = Reserved_for_tests-1;
275 :(code)
276 void test_maybe_make_raw() {
277   // check that scenarios can use local-scope and special variables together
278   vector<recipe_ordinal> tmp = load(
279       "def scenario_foo [\n"
280       "  local-scope\n"
281       "  __maybe_make_raw_test__:num <- copy 34\n"
282       "]\n");
283   mark_autogenerated(tmp.at(0));
284   bind_special_scenario_names(tmp.at(0));
285   transform_all();
286   run(tmp.at(0));
287   CHECK_TRACE_DOESNT_CONTAIN_ERRORS();
288 }
289 
290 //: Watch out for redefinitions of scenario routines. We should never ever be
291 //: doing that, regardless of anything else.
292 :(scenario forbid_redefining_scenario_even_if_forced)
293 % Hide_errors = true;
294 % Disable_redefine_checks = true;
295 def scenario-foo [
296   1:num <- copy 34
297 ]
298 def scenario-foo [
299   1:num <- copy 35
300 ]
301 +error: redefining recipe scenario-foo
302 
303 :(scenario scenario_containing_parse_error)
304 % Hide_errors = true;
305 scenario foo [
306   memory-should-contain [
307     1 <- 0
308   # missing ']'
309 ]
310 # no crash
311 
312 :(scenario scenario_containing_transform_error)
313 % Hide_errors = true;
314 def main [
315   local-scope
316   add x, 1
317 ]
318 # no crash
319 
320 :(after "bool should_check_for_redefine(const string& recipe_name)")
321   if (recipe_name.find("scenario-") == 0) return true;
322 
323 //:: The special instructions we want to support inside scenarios.
324 //: These are easy to support in an interpreter, but will require more work
325 //: when we eventually build a compiler.
326 
327 //: 'run' is a purely lexical convenience to separate the code actually being
328 //: tested from any setup
329 
330 :(scenario run)
331 def main [
332   run [
333     1:num <- copy 13
334   ]
335 ]
336 +mem: storing 13 in location 1
337 
338 :(before "End Rewrite Instruction(curr, recipe result)")
339 if (curr.name == "run") {
340   // Just inline all instructions inside the run block in the containing
341   // recipe. 'run' is basically a comment; pretend it doesn't exist.
342   istringstream in2("[\n"+curr.ingredients.at(0).name+"\n]\n");
343   slurp_body(in2, result);
344   curr.clear();
345 }
346 
347 :(scenario run_multiple)
348 def main [
349   run [
350     1:num <- copy 13
351   ]
352   run [
353     2:num <- copy 13
354   ]
355 ]
356 +mem: storing 13 in location 1
357 +mem: storing 13 in location 2
358 
359 //: 'memory-should-contain' raises errors if specific locations aren't as expected
360 //: Also includes some special support for checking Mu texts.
361 
362 :(before "End Globals")
363 bool Scenario_testing_scenario = false;
364 :(before "End Reset")
365 Scenario_testing_scenario = false;
366 
367 :(scenario memory_check)
368 % Scenario_testing_scenario = true;
369 % Hide_errors = true;
370 def main [
371   memory-should-contain [
372     1 <- 13
373   ]
374 ]
375 +run: checking location 1
376 +error: F - main: expected location '1' to contain 13 but saw 0
377 
378 :(before "End Primitive Recipe Declarations")
379 MEMORY_SHOULD_CONTAIN,
380 :(before "End Primitive Recipe Numbers")
381 put(Recipe_ordinal, "memory-should-contain", MEMORY_SHOULD_CONTAIN);
382 :(before "End Primitive Recipe Checks")
383 case MEMORY_SHOULD_CONTAIN: {
384   break;
385 }
386 :(before "End Primitive Recipe Implementations")
387 case MEMORY_SHOULD_CONTAIN: {
388   if (!Passed) break;
389   check_memory(current_instruction().ingredients.at(0).name);
390   break;
391 }
392 
393 :(code)
394 void check_memory(const string& s) {
395   istringstream in(s);
396   in >> std::noskipws;
397   set<int> locations_checked;
398   while (true) {
399     skip_whitespace_and_comments(in);
400     if (!has_data(in)) break;
401     string lhs = next_word(in);
402     if (lhs.empty()) {
403       assert(!has_data(in));
404       raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (0)\n" << end();
405       return;
406     }
407     if (!is_integer(lhs)) {
408       check_type(lhs, in);
409       continue;
410     }
411     int address = to_integer(lhs);
412     skip_whitespace_and_comments(in);
413     string _assign;  in >> _assign;  assert(_assign == "<-");
414     skip_whitespace_and_comments(in);
415     string rhs = next_word(in);
416     if (rhs.empty()) {
417       assert(!has_data(in));
418       raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (1)\n" << end();
419       return;
420     }
421     if (!is_integer(rhs) && !is_noninteger(rhs)) {
422       if (!Hide_errors) cerr << '\n';
423       raise << "F - " << maybe(current_recipe_name()) << "location '" << address << "' can't contain non-number " << rhs << '\n' << end();
424       if (!Scenario_testing_scenario) Passed = false;
425       return;
426     }
427     double value = to_double(rhs);
428     if (contains_key(locations_checked, address))
429       raise << maybe(current_recipe_name()) << "duplicate expectation for location '" << address << "'\n" << end();
430     trace("run") << "checking location " << address << end();
431     if (get_or_insert(Memory, address) != value) {
432       if (!Hide_errors) cerr << '\n';
433       raise << "F - " << maybe(current_recipe_name()) << "expected location '" << address << "' to contain " << no_scientific(value) << " but saw " << no_scientific(get_or_insert(Memory, address)) << '\n' << end();
434       if (!Scenario_testing_scenario) Passed = false;
435       return;
436     }
437     locations_checked.insert(address);
438   }
439 }
440 
441 void check_type(const string& lhs, istream& in) {
442   reagent x(lhs);
443   if (is_mu_array(x.type) && is_mu_character(array_element(x.type))) {
444     x.set_value(to_integer(x.name));
445     skip_whitespace_and_comments(in);
446     string _assign = next_word(in);
447     if (_assign.empty()) {
448       assert(!has_data(in));
449       raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (2)\n" << end();
450       return;
451     }
452     assert(_assign == "<-");
453     skip_whitespace_and_comments(in);
454     string literal = next_word(in);
455     if (literal.empty()) {
456       assert(!has_data(in));
457       raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (3)\n" << end();
458       return;
459     }
460     int address = x.value;
461     // exclude quoting brackets
462     if (*literal.begin() != '[') {
463       raise << maybe(current_recipe_name()) << "array:character types inside 'memory-should-contain' can only be compared with text literals surrounded by [], not '" << literal << "'\n" << end();
464       return;
465     }
466     literal.erase(literal.begin());
467     assert(*--literal.end() == ']');  literal.erase(--literal.end());
468     check_mu_text(address, literal);
469     return;
470   }
471   // End Scenario Type Special-cases
472   raise << "don't know how to check memory for '" << lhs << "'\n" << end();
473 }
474 
475 void check_mu_text(int start, const string& literal) {
476   trace("run") << "checking text length at " << start << end();
477   int array_length = static_cast<int>(get_or_insert(Memory, start));
478   if (array_length != SIZE(literal)) {
479     if (!Hide_errors) cerr << '\n';
480     raise << "F - " << maybe(current_recipe_name()) << "expected location '" << start << "' to contain length " << SIZE(literal) << " of text [" << literal << "] but saw " << array_length << " (for text [" << read_mu_characters(start+/*skip length*/1, array_length) << "])\n" << end();
481     if (!Scenario_testing_scenario) Passed = false;
482     return;
483   }
484   int curr = start+1;  // now skip length
485   for (int i = 0;  i < SIZE(literal);  ++i) {
486     trace("run") << "checking location " << curr+i << end();
487     if (get_or_insert(Memory, curr+i) != literal.at(i)) {
488       if (!Hide_errors) cerr << '\n';
489       raise << "F - " << maybe(current_recipe_name()) << "expected location " << (curr+i) << " to contain " << literal.at(i) << " but saw " << no_scientific(get_or_insert(Memory, curr+i)) << '\n' << end();
490       if (!Scenario_testing_scenario) Passed = false;
491       return;
492     }
493   }
494 }
495 
496 :(scenario memory_check_multiple)
497 % Scenario_testing_scenario = true;
498 % Hide_errors = true;
499 def main [
500   memory-should-contain [
501     1 <- 0
502     1 <- 0
503   ]
504 ]
505 +error: main: duplicate expectation for location '1'
506 
507 :(scenario memory_check_mu_text_length)
508 % Scenario_testing_scenario = true;
509 % Hide_errors = true;
510 def main [
511   1:num <- copy 3
512   2:num <- copy 97  # 'a'
513   3:num <- copy 98  # 'b'
514   4:num <- copy 99  # 'c'
515   memory-should-contain [
516     1:array:character <- [ab]
517   ]
518 ]
519 +error: F - main: expected location '1' to contain length 2 of text [ab] but saw 3 (for text [abc])
520 
521 :(scenario memory_check_mu_text)
522 def main [
523   1:num <- copy 3
524   2:num <- copy 97  # 'a'
525   3:num <- copy 98  # 'b'
526   4:num <- copy 99  # 'c'
527   memory-should-contain [
528     1:array:character <- [abc]
529   ]
530 ]
531 +run: checking text length at 1
532 +run: checking location 2
533 +run: checking location 3
534 +run: checking location 4
535 
536 :(scenario memory_invalid_string_check)
537 % Scenario_testing_scenario = true;
538 % Hide_errors = true;
539 def main [
540   memory-should-contain [
541     1 <- [abc]
542   ]
543 ]
544 +error: F - main: location '1' can't contain non-number [abc]
545 
546 :(scenario memory_invalid_string_check2)
547 % Hide_errors = true;
548 def main [
549   1:num <- copy 3
550   2:num <- copy 97  # 'a'
551   3:num <- copy 98  # 'b'
552   4:num <- copy 99  # 'c'
553   memory-should-contain [
554     1:array:character <- 0
555   ]
556 ]
557 +error: main: array:character types inside 'memory-should-contain' can only be compared with text literals surrounded by [], not '0'
558 
559 :(scenario memory_check_with_comment)
560 % Scenario_testing_scenario = true;
561 % Hide_errors = true;
562 def main [
563   memory-should-contain [
564     1 <- 34  # comment
565   ]
566 ]
567 -error: location 1 can't contain non-number 34  # comment
568 # but there'll be an error signalled by memory-should-contain
569 
570 //: 'trace-should-contain' is like the '+' lines in our scenarios so far
571 // Like runs of contiguous '+' lines, order is important. The trace checks
572 // that the lines are present *and* in the specified sequence. (There can be
573 // other lines in between.)
574 
575 :(scenario trace_check_fails)
576 % Scenario_testing_scenario = true;
577 % Hide_errors = true;
578 def main [
579   trace-should-contain [
580     a: b
581     a: d
582   ]
583 ]
584 +error: F - main: missing [b] in trace with label 'a'
585 
586 :(before "End Primitive Recipe Declarations")
587 TRACE_SHOULD_CONTAIN,
588 :(before "End Primitive Recipe Numbers")
589 put(Recipe_ordinal, "trace-should-contain", TRACE_SHOULD_CONTAIN);
590 :(before "End Primitive Recipe Checks")
591 case TRACE_SHOULD_CONTAIN: {
592   break;
593 }
594 :(before "End Primitive Recipe Implementations")
595 case TRACE_SHOULD_CONTAIN: {
596   if (!Passed) break;
597   check_trace(current_instruction().ingredients.at(0).name);
598   break;
599 }
600 
601 :(code)
602 // simplified version of check_trace_contents() that emits errors rather
603 // than just printing to stderr
604 void check_trace(const string& expected) {
605   Trace_stream->newline();
606   vector<trace_line> expected_lines = parse_trace(expected);
607   if (expected_lines.empty()) return;
608   int curr_expected_line = 0;
609   for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin();  p != Trace_stream->past_lines.end();  ++p) {
610     if (expected_lines.at(curr_expected_line).label != p->label) continue;
611     if (expected_lines.at(curr_expected_line).contents != trim(p->contents)) continue;
612     // match
613     ++curr_expected_line;
614     if (curr_expected_line == SIZE(expected_lines)) return;
615   }
616   if (!Hide_errors) cerr << '\n';
617   raise << "F - " << maybe(current_recipe_name()) << "missing [" << expected_lines.at(curr_expected_line).contents << "] "
618         << "in trace with label '" << expected_lines.at(curr_expected_line).label << "'\n" << end();
619   if (!Hide_errors)
620     DUMP(expected_lines.at(curr_expected_line).label);
621   if (!Scenario_testing_scenario) Passed = false;
622 }
623 
624 vector<trace_line> parse_trace(const string& expected) {
625   vector<string> buf = split(expected, "\n");
626   vector<trace_line> result;
627   for (int i = 0;  i < SIZE(buf);  ++i) {
628     buf.at(i) = trim(buf.at(i));
629     if (buf.at(i).empty()) continue;
630     int delim = buf.at(i).find(": ");
631     if (delim == -1) {
632       raise << maybe(current_recipe_name()) << "lines in 'trace-should-contain' should be of the form <label>: <contents>. Both parts are required.\n" << end();
633       result.clear();
634       return result;
635     }
636     result.push_back(trace_line(trim(buf.at(i).substr(0, delim)),  trim(buf.at(i).substr(delim+2))));
637   }
638   return result;
639 }
640 
641 :(scenario trace_check_fails_in_nonfirst_line)
642 % Scenario_testing_scenario = true;
643 % Hide_errors = true;
644 def main [
645   run [
646     trace 1, [a], [b]
647   ]
648   trace-should-contain [
649     a: b
650     a: d
651   ]
652 ]
653 +error: F - main: missing [d] in trace with label 'a'
654 
655 :(scenario trace_check_passes_silently)
656 % Scenario_testing_scenario = true;
657 def main [
658   run [
659     trace 1, [a], [b]
660   ]
661   trace-should-contain [
662     a: b
663   ]
664 ]
665 -error: missing [b] in trace with label 'a'
666 $error: 0
667 
668 //: 'trace-should-not-contain' is like the '-' lines in our scenarios so far
669 //: Each trace line is separately checked for absense. Order is *not*
670 //: important, so you can't say things like "B should not exist after A."
671 
672 :(scenario trace_negative_check_fails)
673 % Scenario_testing_scenario = true;
674 % Hide_errors = true;
675 def main [
676   run [
677     trace 1, [a], [b]
678   ]
679   trace-should-not-contain [
680     a: b
681   ]
682 ]
683 +error: F - main: unexpected [b] in trace with label 'a'
684 
685 :(before "End Primitive Recipe Declarations")
686 TRACE_SHOULD_NOT_CONTAIN,
687 :(before "End Primitive Recipe Numbers")
688 put(Recipe_ordinal, "trace-should-not-contain", TRACE_SHOULD_NOT_CONTAIN);
689 :(before "End Primitive Recipe Checks")
690 case TRACE_SHOULD_NOT_CONTAIN: {
691   break;
692 }
693 :(before "End Primitive Recipe Implementations")
694 case TRACE_SHOULD_NOT_CONTAIN: {
695   if (!Passed) break;
696   check_trace_missing(current_instruction().ingredients.at(0).name);
697   break;
698 }
699 
700 :(code)
701 // simplified version of check_trace_contents() that emits errors rather
702 // than just printing to stderr
703 bool check_trace_missing(const string& in) {
704   Trace_stream->newline();
705   vector<trace_line> lines = parse_trace(in);
706   for (int i = 0;  i < SIZE(lines);  ++i) {
707     if (trace_count(lines.at(i).label, lines.at(i).contents) != 0) {
708       raise << "F - " << maybe(current_recipe_name()) << "unexpected [" << lines.at(i).contents << "] in trace with label '" << lines.at(i).label << "'\n" << end();
709       if (!Scenario_testing_scenario) Passed = false;
710       return false;
711     }
712   }
713   return true;
714 }
715 
716 :(scenario trace_negative_check_passes_silently)
717 % Scenario_testing_scenario = true;
718 def main [
719   trace-should-not-contain [
720     a: b
721   ]
722 ]
723 -error: unexpected [b] in trace with label 'a'
724 $error: 0
725 
726 :(scenario trace_negative_check_fails_on_any_unexpected_line)
727 % Scenario_testing_scenario = true;
728 % Hide_errors = true;
729 def main [
730   run [
731     trace 1, [a], [d]
732   ]
733   trace-should-not-contain [
734     a: b
735     a: d
736   ]
737 ]
738 +error: F - main: unexpected [d] in trace with label 'a'
739 
740 :(scenario trace_count_check)
741 def main [
742   run [
743     trace 1, [a], [foo]
744   ]
745   check-trace-count-for-label 1, [a]
746 ]
747 # checks are inside scenario
748 
749 :(before "End Primitive Recipe Declarations")
750 CHECK_TRACE_COUNT_FOR_LABEL,
751 :(before "End Primitive Recipe Numbers")
752 put(Recipe_ordinal, "check-trace-count-for-label", CHECK_TRACE_COUNT_FOR_LABEL);
753 :(before "End Primitive Recipe Checks")
754 case CHECK_TRACE_COUNT_FOR_LABEL: {
755   if (SIZE(inst.ingredients) != 2) {
756     raise << maybe(get(Recipe, r).name) << "'check-trace-count-for-label' requires exactly two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
757     break;
758   }
759   if (!is_mu_number(inst.ingredients.at(0))) {
760     raise << maybe(get(Recipe, r).name) << "first ingredient of 'check-trace-count-for-label' should be a number (count), but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
761     break;
762   }
763   if (!is_literal_text(inst.ingredients.at(1))) {
764     raise << maybe(get(Recipe, r).name) << "second ingredient of 'check-trace-count-for-label' should be a literal string (label), but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
765     break;
766   }
767   break;
768 }
769 :(before "End Primitive Recipe Implementations")
770 case CHECK_TRACE_COUNT_FOR_LABEL: {
771   if (!Passed) break;
772   int expected_count = ingredients.at(0).at(0);
773   string label = current_instruction().ingredients.at(1).name;
774   int count = trace_count(label);
775   if (count != expected_count) {
776     if (!Hide_errors) cerr << '\n';
777     raise << "F - " << maybe(current_recipe_name()) << "expected " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
778     if (!Hide_errors) DUMP(label);
779     if (!Scenario_testing_scenario) Passed = false;
780   }
781   break;
782 }
783 
784 :(before "End Primitive Recipe Declarations")
785 CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN,
786 :(before "End Primitive Recipe Numbers")
787 put(Recipe_ordinal, "check-trace-count-for-label-greater-than", CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN);
788 :(before "End Primitive Recipe Checks")
789 case CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN: {
790   if (SIZE(inst.ingredients) != 2) {
791     raise << maybe(get(Recipe, r).name) << "'check-trace-count-for-label' requires exactly two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
792     break;
793   }
794   if (!is_mu_number(inst.ingredients.at(0))) {
795     raise << maybe(get(Recipe, r).name) << "first ingredient of 'check-trace-count-for-label' should be a number (count), but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
796     break;
797   }
798   if (!is_literal_text(inst.ingredients.at(1))) {
799     raise << maybe(get(Recipe, r).name) << "second ingredient of 'check-trace-count-for-label' should be a literal string (label), but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
800     break;
801   }
802   break;
803 }
804 :(before "End Primitive Recipe Implementations")
805 case CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN: {
806   if (!Passed) break;
807   int expected_count = ingredients.at(0).at(0);
808   string label = current_instruction().ingredients.at(1).name;
809   int count = trace_count(label);
810   if (count <= expected_count) {
811     if (!Hide_errors) cerr << '\n';
812     raise << maybe(current_recipe_name()) << "expected more than " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
813     if (!Hide_errors) {
814       cerr << "trace contents:\n";
815       DUMP(label);
816     }
817     if (!Scenario_testing_scenario) Passed = false;
818   }
819   break;
820 }
821 
822 :(before "End Primitive Recipe Declarations")
823 CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN,
824 :(before "End Primitive Recipe Numbers")
825 put(Recipe_ordinal, "check-trace-count-for-label-lesser-than", CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN);
826 :(before "End Primitive Recipe Checks")
827 case CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN: {
828   if (SIZE(inst.ingredients) != 2) {
829     raise << maybe(get(Recipe, r).name) << "'check-trace-count-for-label' requires exactly two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
830     break;
831   }
832   if (!is_mu_number(inst.ingredients.at(0))) {
833     raise << maybe(get(Recipe, r).name) << "first ingredient of 'check-trace-count-for-label' should be a number (count), but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
834     break;
835   }
836   if (!is_literal_text(inst.ingredients.at(1))) {
837     raise << maybe(get(Recipe, r).name) << "second ingredient of 'check-trace-count-for-label' should be a literal string (label), but got '" << inst.ingredients.at(1).original_string << "'\n" << end();
838     break;
839   }
840   break;
841 }
842 :(before "End Primitive Recipe Implementations")
843 case CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN: {
844   if (!Passed) break;
845   int expected_count = ingredients.at(0).at(0);
846   string label = current_instruction().ingredients.at(1).name;
847   int count = trace_count(label);
848   if (count >= expected_count) {
849     if (!Hide_errors) cerr << '\n';
850     raise << "F - " << maybe(current_recipe_name()) << "expected less than " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
851     if (!Hide_errors) {
852       cerr << "trace contents:\n";
853       DUMP(label);
854     }
855     if (!Scenario_testing_scenario) Passed = false;
856   }
857   break;
858 }
859 
860 :(scenario trace_count_check_2)
861 % Scenario_testing_scenario = true;
862 % Hide_errors = true;
863 def main [
864   run [
865     trace 1, [a], [foo]
866   ]
867   check-trace-count-for-label 2, [a]
868 ]
869 +error: F - main: expected 2 lines in trace with label 'a' in trace
870 
871 //: Minor detail: ignore 'system' calls in scenarios, since anything we do
872 //: with them is by definition impossible to test through Mu.
873 :(after "case _SYSTEM:")
874   if (Current_scenario) break;
875 
876 //:: Warn if people use '_' manually in recipe names. They're reserved for internal use.
877 
878 :(scenario recipe_name_with_underscore)
879 % Hide_errors = true;
880 def foo_bar [
881 ]
882 +error: foo_bar: don't create recipes with '_' in the name
883 
884 :(before "End recipe Fields")
885 bool is_autogenerated;
886 :(before "End recipe Constructor")
887 is_autogenerated = false;
888 :(code)
889 void mark_autogenerated(recipe_ordinal r) {
890   get(Recipe, r).is_autogenerated = true;
891 }
892 
893 :(after "void transform_all()")
894   for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
895     const recipe& r = p->second;
896     if (r.name.find('_') == string::npos) continue;
897     if (r.is_autogenerated) continue;  // created by previous call to transform_all()
898     raise << r.name << ": don't create recipes with '_' in the name\n" << end();
899   }
900 
901 //:: Helpers
902 
903 :(code)
904 // just for the scenarios running scenarios in C++ layers
905 void run_mu_scenario(const string& form) {
906   Scenario_names.clear();
907   istringstream in(form);
908   in >> std::noskipws;
909   skip_whitespace_and_comments(in);
910   string _scenario = next_word(in);
911   if (_scenario.empty()) {
912     assert(!has_data(in));
913     raise << "no scenario in string passed into run_mu_scenario()\n" << end();
914     return;
915   }
916   assert(_scenario == "scenario");
917   scenario s = parse_scenario(in);
918   run_mu_scenario(s);
919 }