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)

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

s3-2-data-1.txt

Scenario: matching
Given Ana is in CPS 4301
And Ben is in CPS 4301
When Ana opens Find partners
Then Ben is listed
What your program must print for s3-2-data-1.txt
VALID (1 Given, 1 When, 1 Then, 1 And/But)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < s3-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content s3-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.