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) {
233   ¦ ¦ ofstream fout("last_trace");
234   ¦ ¦ fout << Trace_stream->readable_contents("");
235   ¦ ¦ fout.close();
236   ¦ }
237   ¦ delete Trace_stream;
238   ¦ Trace_stream = NULL;
239   }
240   Current_scenario = NULL;
241 }
242 
243 //: Permit numeric locations to be accessed in scenarios.
244 :(before "End check_default_space Special-cases")
245 // user code should never create recipes with underscores in their names
246 if (starts_with(caller.name, "scenario_")) return;  // skip Mu scenarios which will use raw memory locations
247 if (starts_with(caller.name, "run_")) return;  // skip calls to 'run', which should be in scenarios and will also use raw memory locations
248 
249 :(before "End maybe(recipe_name) Special-cases")
250 if (starts_with(recipe_name, "scenario_"))
251   return recipe_name.substr(strlen("scenario_")) + ": ";
252 
253 //: Some variables for fake resources always get special /raw addresses in scenarios.
254 
255 :(code)
256 // Should contain everything passed by is_special_name but failed by is_disqualified.
257 void bind_special_scenario_names(const recipe_ordinal r) {
258   // Special Scenario Variable Names(r)
259   // End Special Scenario Variable Names(r)
260 }
261 :(before "Done Placing Ingredient(ingredient, inst, caller)")
262 maybe_make_raw(ingredient, caller);
263 :(before "Done Placing Product(product, inst, caller)")
264 maybe_make_raw(product, caller);
265 :(code)
266 void maybe_make_raw(reagent& r, const recipe& caller) {
267   if (!is_special_name(r.name)) return;
268   if (starts_with(caller.name, "scenario_"))
269   ¦ r.properties.push_back(pair<string, string_tree*>("raw", NULL));
270   // End maybe_make_raw
271 }
272 
273 //: Test.
274 :(before "End is_special_name Special-cases")
275 if (s == "__maybe_make_raw_test__") return true;
276 :(before "End Special Scenario Variable Names(r)")
277 //: ugly: we only need this for this one test, but need to define it for all time
278 Name[r]["__maybe_make_raw_test__"] = Reserved_for_tests-1;
279 :(code)
280 void test_maybe_make_raw() {
281   // check that scenarios can use local-scope and special variables together
282   vector<recipe_ordinal> tmp = load(
283   ¦ ¦ "def scenario_foo [\n"
284   ¦ ¦ "  local-scope\n"
285   ¦ ¦ "  __maybe_make_raw_test__:num <- copy 34\n"
286   ¦ ¦ "]\n");
287   mark_autogenerated(tmp.at(0));
288   bind_special_scenario_names(tmp.at(0));
289   transform_all();
290   run(tmp.at(0));
291   CHECK_TRACE_DOESNT_CONTAIN_ERRORS();
292 }
293 
294 //: Watch out for redefinitions of scenario routines. We should never ever be
295 //: doing that, regardless of anything else.
296 :(scenario forbid_redefining_scenario_even_if_forced)
297 % Hide_errors = true;
298 % Disable_redefine_checks = true;
299 def scenario-foo [
300   1:num <- copy 34
301 ]
302 def scenario-foo [
303   1:num <- copy 35
304 ]
305 +error: redefining recipe scenario-foo
306 
307 :(scenario scenario_containing_parse_error)
308 % Hide_errors = true;
309 scenario foo [
310   memory-should-contain [
311   ¦ 1 <- 0
312   # missing ']'
313 ]
314 # no crash
315 
316 :(scenario scenario_containing_transform_error)
317 % Hide_errors = true;
318 def main [
319   local-scope
320   add x, 1
321 ]
322 # no crash
323 
324 :(after "bool should_check_for_redefine(const string& recipe_name)")
325   if (recipe_name.find("scenario-") == 0) return true;
326 
327 //:: The special instructions we want to support inside scenarios.
328 //: These are easy to support in an interpreter, but will require more work
329 //: when we eventually build a compiler.
330 
331 //: 'run' is a purely lexical convenience to separate the code actually being
332 //: tested from any setup
333 
334 :(scenario run)
335 def main [
336   run [
337   ¦ 1:num <- copy 13
338   ]
339 ]
340 +mem: storing 13 in location 1
341 
342 :(before "End Rewrite Instruction(curr, recipe result)")
343 if (curr.name == "run") {
344   // Just inline all instructions inside the run block in the containing
345   // recipe. 'run' is basically a comment; pretend it doesn't exist.
346   istringstream in2("[\n"+curr.ingredients.at(0).name+"\n]\n");
347   slurp_body(in2, result);
348   curr.clear();
349 }
350 
351 :(scenario run_multiple)
352 def main [
353   run [
354   ¦ 1:num <- copy 13
355   ]
356   run [
357   ¦ 2:num <- copy 13
358   ]
359 ]
360 +mem: storing 13 in location 1
361 +mem: storing 13 in location 2
362 
363 //: 'memory-should-contain' raises errors if specific locations aren't as expected
364 //: Also includes some special support for checking Mu texts.
365 
366 :(before "End Globals")
367 bool Scenario_testing_scenario = false;
368 :(before "End Reset")
369 Scenario_testing_scenario = false;
370 
371 :(scenario memory_check)
372 % Scenario_testing_scenario = true;
373 % Hide_errors = true;
374 def main [
375   memory-should-contain [
376   ¦ 1 <- 13
377   ]
378 ]
379 +run: checking location 1
380 +error: F - main: expected location '1' to contain 13 but saw 0
381 
382 :(before "End Primitive Recipe Declarations")
383 MEMORY_SHOULD_CONTAIN,
384 :(before "End Primitive Recipe Numbers")
385 put(Recipe_ordinal, "memory-should-contain", MEMORY_SHOULD_CONTAIN);
386 :(before "End Primitive Recipe Checks")
387 case MEMORY_SHOULD_CONTAIN: {
388   break;
389 }
390 :(before "End Primitive Recipe Implementations")
391 case MEMORY_SHOULD_CONTAIN: {
392   if (!Passed) break;
393   check_memory(current_instruction().ingredients.at(0).name);
394   break;
395 }
396 
397 :(code)
398 void check_memory(const string& s) {
399   istringstream in(s);
400   in >> std::noskipws;
401   set<int> locations_checked;
402   while (true) {
403   ¦ skip_whitespace_and_comments(in);
404   ¦ if (!has_data(in)) break;
405   ¦ string lhs = next_word(in);
406   ¦ if (lhs.empty()) {
407   ¦ ¦ assert(!has_data(in));
408   ¦ ¦ raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (0)\n" << end();
409   ¦ ¦ return;
410   ¦ }
411   ¦ if (!is_integer(lhs)) {
412   ¦ ¦ check_type(lhs, in);
413   ¦ ¦ continue;
414   ¦ }
415   ¦ int address = to_integer(lhs);
416   ¦ skip_whitespace_and_comments(in);
417   ¦ string _assign;  in >> _assign;  assert(_assign == "<-");
418   ¦ skip_whitespace_and_comments(in);
419   ¦ string rhs = next_word(in);
420   ¦ if (rhs.empty()) {
421   ¦ ¦ assert(!has_data(in));
422   ¦ ¦ raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (1)\n" << end();
423   ¦ ¦ return;
424   ¦ }
425   ¦ if (!is_integer(rhs) && !is_noninteger(rhs)) {
426   ¦ ¦ if (!Hide_errors) cerr << '\n';
427   ¦ ¦ raise << "F - " << maybe(current_recipe_name()) << "location '" << address << "' can't contain non-number " << rhs << '\n' << end();
428   ¦ ¦ if (!Scenario_testing_scenario) Passed = false;
429   ¦ ¦ return;
430   ¦ }
431   ¦ double value = to_double(rhs);
432   ¦ if (contains_key(locations_checked, address))
433   ¦ ¦ raise << maybe(current_recipe_name()) << "duplicate expectation for location '" << address << "'\n" << end();
434   ¦ trace(9999, "run") << "checking location " << address << end();
435   ¦ if (get_or_insert(Memory, address) != value) {
436   ¦ ¦ if (!Hide_errors) cerr << '\n';
437   ¦ ¦ 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();
438   ¦ ¦ if (!Scenario_testing_scenario) Passed = false;
439   ¦ ¦ return;
440   ¦ }
441   ¦ locations_checked.insert(address);
442   }
443 }
444 
445 void check_type(const string& lhs, istream& in) {
446   reagent x(lhs);
447   if (is_mu_array(x.type) && is_mu_character(array_element(x.type))) {
448   ¦ x.set_value(to_integer(x.name));
449   ¦ skip_whitespace_and_comments(in);
450   ¦ string _assign = next_word(in);
451   ¦ if (_assign.empty()) {
452   ¦ ¦ assert(!has_data(in));
453   ¦ ¦ raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (2)\n" << end();
454   ¦ ¦ return;
455   ¦ }
456   ¦ assert(_assign == "<-");
457   ¦ skip_whitespace_and_comments(in);
458   ¦ string literal = next_word(in);
459   ¦ if (literal.empty()) {
460   ¦ ¦ assert(!has_data(in));
461   ¦ ¦ raise << maybe(current_recipe_name()) << "incomplete 'memory-should-contain' block at end of file (3)\n" << end();
462   ¦ ¦ return;
463   ¦ }
464   ¦ int address = x.value;
465   ¦ // exclude quoting brackets
466   ¦ if (*literal.begin() != '[') {
467   ¦ ¦ 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();
468   ¦ ¦ return;
469   ¦ }
470   ¦ literal.erase(literal.begin());
471   ¦ assert(*--literal.end() == ']');  literal.erase(--literal.end());
472   ¦ check_mu_text(address, literal);
473   ¦ return;
474   }
475   // End Scenario Type Special-cases
476   raise << "don't know how to check memory for '" << lhs << "'\n" << end();
477 }
478 
479 void check_mu_text(int start, const string& literal) {
480   trace(9999, "run") << "checking text length at " << start << end();
481   int array_length = static_cast<int>(get_or_insert(Memory, start));
482   if (array_length != SIZE(literal)) {
483   ¦ if (!Hide_errors) cerr << '\n';
484   ¦ 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();
485   ¦ if (!Scenario_testing_scenario) Passed = false;
486   ¦ return;
487   }
488   int curr = start+1;  // now skip length
489   for (int i = 0;  i < SIZE(literal);  ++i) {
490   ¦ trace(9999, "run") << "checking location " << curr+i << end();
491   ¦ if (get_or_insert(Memory, curr+i) != literal.at(i)) {
492   ¦ ¦ if (!Hide_errors) cerr << '\n';
493   ¦ ¦ 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();
494   ¦ ¦ if (!Scenario_testing_scenario) Passed = false;
495   ¦ ¦ return;
496   ¦ }
497   }
498 }
499 
500 :(scenario memory_check_multiple)
501 % Scenario_testing_scenario = true;
502 % Hide_errors = true;
503 def main [
504   memory-should-contain [
505   ¦ 1 <- 0
506   ¦ 1 <- 0
507   ]
508 ]
509 +error: main: duplicate expectation for location '1'
510 
511 :(scenario memory_check_mu_text_length)
512 % Scenario_testing_scenario = true;
513 % Hide_errors = true;
514 def main [
515   1:num <- copy 3
516   2:num <- copy 97  # 'a'
517   3:num <- copy 98  # 'b'
518   4:num <- copy 99  # 'c'
519   memory-should-contain [
520   ¦ 1:array:character <- [ab]
521   ]
522 ]
523 +error: F - main: expected location '1' to contain length 2 of text [ab] but saw 3 (for text [abc])
524 
525 :(scenario memory_check_mu_text)
526 def main [
527   1:num <- copy 3
528   2:num <- copy 97  # 'a'
529   3:num <- copy 98  # 'b'
530   4:num <- copy 99  # 'c'
531   memory-should-contain [
532   ¦ 1:array:character <- [abc]
533   ]
534 ]
535 +run: checking text length at 1
536 +run: checking location 2
537 +run: checking location 3
538 +run: checking location 4
539 
540 :(scenario memory_invalid_string_check)
541 % Scenario_testing_scenario = true;
542 % Hide_errors = true;
543 def main [
544   memory-should-contain [
545   ¦ 1 <- [abc]
546   ]
547 ]
548 +error: F - main: location '1' can't contain non-number [abc]
549 
550 :(scenario memory_invalid_string_check2)
551 % Hide_errors = true;
552 def main [
553   1:num <- copy 3
554   2:num <- copy 97  # 'a'
555   3:num <- copy 98  # 'b'
556   4:num <- copy 99  # 'c'
557   memory-should-contain [
558   ¦ 1:array:character <- 0
559   ]
560 ]
561 +error: main: array:character types inside 'memory-should-contain' can only be compared with text literals surrounded by [], not '0'
562 
563 :(scenario memory_check_with_comment)
564 % Scenario_testing_scenario = true;
565 % Hide_errors = true;
566 def main [
567   memory-should-contain [
568   ¦ 1 <- 34  # comment
569   ]
570 ]
571 -error: location 1 can't contain non-number 34  # comment
572 # but there'll be an error signalled by memory-should-contain
573 
574 //: 'trace-should-contain' is like the '+' lines in our scenarios so far
575 // Like runs of contiguous '+' lines, order is important. The trace checks
576 // that the lines are present *and* in the specified sequence. (There can be
577 // other lines in between.)
578 
579 :(scenario trace_check_fails)
580 % Scenario_testing_scenario = true;
581 % Hide_errors = true;
582 def main [
583   trace-should-contain [
584   ¦ a: b
585   ¦ a: d
586   ]
587 ]
588 +error: F - main: missing [b] in trace with label 'a'
589 
590 :(before "End Primitive Recipe Declarations")
591 TRACE_SHOULD_CONTAIN,
592 :(before "End Primitive Recipe Numbers")
593 put(Recipe_ordinal, "trace-should-contain", TRACE_SHOULD_CONTAIN);
594 :(before "End Primitive Recipe Checks")
595 case TRACE_SHOULD_CONTAIN: {
596   break;
597 }
598 :(before "End Primitive Recipe Implementations")
599 case TRACE_SHOULD_CONTAIN: {
600   if (!Passed) break;
601   check_trace(current_instruction().ingredients.at(0).name);
602   break;
603 }
604 
605 :(code)
606 // simplified version of check_trace_contents() that emits errors rather
607 // than just printing to stderr
608 void check_trace(const string& expected) {
609   Trace_stream->newline();
610   vector<trace_line> expected_lines = parse_trace(expected);
611   if (expected_lines.empty()) return;
612   int curr_expected_line = 0;
613   for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin();  p != Trace_stream->past_lines.end();  ++p) {
614   ¦ if (expected_lines.at(curr_expected_line).label != p->label) continue;
615   ¦ if (expected_lines.at(curr_expected_line).contents != trim(p->contents)) continue;
616   ¦ // match
617   ¦ ++curr_expected_line;
618   ¦ if (curr_expected_line == SIZE(expected_lines)) return;
619   }
620   if (!Hide_errors) cerr << '\n';
621   raise << "F - " << maybe(current_recipe_name()) << "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 << maybe(current_recipe_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: F - main: 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: F - main: 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 << "F - " << maybe(current_recipe_name()) << "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: F - main: 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 '" << to_original_string(inst) << "'\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 (!Hide_errors) cerr << '\n';
781   ¦ raise << "F - " << maybe(current_recipe_name()) << "expected " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
782   ¦ if (!Hide_errors) DUMP(label);
783   ¦ if (!Scenario_testing_scenario) Passed = false;
784   }
785   break;
786 }
787 
788 :(before "End Primitive Recipe Declarations")
789 CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN,
790 :(before "End Primitive Recipe Numbers")
791 put(Recipe_ordinal, "check-trace-count-for-label-greater-than", CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN);
792 :(before "End Primitive Recipe Checks")
793 case CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN: {
794   if (SIZE(inst.ingredients) != 2) {
795   ¦ raise << maybe(get(Recipe, r).name) << "'check-trace-count-for-label' requires exactly two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
796   ¦ break;
797   }
798   if (!is_mu_number(inst.ingredients.at(0))) {
799   ¦ 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();
800   ¦ break;
801   }
802   if (!is_literal_text(inst.ingredients.at(1))) {
803   ¦ 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();
804   ¦ break;
805   }
806   break;
807 }
808 :(before "End Primitive Recipe Implementations")
809 case CHECK_TRACE_COUNT_FOR_LABEL_GREATER_THAN: {
810   if (!Passed) break;
811   int expected_count = ingredients.at(0).at(0);
812   string label = current_instruction().ingredients.at(1).name;
813   int count = trace_count(label);
814   if (count <= expected_count) {
815   ¦ if (!Hide_errors) cerr << '\n';
816   ¦ raise << maybe(current_recipe_name()) << "expected more than " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
817   ¦ if (!Hide_errors) {
818   ¦ ¦ cerr << "trace contents:\n";
819   ¦ ¦ DUMP(label);
820   ¦ }
821   ¦ if (!Scenario_testing_scenario) Passed = false;
822   }
823   break;
824 }
825 
826 :(before "End Primitive Recipe Declarations")
827 CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN,
828 :(before "End Primitive Recipe Numbers")
829 put(Recipe_ordinal, "check-trace-count-for-label-lesser-than", CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN);
830 :(before "End Primitive Recipe Checks")
831 case CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN: {
832   if (SIZE(inst.ingredients) != 2) {
833   ¦ raise << maybe(get(Recipe, r).name) << "'check-trace-count-for-label' requires exactly two ingredients, but got '" << to_original_string(inst) << "'\n" << end();
834   ¦ break;
835   }
836   if (!is_mu_number(inst.ingredients.at(0))) {
837   ¦ 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();
838   ¦ break;
839   }
840   if (!is_literal_text(inst.ingredients.at(1))) {
841   ¦ 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();
842   ¦ break;
843   }
844   break;
845 }
846 :(before "End Primitive Recipe Implementations")
847 case CHECK_TRACE_COUNT_FOR_LABEL_LESSER_THAN: {
848   if (!Passed) break;
849   int expected_count = ingredients.at(0).at(0);
850   string label = current_instruction().ingredients.at(1).name;
851   int count = trace_count(label);
852   if (count >= expected_count) {
853   ¦ if (!Hide_errors) cerr << '\n';
854   ¦ raise << "F - " << maybe(current_recipe_name()) << "expected less than " << expected_count << " lines in trace with label '" << label << "' in trace\n" << end();
855   ¦ if (!Hide_errors) {
856   ¦ ¦ cerr << "trace contents:\n";
857   ¦ ¦ DUMP(label);
858   ¦ }
859   ¦ if (!Scenario_testing_scenario) Passed = false;
860   }
861   break;
862 }
863 
864 :(scenario trace_count_check_2)
865 % Scenario_testing_scenario = true;
866 % Hide_errors = true;
867 def main [
868   run [
869   ¦ trace 1, [a], [foo]
870   ]
871   check-trace-count-for-label 2, [a]
872 ]
873 +error: F - main: expected 2 lines in trace with label 'a' in trace
874 
875 //: Minor detail: ignore 'system' calls in scenarios, since anything we do
876 //: with them is by definition impossible to test through Mu.
877 :(after "case _SYSTEM:")
878   if (Current_scenario) break;
879 
880 //:: Warn if people use '_' manually in function names. They're reserved for internal use.
881 
882 :(scenario recipe_name_with_underscore)
883 % Hide_errors = true;
884 def foo_bar [
885 ]
886 +error: foo_bar: don't create recipes with '_' in the name
887 
888 :(before "End recipe Fields")
889 bool is_autogenerated;
890 :(before "End recipe Constructor")
891 is_autogenerated = false;
892 :(code)
893 void mark_autogenerated(recipe_ordinal r) {
894   get(Recipe, r).is_autogenerated = true;
895 }
896 
897 :(after "void transform_all()")
898   for (map<recipe_ordinal, recipe>::iterator p = Recipe.begin();  p != Recipe.end();  ++p) {
899   ¦ const recipe& r = p->second;
900   ¦ if (r.name.find('_') == string::npos) continue;
901   ¦ if (r.is_autogenerated) continue;  // created by previous call to transform_all()
902   ¦ raise << r.name << ": don't create recipes with '_' in the name\n" << end();
903   }
904 
905 //:: Helpers
906 
907 :(code)
908 // just for the scenarios running scenarios in C++ layers
909 void run_mu_scenario(const string& form) {
910   Scenario_names.clear();
911   istringstream in(form);
912   in >> std::noskipws;
913   skip_whitespace_and_comments(in);
914   string _scenario = next_word(in);
915   if (_scenario.empty()) {
916   ¦ assert(!has_data(in));
917   ¦ raise << "no scenario in string passed into run_mu_scenario()\n" << end();
918   ¦ return;
919   }
920   assert(_scenario == "scenario");
921   scenario s = parse_scenario(in);
922   run_mu_scenario(s);
923 }