about summary refs log tree commit diff stats
path: root/cpp/052scenario_trace.cc
blob: cf782835c2b2d885e6c1b54d54720adc0f47625d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//: Support a trace block inside scenario blocks.
//: Allows two kinds of assertions:
//:   trace must contain [ ... ]
//:   trace must not contain [ ... ]

:(scenarios "parse_scenario")
:(scenario scenario_trace_block)
scenario foo [
  trace should contain [
    bar: baz
    bar: floo
  ]
  trace should not contain [
    bar: abc
  ]

]
+scenario: +bar: baz
+scenario: +bar: floo
+scenario: -bar: abc

:(before "End scenario Fields")
vector<pair<string, string> > trace_checks;
vector<pair<string, string> > trace_negative_checks;

:(before "End Scenario Command Handlers")
else if (scenario_command == "trace") {
  handle_scenario_trace_directive(inner, x);
}

:(before "End Scenario Checks")
check_trace_contents(Scenarios[i]);
check_trace_negative_contents(Scenarios[i]);

:(code)
void handle_scenario_trace_directive(istream& in, scenario& out) {
  if (next_word(in) != "should") {
    raise << "'trace' directive inside scenario must continue 'trace should'\n";
  }
  string s = next_word(in);
  if (s == "not") {
    handle_scenario_trace_negative_directive(in, out);
    return;
  }
  if (s != "contain") {
    raise << "'trace' directive inside scenario must continue 'trace should [not] contain'\n";
  }
  skip_bracket(in, "'trace' directive inside scenario must begin with 'trace should contain ['\n");
  while (true) {
    skip_whitespace_and_comments(in);
    if (in.eof()) break;
    if (in.peek() == ']') break;
    string curr_line;
    getline(in, curr_line);
    istringstream tmp(curr_line);
    tmp >> std::noskipws;
    string label = slurp_until(tmp, ':');
    if (tmp.get() != ' ') {
      raise << "'trace' directive inside scenario should contain lines of the form 'label: message', instead got " << curr_line;
      continue;
    }
    string message = slurp_rest(tmp);
    out.trace_checks.push_back(pair<string, string>(label, message));
    trace("scenario") << '+' << label << ": " << message << '\n';
  }
  skip_whitespace(in);
  assert(in.get() == ']');
}

void handle_scenario_trace_negative_directive(istream& in, scenario& out) {
  // 'not' already slurped
  if (next_word(in) != "contain") {
    raise << "'trace' directive inside scenario must continue 'trace should not contain'\n";
  }
  skip_bracket(in, "'trace' directive inside scenario must begin with 'trace should not contain ['\n");
  while (true) {
    skip_whitespace_and_comments(in);
    if (in.eof()) break;
    if (in.peek() == ']') break;
    string curr_line;
    getline(in, curr_line);
    istringstream tmp(curr_line);
    tmp >> std::noskipws;
    string label = slurp_until(tmp, ':');
    if (tmp.get() != ' ') {
      raise << "'trace' directive inside scenario should contain lines of the form 'label: message', instead got " << curr_line;
      continue;
    }
    string message = slurp_rest(tmp);
    out.trace_negative_checks.push_back(pair<string, string>(label, message));
    trace("scenario") << '-' << label << ": " << message << '\n';
  }
  skip_whitespace(in);
  assert(in.get() == ']');
}

string slurp_rest(istream& in) {
  ostringstream out;
  char c;
  while (in >> c) {
    out << c;
  }
  return out.str();
}

void check_trace_contents(const scenario& s) {
  if (s.trace_checks.empty()) return;
  ostringstream contents;
  for (size_t i = 0; i < s.trace_checks.size(); ++i) {
    contents << s.trace_checks[i].first << ": " << s.trace_checks[i].second << "";
  }
  CHECK_TRACE_CONTENTS(contents.str());
}

void check_trace_negative_contents(const scenario& s) {
  for (size_t i = 0; i < s.trace_negative_checks.size(); ++i) {
    if (trace_count(s.trace_negative_checks[i].first, s.trace_negative_checks[i].second) > 0) {
      raise << "trace shouldn't contain " << s.trace_negative_checks[i].first << ": " << s.trace_negative_checks[i].second << '\n';
      Passed = false;
    }
  }
}