THINK FIRST·CODE LATER

← All labs

Given/When/Then scenario validator

Problem

Acceptance criteria are only useful if they are well-formed. Validate one scenario written in Given/When/Then style.

Read lines until the end of input. Ignore empty lines and a first line starting with Scenario:. Every other line (after trimming) must start with one of the keywords Given, When, Then, And, But followed by a space. The rules:

  • The steps must follow the order Given* → When+ → Then+ (zero or more Given, then at least one When, then at least one Then). And/But continue the previous keyword (they are not allowed as the first step).
  • After a Then, a new Given or When is not allowed.

Report problems with the (1-based) line number counted over all input lines, in this form, stopping at the first problem:

  • Line k: unknown keyword — the line does not start with a valid keyword
  • Line k: And/But cannot start a scenario
  • Line k: Given after When / Line k: Given after Then / Line k: When after Then
  • Line k: Then before When

If no line is wrong, check the end: no When → Missing When; no Then → Missing Then. If everything is fine print VALID (g Given, w When, t Then, a And/But).

Input:

Scenario: matching
Given Ana is in CPS 4301
And Ben is in CPS 4301
When Ana opens Find partners
Then Ben is listed

Output:

VALID (1 Given, 1 When, 1 Then, 1 And/But)

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.