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     setup();
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     teardown();
233     if (Save_trace) {
234       ofstream fout("last_trace");
235       fout << Trace_stream->readable_contents("");
236       fout.close();
237     }
238     delete Trace_stream;
239     Trace_stream = NULL;
240   }
241   Current_scenario = NULL;
242 }
243 
244 //: Permit numeric locations to be accessed in scenarios.
245 :(before "End check_default_space Special-cases")
246 // user code should never create recipes with underscores in their names
247 if (caller.name.find("scenario_") == 0) return;  // skip Mu scenarios which will use raw memory locations
248 if (caller.name.find("run_") == 0) return;  // skip calls to 'run', which should be in scenarios and will also use raw memory locations
249 
250 //: Some variables for fake resources always get special /raw addresses in scenarios.
251 
252 :(code)
253 // Should contain everything passed by is_special_name but failed by is_disqualified.
254 void bind_special_scenario_names(const recipe_ordinal r) {
255   // Special Scenario Variable Names(r)
256   // End Special Scenario Variable Names(r)
257 }
258 :(before "Done Placing Ingredient(ingredient, inst, caller)")
259 maybe_make_raw(ingredient, caller);
260 :(before "Done Placing Product(product, inst, caller)")
261 maybe_make_raw(product, caller);
262 :(code)
263 void maybe_make_raw(reagent& r, const recipe& caller) {
264   if (!is_special_name(r.name)) return;
265   if (starts_with(caller.name, "scenario_"))
266     r.properties.push_back(pair<string, string_tree*>("raw", NULL));
267   // End maybe_make_raw
268 }
269 
270 //: Test.
271 :(before "End is_special_name Special-cases")
272 if (s == "__maybe_make_raw_test__") return true;
273 :(before "End Special Scenario Variable Names(r)")
274 //: ugly: we only need this for this one test, but need to define it for all time
275 Name[r]["__maybe_make_raw_test__"] = Reserved_for_tests-1;
276 :(code)
277 void test_maybe_make_raw() {
278   // check that scenarios can use local-scope and special variables together
279   vector<recipe_ordinal> tmp = load(
280       "def scenario_foo [\n"
281       "  local-scope\n"
282       "  __maybe_make_raw_test__:num <- copy 34\n"
283       "]\n");
284   mark_autogenerated(tmp.at(0));
285   bind_special_scenario_names(tmp.at(0));
286   transform_all();
287   run(tmp.at(0));
288   CHECK_TRACE_DOESNT_CONTAIN_ERRORS();
289 }
290 
291 //: Watch out for redefinitions of scenario routines. We should never ever be
292 //: doing that, regardless of anything else.
293 :(scenario forbid_redefining_scenario_even_if_forced)
294 % Hide_errors = true;
295 % Disable_redefine_checks = true;
296 def scenario-foo [
297   1:num <- copy 34
298 ]
299 def scenario-foo [
300   1:num <- copy 35
301 ]
302 +error: redefining recipe scenario-foo
303 
304 :(scenario scenario_containing_parse_error)
305 % Hide_errors = true;
306 scenario foo [
307   memory-should-contain [
308     1 <- 0
309   # missing ']'
310 ]
311 # no crash
312 
313 :(scenario scenario_containing_transform_error)
314 % Hide_errors = true;
315 def main [
316   local-scope
317   add x, 1
318 ]
319 # no crash
320 
321 :(after "bool should_check_for_redefine(const string& recipe_name)")
322   if (recipe_name.find("scenario-") == 0) return true;
323 
324 //:: The special instructions we want to support inside scenarios.
325 //: These are easy to support in an interpreter, but will require more work
326 //: when we eventually build a compiler.
327 
328 //: 'run' is a purely lexical convenience to separate the code actually being
329 //: tested from any setup or teardown
330 
331 :(scenario run)
332 def main [
333   run [
334     1:num <- copy 13
335   ]
336 ]
337 +mem: storing 13 in location 1
338 
339 :(before "End Rewrite Instruction(curr, recipe result)")
340 if (curr.name == "run") {
341   // Just inline all instructions inside the run block in the containing
342   // recipe. 'run' is basically a comment; pretend it doesn't exist.
343   istringstream in2("[\n"+curr.ingredients.at(0).name+"\n]\n");
344   slurp_body(in2, result);
345   curr.clear();
346 }
347 
348 :(scenario run_multiple)
349 def main [
350   run [
351     1:num <- copy 13
352   ]
353   run [
354     2:num <- copy 13
355   ]
356 ]
357 +mem: storing 13 in location 1
358 +mem: storing 13 in location 2
359 
360 //: 'memory-should-contain' raises errors if specific locations aren't as expected
361 //: Also includes some special support for checking strings.
362 
363 :(before "End Globals")
364 bool Scenario_testing_scenario = false;
365 :(before "End Setup")
366 Scenario_testing_scenario = false;
367 
368 :(scenario memory_check)
369 % Scenario_testing_scenario = true;
370 % Hide_errors = true;
371 def main [
372   memory-should-contain [
373     1 <- 13
374   ]
375 ]
376 +run: checking location 1
377 +error: expected location '1' to contain 13 but saw 0
378 
379 :(before "End Primitive Recipe Declarations")
380 MEMORY_SHOULD_CONTAIN,
381 :(before "End Primitive Recipe Numbers")
382 put(Recipe_ordinal, "memory-should-contain", MEMORY_SHOULD_CONTAIN);
383 :(before "End Primitive Recipe Checks")
384 case MEMORY_SHOULD_CONTAIN: {
385   break;
386 }
387 :(before "End Primitive Recipe Implementations")
388 case MEMORY_SHOULD_CONTAIN: {
389   if (!Passed) break;
390   check_memory(current_instruction().ingredients.at(0).name);
391   break;
392 }
393 
394 :(code)
395 void check_memory(const string& s) {
396   istringstream in(s);
397   in >> std::noskipws;
398   set<int> locations_checked;
399   while (true) {
400     skip_whitespace_and_comments(in);
401     if (!has_data(in)) break;
402     string lhs = next_word(in);
403     if (lhs.empty()) {
404       assert(!has_data(in));
405       raise << "incomplete 'memory-should-contain' block at end of file (0)\n" << end();
406       return;
407     }
408     if (!is_integer(lhs)) {
409       check_type(lhs, in);
410       continue;
411     }
412     int address = to_integer(lhs);
413     skip_whitespace_and_comments(in);
414     string _assign;  in >> _assign;  assert(_assign == "<-");
415     skip_whitespace_and_comments(in);
416     string rhs = next_word(in);
417     if (rhs.empty()) {
418       assert(!has_data(in));
419       raise << "incomplete 'memory-should-contain' block at end of file (1)\n" << end();
420       return;
421     }
422     if (!is_integer(rhs) && !is_noninteger(rhs)) {
423       if (Current_scenario && !Scenario_testing_scenario)
424         // genuine test in a .mu file
425         raise << "\nF - " << Current_scenario->name << ": location '" << address << "' can't contain non-number " << rhs << "\n" << end();
426       else
427         // just testing scenario support
428         raise << "location '" << address << "' can't contain non-number " << rhs << '\n' << end();
429       if (!Scenario_testing_scenario) Passed = false;
430       return;
431     }
432     double value = to_double(rhs);
433     if (contains_key(locations_checked, address))
434       raise << "duplicate expectation for location '" << address << "'\n" << end();
435     trace(9999, "run") << "checking location " << address << end();
436     if (get_or_insert(Memory, address) != value) {
437       if (Current_scenario && !Scenario_testing_scenario) {
438         // genuine test in a .mu file
439         raise << "\nF - " << Current_scenario->name << ": expected location '" << address << "' to contain " << no_scientific(value) << " but saw " << no_scientific(get_or_insert(Memory, address)) << '\n' << end();
440       }
441       else {
442         // just testing scenario support
443         raise << "expected location '" << address << "' to contain " << no_scientific(value) << " but saw " << no_scientific(get_or_insert(Memory, address)) << '\n' << end();
444       }
445       if (!Scenario_testing_scenario) Passed = false;
446       return;
447     }
448     locations_checked.insert(address);
449   }
450 }
451 
452 void check_type(const string& lhs, istream& in) {
453   reagent x(lhs);
454   if (is_mu_array(x.type) && is_mu_character(array_element(x.type))) {
455     x.set_value(to_integer(x.name));
456     skip_whitespace_and_comments(in);
457     string _assign = next_word(in);
458     if (_assign.empty()) {
459       assert(!has_data(in));
460       raise << "incomplete 'memory-should-contain' block at end of file (2)\n" << end();
461       return;
462     }
463     assert(_assign == "<-");
464     skip_whitespace_and_comments(in);
465     string literal = next_word(in);
466     if (literal.empty()) {
467       assert(!has_data(in));
468       raise << "incomplete 'memory-should-contain' block at end of file (3)\n" << end();
469       return;
470     }
471     int address = x.value;
472     // exclude quoting brackets
473     assert(*literal.begin() == '[');  literal.erase(literal.begin());
474     assert(*--literal.end() == ']');  literal.erase(--literal.end());
475     check_string(address, literal);
476     return;
477   }
478   // End Scenario Type Special-cases
479   raise << "don't know how to check memory for '" << lhs << "'\n" << end();
480 }
481 
482 void check_string(int address, const string& literal) {
483   trace(9999, "run") << "checking string length at " << address << end();
484   if (get_or_insert(Memory, address) != SIZE(literal)) {
485     if (Current_scenario && !Scenario_testing_scenario)
486       raise << "\nF - " << Current_scenario->name << ": expected location '" << address << "' to contain length " << SIZE(literal) << " of string [" << literal << "] but saw " << no_scientific(get_or_insert(Memory, address)) << " (" << read_mu_text(address-/*fake refcount*/1) << ")\n" << end();
487     else
488       raise << "expected location '" << address << "' to contain length " << SIZE(literal) << " of string [" << literal << "] but saw " << no_scientific(get_or_insert(Memory, address)) << '\n' << end();
489     if (!Scenario_testing_scenario) Passed = false;
490     return;
491   }
492   ++address;  // now skip length
493   for (int i = 0;  i < SIZE(literal);  ++i) {
494     trace(9999, "run") << "checking location " << address+i << end();
495     if (get_or_insert(Memory, address+i) != literal.at(i)) {
496       if (Current_scenario && !Scenario_testing_scenario) {
497         // genuine test in a .mu file
498         raise << "\nF - " << Current_scenario->name << ": expected location " << (address+i) << " to contain " << literal.at(i) << " but saw " << no_scientific(get_or_insert(Memory, address+i)) << " ('" << static_cast<char>(get_or_insert(Memory, address+i)) << "')\n" << end();
499       }
500       else {
501         // just testing scenario support
502         raise << "expected location " << (address+i) << " to contain " << literal.at(i) << " but saw " << no_scientific(get_or_insert(Memory, address+i)) << '\n' << end();
503       }
504       if (!Scenario_testing_scenario) Passed = false;
505       return;
506     }
507   }
508 }
509 
510 :(scenario memory_check_multiple)
511 % Scenario_testing_scenario = true;
512 % Hide_errors = true;
513 def main [
514   memory-should-contain [
515     1 <- 0
516     1 <- 0
517   ]
518 ]
519 +error: duplicate expectation for location '1'
520 
521 :(scenario memory_check_string_length)
522 % Scenario_testing_scenario = true;
523 % Hide_errors = true;
524 def main [
525   1:num <- copy 3
526   2:num <- copy 97  # 'a'
527   3:num <- copy 98  # 'b'
528   4:num <- copy 99  # 'c'
529   memory-should-contain [
530     1:array:character <- [ab]
531   ]
532 ]
533 +error: expected location '1' to contain length 2 of string [ab] but saw 3
534 
535 :(scenario memory_check_string)
536 def main [
537   1:num <- copy 3
538   2:num <- copy 97  # 'a'
539   3:num <- copy 98  # 'b'
540   4:num <- copy 99  # 'c'
541   memory-should-contain [
542     1:array:character <- [abc]
543   ]
544 ]
545 +run: checking string length at 1
546 +run: checking location 2
547 +run: checking location 3
548 +run: checking location 4
549 
550 :(scenario memory_invalid_string_check)
551 % Scenario_testing_scenario = true;
552 % Hide_errors = true;
553 def main [
554   memory-should-contain [
555     1 <- [abc]
556   ]
557 ]
558 +error: location '1' can't contain non-number [abc]
559 
560 :(scenario memory_check_with_comment)
561 % Scenario_testing_scenario = true;
562 % Hide_errors = true;
563 def main [
564   memory-should-contain [
565     1 <- 34  # comment
566   ]
567 ]
568 -error: location 1 can't contain non-number 34  # comment
569 # but there'll be an error signalled by memory-should-contain
570 
571 //: 'trace-should-contain' is like the '+' lines in our scenarios so far
572 // Like runs of contiguous '+' lines, order is important. The trace checks
573 // that the lines are present *and* in the specified sequence. (There can be
574 // other lines in between.)
575 
576 :(scenario trace_check_fails)
577 % Scenario_testing_scenario = true;
578 % Hide_errors = true;
579 def main [
580   trace-should-contain [
581     a: b
582     a: d
583   ]
584 ]
585 +error: missing [b] in trace with label 'a'
586 
587 :(before "End Primitive Recipe Declarations")
588 TRACE_SHOULD_CONTAIN,
589 :(before "End Primitive Recipe Numbers")
590 put(Recipe_ordinal, "trace-should-contain", TRACE_SHOULD_CONTAIN);
591 :(before "End Primitive Recipe Checks")
592 case TRACE_SHOULD_CONTAIN: {
593   break;
594 }
595 :(before "End Primitive Recipe Implementations")
596 case TRACE_SHOULD_CONTAIN: {
597   if (!Passed) break;
598   check_trace(current_instruction().ingredients.at(0).name);
599   break;
600 }
601 
602 :(code)
603 // simplified version of check_trace_contents() that emits errors rather
604 // than just printing to stderr
605 void check_trace(const string& expected) {
606   Trace_stream->newline();
607   vector<trace_line> expected_lines = parse_trace(expected);
608   if (expected_lines.empty()) return;
609   int curr_expected_line = 0;
610   for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin();  p != Trace_stream->past_lines.end();  ++p) {
611     if (expected_lines.at(curr_expected_line).label != p->label) continue;
612     if (expected_lines.at(curr_expected_line).contents != trim(p->contents)) continue;
613     // match
614     ++curr_expected_line;
615     if (curr_expected_line == SIZE(expected_lines)) return;
616   }
617   if (Current_scenario && !Scenario_testing_scenario)
618     raise << "\nF - " << Current_scenario->name << ": missing [" << expected_lines.at(curr_expected_line).contents << "] "
619           << "in trace with label '" << expected_lines.at(curr_expected_line).label << "'\n" << end();
620   else
621     raise << "missing [" << expected_lines.at(curr_expected_line).contents << "] "
622           << "in trace with label '" << expected_lines.at(curr_expected_line).label << "'\n" << end();
623   if (!Hide_errors)
624     DUMP(expected_lines.at(curr_expected_line).label);
625   if (!Scenario_testing_scenario) Passed = false;
626 }
627 
628 vector<trace_line> parse_trace(const string& expected) {
629   vector<string> buf = split(expected, "\n");
630   vector<trace_line> result;
631   for (int i = 0;  i < SIZE(buf);  ++i) {
632     buf.at(i) = trim(buf.at(i));
633     if (buf.at(i).empty()) continue;
634     int delim = buf.at(i).find(": ");
635     if (delim == -1) {
636       raise << Current_scenario->name << ": lines in 'trace-should-contain' should be of the form <label>: <contents>. Both parts are required.\n" << end();
637       result.clear();
638       return result;
639     }
640     result.push_back(trace_line(trim(buf.at(i).substr(0, delim)),  trim(buf.at(i).substr(delim+2))));
641   }
642   return result;
643 }
644 
645 :(scenario trace_check_fails_in_nonfirst_line)
646 % Scenario_testing_scenario = true;
647 % Hide_errors = true;
648 def main [
649   run [
650     trace 1, [a], [b]
651   ]
652   trace-should-contain [
653     a: b
654     a: d
655   ]
656 ]
657 +error: missing [d] in trace with label 'a'
658 
659 :(scenario trace_check_passes_silently)
660 % Scenario_testing_scenario = true;
661 def main [
662   run [
663     trace 1, [a], [b]
664   ]
665   trace-should-contain [
666     a: b
667   ]
668 ]
669 -error: missing [b] in trace with label 'a'
670 $error: 0
671 
672 //: 'trace-should-not-contain' is like the '-' lines in our scenarios so far
673 //: Each trace line is separately checked for absense. Order is *not*
674 //: important, so you can't say things like "B should not exist after A."
675 
676 :(scenario trace_negative_check_fails)
677 % Scenario_testing_scenario = true;
678 % Hide_errors = true;
679 def main [
680   run [
681     trace 1, [a], [b]
682   ]
683   trace-should-not-contain [
684     a: b
685   ]
686 ]
687 +error: unexpected [b] in trace with label 'a'
688 
689 :(before "End Primitive Recipe Declarations")
690 TRACE_SHOULD_NOT_CONTAIN,
691 :(before "End Primitive Recipe Numbers")
692 put(Recipe_ordinal, "trace-should-not-contain", TRACE_SHOULD_NOT_CONTAIN);
693 :(before "End Primitive Recipe Checks")
694 case TRACE_SHOULD_NOT_CONTAIN: {
695   break;
696 }
697 :(before "End Primitive Recipe Implementations")
698 case TRACE_SHOULD_NOT_CONTAIN: {
699   if (!Passed) break;
700   check_trace_missing(current_instruction().ingredients.at(0).name);
701   break;
702 }
703 
704 :(code)
705 // simplified version of check_trace_contents() that emits errors rather
706 // than just printing to stderr
707 bool check_trace_missing(const string& in) {
708   Trace_stream->newline();
709   vector<trace_line> lines = parse_trace(in);
710   for (int i = 0;  i < SIZE(lines);  ++i) {
711     if (trace_count(lines.at(i).label, lines.at(i).contents) != 0) {
712       raise << "unexpected [" << lines.at(i).contents << "] in trace with label '" << lines.at(i).label << "'\n" << end();
713       if (!Scenario_testing_scenario) Passed = false;
714       return false;
715     }
716   }
717   return true;
718 }
719 
720 :(scenario trace_negative_check_passes_silently)
721 % Scenario_testing_scenario = true;
722 def main [
723   trace-should-not-contain [
724     a: b
725   ]
726 ]
727 -error: unexpected [b] in trace with label 'a'
728 $error: 0
729 
730 :(scenario trace_negative_check_fails_on_any_unexpected_line)
731 % Scenario_testing_scenario = true;
732 % Hide_errors = true;
733 def main [
734   run [
735     trace 1, [a], [d]
736   ]
737   trace-should-not-contain [
738     a: b
739     a: d
740   ]
741 ]
742 +error: unexpected [d] in trace with label 'a'
743 
744 :(scenario trace_count_check)
745 def main [
746   run [
747     trace 1, [a], [foo]
748   ]
749   check-trace-count-for-label 1, [a]
750 ]
751 # checks are inside scenario
752 
753 :(before "End Primitive Recipe Declarations")
754 CHECK_TRACE_COUNT_FOR_LABEL,
755 :(before "End Primitive Recipe Numbers")
756 put(Recipe_ordinal, "check-trace-count-for-label", CHECK_TRACE_COUNT_FOR_LABEL);
757 :(before "End Primitive Recipe Checks")
758 case CHECK_TRACE_COUNT_FOR_LABEL: {
759   if (SIZE(inst.ingredients) != 2) {
760     raise << maybe(get(Recipe, r).name) << "'check-trace-count-for-label' requires exactly two ingredients, but got '" << inst.original_string << "'\n" << end();
761     break;
762   }
763   if (!is_mu_number(inst.ingredients.at(0))) {
764     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();
765     break;
766   }
767   if (!is_literal_text(inst.ingredients.at(1))) {
768     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();
769     break;
770   }
771   break;
772 }
773 :(before "End Primitive Recipe Implementations")
774 case CHECK_TRACE_COUNT_FOR_LABEL: {
775   if (!Passed) break;
776   int expected_count = ingredients.at(0).at(0);
777   string label = current_instruction().ingredients.at(1).name;
778   int count = trace_count(label);
779   if (count != expected_count) {
780     if (Current_scenario && !Scenario_testing_scenario) {
781       // genuine test in a .mu file
782       raise << "\nF - " << Current_scenario->name << ": " << maybe(current_recipe_name()) << "expected " << expected_count << " lines in trace with label '" << label << "' in trace: " << end();
783       DUMP(label);
784     }
785     else {
786       // just testing scenario support
787       raise << maybe(current_recipe_name()) << "expected " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
788     }
789     if (!Scenario_testing_scenario) Passed = false;
790   }
791   break;
792 }
793 
794 :(scenario trace_count_check_2)
795 % Scenario_testing_scenario = true;
796 % Hide_errors = true;
797 def main [
798   run [
799     trace 1, [a], [foo]
800   ]
801   check-trace-count-for-label 2, [a]
802 ]
803 +error: main: expected 2 lines in trace with label 'a' in trace
804 
805 //: Minor detail: ignore 'system' calls in scenarios, since anything we do
806 //: with them is by definition impossible to test through Mu.
807 :(after "case _SYSTEM:")
808   if (Current_scenario) break;
809 
810 //:: Warn if people use '_' manually in function names. They're reserved for internal use.
811 
812 :(scenario recipe_name_with_underscore)
813 % Hide_errors = true;
814 def foo_bar [
815 ]
816 +error: foo_bar: don't create recipes with '_' in the name
817 
818 :(before "End recipe Fields")
819 bool is_autogenerated;
820 :(before "End recipe Constructor")
821 is_autogenerated = false;
822 :(code)
823 void mark_autogenerated(recipe_ordinal r) {
824   get(Recipe, r).is_autogenerated = true;
825 }
826 
827 :(after "void transform_all()")
828   for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
829     const recipe& r = p->second;
830     if (r.name.find('_') == string::npos) continue;
831     if (r.is_autogenerated) continue;  // created by previous call to transform_all()
832     raise << r.name << ": don't create recipes with '_' in the name\n" << end();
833   }
834 
835 //:: Helpers
836 
837 :(code)
838 // just for the scenarios running scenarios in C++ layers
839 void run_mu_scenario(const string& form) {
840   Scenario_names.clear();
841   istringstream in(form);
842   in >> std::noskipws;
843   skip_whitespace_and_comments(in);
844   string _scenario = next_word(in);
845   if (_scenario.empty()) {
846     assert(!has_data(in));
847     raise << "no scenario in string passed into run_mu_scenario()\n" << end();
848     return;
849   }
850   assert(_scenario == "scenario");
851   scenario s = parse_scenario(in);
852   run_mu_scenario(s);
853 }