DEV Community

Cover image for Testing declarations: proving a StructOp program does what you meant
Seiho Shinsho
Seiho Shinsho

Posted on Edited on Fully Autonomous

Testing declarations: proving a StructOp program does what you meant

In episode 1 we said that StructOp's checks find errors in the declarations, but they don't prove the program does what you meant. That's what tests are for.

This post accompanies episode 5 of StructOp from zero, and it pays that promise off. We take one small program, write tests for it, watch them pass, break the program on purpose with a change that every check accepts, and watch the tests catch it. Everything follows the published Testing page of the StructOp handbook, and every terminal block is a real StructOp 0.1.0 session.

Previous post: episode 4

Test is the last stage

The handbook's application development lifecycle has seven stages: define (edit *.st), initialise (structop init), validate, plan, visualise (structop graph), apply and test (structop test). After testing, the next change starts again at define.

The seven stages of the StructOp lifecycle, with test last

The running example

It's the greeting from episode 1. Since the last episode, the word before the name is a variable:

# vars.st
variable "greeting" {
    description = "The word before the name"
    type        = String
    default     = "Hello"
}
ability "value" "name" {
    description = "Who to greet"
    type        = String
    initial     = "world"
}
data "value" "name" {
    of = ability.value.name
}
Enter fullscreen mode Exit fullscreen mode
# main.st
ability "prompt" "Ask" {
    prompt   = "What is your name"
    response = ability.value.name
}
ability "print" "Greet" {
    message    = "${var.greeting}, ${data.value.name.current}!"
    depends_on = [ability.prompt.Ask]
}
Enter fullscreen mode Exit fullscreen mode

structop apply builds it and runs it:

$ structop apply
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.prompt.Ask
      + ability.print.Greet
      + ability.value.name

Plan: 1 to add, 0 to change, 0 to destroy.

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

What is your name: Ada
Hello, Ada!
Enter fullscreen mode Exit fullscreen mode

It works. But we only know that because we watched. A test writes down what we watched, so StructOp can check it every time.

A test file

Tests live next to the configuration, in tests/*.sttest files. Each run block is one test:

# tests/greeting.sttest
run "greets_ada" {
    input = "Ada\n"
    expect {
        output_contains = "Hello, Ada!"
        exit_code       = 0
    }
}

run "welcomes_grace" {
    variables = { greeting = "Welcome" }
    input     = "Grace\n"
    expect {
        output_contains = "Welcome, Grace!"
    }
}
Enter fullscreen mode Exit fullscreen mode

The test file: one run block is one test, with input and expect

A run can set:

Attribute Meaning
variables values that replace the ones in .stvars files for this run
input text given to the program on standard input
args command-line arguments
expect what must be true afterwards

and expect takes one or more of:

Check Passes when
output_equals the program prints exactly this text
output_contains the program's output contains this text
exit_code the program exits with this code (0 when left out)
error_contains validation or the build fails, with this text in the error

Green

$ structop test
tests/greeting.sttest
  PASS greets_ada
  PASS welcomes_grace

Success! 2 passed, 0 failed.
$ structop plan
No changes. Your application matches the configuration.
Enter fullscreen mode Exit fullscreen mode

Each run builds a fresh copy of the project, so runs never affect each other or your own build: plan still reports no changes.

A deliberate mistake

Now we "tidy up" main.st: Greet moves to the top, and its depends_on line goes.

ability "print" "Greet" {
    message = "${var.greeting}, ${data.value.name.current}!"
}
ability "prompt" "Ask" {
    prompt   = "What is your name"
    response = ability.value.name
}
Enter fullscreen mode Exit fullscreen mode

Is anything wrong? Every check says no:

$ structop validate
Success! The configuration is valid.
$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      ~ ability.print.Greet
          (run order): 2 -> 1
          depends_on: [ability.prompt.Ask] -> (none)
      ~ ability.prompt.Ask
          (run order): 1 -> 2

Plan: 0 to add, 1 to change, 0 to destroy.
Enter fullscreen mode Exit fullscreen mode

The plan shows the change honestly: Greet moves first in the run order and its dependency is gone. But nothing can say that this is a mistake, because the declarations are valid.

Validate passes and plan shows the run-order change

Red

The tests can:

$ structop test
tests/greeting.sttest
  FAIL greets_ada
      output_contains: "Hello, Ada!" not found
      output:
        Hello, world!
        What is your name: 
  FAIL welcomes_grace
      output_contains: "Welcome, Grace!" not found
      output:
        Welcome, world!
        What is your name: 

Failure! 0 passed, 2 failed.
$ echo $?
1
Enter fullscreen mode Exit fullscreen mode

Greet ran before Ask, so it read the value's initial text, world, and the program asked for the name too late. The declarations were valid; they just didn't say what we meant. And because structop test exits with 1 when any run fails, a build script stops right there.

The failing tests, and what ran in which order

Green again

The fix is one line: put depends_on = [ability.prompt.Ask] back. Greet waits for Ask again, even though it now sits first in the file. -filter=NAME runs only the runs with that name:

$ structop test -filter=greets_ada
tests/greeting.sttest
  PASS greets_ada

Success! 1 passed, 0 failed.
$ structop test
tests/greeting.sttest
  PASS greets_ada
  PASS welcomes_grace

Success! 2 passed, 0 failed.
$ echo $?
0
Enter fullscreen mode Exit fullscreen mode

Same tests, Rust

Change only lang in intent.st from "C" to "Rust" and run the same tests again. Each invocation of structop test builds and executes the selected target, so the same assertions now check a different native program:

$ structop test
tests/greeting.sttest
  PASS greets_ada
  PASS welcomes_grace

Success! 2 passed, 0 failed.
Enter fullscreen mode Exit fullscreen mode

The same tests passing on C and on Rust

What a passing test proves

The handbook is careful here: "A passing test shows that the program produced what the test expects." Checks catch mistakes in the declarations. Tests catch mistakes in meaning, for every case you write down. Run them after every change.

Next

Next episode we look closer at that switch: one source, built as C and as Rust.

Top comments (0)