Chapter 34: the seams that make a game testable, built

The notifier example showed the seam; this one shows what it buys. A guessing game gets its secret and its player from components, so the live program asks the operating system and the keyboard while a test supplies a fixed number and a script. The lesson builds the game and its live wiring in one file, then a second file of test doubles, three test wirings and eight tests. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-guess-seam.

The EK9 IDE as the lesson opens

What you'll build

The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.

guess-seam/GuessSeam.ek9

#!ek9
defines module book.di.seam

  defines constant
    HIGHEST <- 100

    MAX_TRIES <- 7

  defines class

    GuessGame
      secrets as SecretSource!
      player as Player!

      play()
        <- rtn as String?
        secret <- secrets.secret()
        if not onTheBoard(secret)
          throw Exception(`the secret ${secret} is not from 1 to ${HIGHEST}`)
        tries <- 0
        solved <- false
        while triesLeft(tries) and not solved
          tries++
          if guess <- player.nextGuess()
            solved := guess == secret
            Stdout().println(hint(secret, guess))
        rtn: verdict(solved, tries)

      default operator ?

  defines function

    hint() as pure
      ->
        secret as Integer
        guess as Integer
      <- rtn as String: String()

      order <- guess <=> secret
      rtn: order < 0 <- "higher" : order > 0 <- "lower" : "correct"

    onTheBoard() as pure
      -> secret as Integer
      <- rtn as Boolean: secret >= 1 and secret <= HIGHEST

    triesLeft() as pure
      -> tries as Integer
      <- rtn as Boolean: tries < MAX_TRIES

    verdict() as pure
      ->
        solved as Boolean
        tries as Integer
      <- rtn as String: String()

      rtn: solved <- `solved in ${tries} tries` : `not solved in ${tries} tries`

  defines component

    SecretSource as abstract
      secret() as abstract
        <- rtn as Integer?

    Player as abstract
      nextGuess() as abstract
        <- rtn as Integer?

    RandomSecret is SecretSource
      override secret()
        <- rtn as Integer: OS().random(1, HIGHEST)

    KeyboardPlayer is Player
      override nextGuess()
        <- rtn as Integer: Integer(Stdin().next())

  defines application

    liveGame
      register RandomSecret() as SecretSource
      register KeyboardPlayer() as Player

  defines program

    Play() with application of liveGame
      game <- GuessGame()
      Stdout().println(game.play())

guess-seam/dev/GuessSeamTests.ek9

#!ek9
defines module book.di.seam.tests

  references
    book.di.seam::GuessGame
    book.di.seam::HIGHEST
    book.di.seam::hint
    book.di.seam::Player
    book.di.seam::RandomSecret
    book.di.seam::SecretSource
    book.di.seam::triesLeft
    book.di.seam::verdict

  defines component

    FixedSecret is SecretSource
      override secret()
        <- rtn as Integer: 37

    ScriptedPlayer is Player
      guesses as Iterator of Integer?

      ScriptedPlayer()
        script <- [50, 25, 37]
        guesses: script.iterator()

      override nextGuess()
        <- rtn as Integer: Integer()
        if guesses.hasNext()
          rtn: guesses.next()

      default operator ?

    HopelessPlayer is Player
      override nextGuess()
        <- rtn as Integer: 1

    OffTheBoardSecret is SecretSource
      override secret()
        <- rtn as Integer: 0

  defines application

    scriptedGame
      register FixedSecret() as SecretSource
      register ScriptedPlayer() as Player

    hopelessGame
      register FixedSecret() as SecretSource
      register HopelessPlayer() as Player

    brokenGame
      register OffTheBoardSecret() as SecretSource
      register HopelessPlayer() as Player

  defines program

    @Test
    ThirdGuessWins() with application of scriptedGame
      game <- GuessGame()
      assert game.play() == "solved in 3 tries"

    @Test
    RunsOutOfTries() with application of hopelessGame
      game <- GuessGame()
      assert game.play() == "not solved in 7 tries"

    @Test
    ASecretOffTheBoardIsRefused() with application of brokenGame
      game <- GuessGame()
      assertThrows (Exception, game.play())

    @Test
    HintsPointTheRightWay()
      assert hint(37, 50) == "lower"
      assert hint(37, 25) == "higher"
      assert hint(37, 37) == "correct"

    @Test
    VerdictCountsTheTries()
      assert verdict(true, 3) == "solved in 3 tries"
      assert verdict(false, 7) == "not solved in 7 tries"

    @Test
    TheLastTryIsTheSeventh()
      assert triesLeft(6)
      assert not triesLeft(7)

    @Test
    LiveSecretStaysOnTheBoard()
      secret <- RandomSecret().secret()
      assert secret >= 1 and secret <= HIGHEST
      assert RandomSecret()?

    @Test
    GameIsSet() with application of scriptedGame
      assert GuessGame()?

Step 1: Constants

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

HIGHEST is the constant's name and 100 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: HIGHEST is the constant's name and 100 its value - a literal in the Value field, from which the type is inferred

The preview shows HIGHEST about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows HIGHEST about to be written

Choose Code > New Construct > Constant…

The EK9 IDE once this is done: Choose Code > New Construct > Constant…

MAX_TRIES is the constant's name and 7 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: MAX_TRIES is the constant's name and 7 its value - a literal in the Value field, from which the type is inferred

The preview shows MAX_TRIES about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows MAX_TRIES about to be written

Step 2: hint — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

hint is the function's name. Add the parameters secret of type Integer and guess of type Integer, set Returns to String and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: hint is the function's name

The preview shows hint about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows hint about to be written

Select the hint() as pure line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the hint() as pure line and, above it, type the blank line the book opens its first function with

Select the stub, from <- rtn as String? down to its //AI line, and type hint's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type hint's own return and work, as the book writes them

Step 3: onTheBoard — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

onTheBoard is the function's name. Add one parameter, secret of type Integer, set Returns to Boolean and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: onTheBoard is the function's name

The preview shows onTheBoard about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows onTheBoard about to be written

Select the stub, from <- rtn as Boolean? down to its //AI line, and type onTheBoard's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Boolean? down to its //AI line, and type onTheBoard's own return and work, as the book writes them

Step 4: triesLeft — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

triesLeft is the function's name. Add one parameter, tries of type Integer, set Returns to Boolean and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: triesLeft is the function's name

The preview shows triesLeft about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows triesLeft about to be written

Select the stub, from <- rtn as Boolean? down to its //AI line, and type triesLeft's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Boolean? down to its //AI line, and type triesLeft's own return and work, as the book writes them

Step 5: verdict — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: Choose Code > New Construct > Function…

verdict is the function's name. Add the parameters solved of type Boolean and tries of type Integer, set Returns to String and tick pure. Choose Preview.

The EK9 IDE just before choosing Preview: verdict is the function's name

The preview shows verdict about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows verdict about to be written

Select the stub, from <- rtn as String? down to its //AI line, and type verdict's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as String? down to its //AI line, and type verdict's own return and work, as the book writes them

Step 6: SecretSource — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

SecretSource is the component's name. The secret is a seam: where the number comes from is a component, so a test can fix it. Choose Abstract under Extensibility. Choose Preview.

The EK9 IDE just before choosing Preview: SecretSource is the component's name

The preview shows SecretSource about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows SecretSource about to be written

Select the SecretSource line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the SecretSource line and, above it, type the blank line the book opens the block with

Right-click the SecretSource line and choose Add Method…

The EK9 IDE once this is done: Right-click the SecretSource line and choose Add Method…

secret is the method's name. Set Return type to Integer and tick abstract - a contract with no body. Choose Apply.

The EK9 IDE just before choosing Apply: secret is the method's name

Step 7: Player — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

Player is the component's name. The player is the second seam: a keyboard in production, a script in a test. Choose Abstract under Extensibility. Choose Preview.

The EK9 IDE just before choosing Preview: Player is the component's name

The preview shows Player about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Player about to be written

Right-click the Player line and choose Add Method…

The EK9 IDE once this is done: Right-click the Player line and choose Add Method…

nextGuess is the method's name. Set Return type to Integer and tick abstract - a contract with no body. Choose Apply.

The EK9 IDE just before choosing Apply: nextGuess is the method's name

Step 8: RandomSecret — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

RandomSecret is the component's name. It is the live secret. Type SecretSource into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: RandomSecret is the component's name

The preview shows RandomSecret about to be written - and, if SecretSource owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows RandomSecret about to be written - and, if SecretSource owes any methods, an override stub for each

Select the stub, from <- rtn as Integer: 0 down to its //AI line, and type the live answer: a random number from the operating system. Saves when typed.

The EK9 IDE once this is done: Select the stub, from <- rtn as Integer

Step 9: KeyboardPlayer — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

KeyboardPlayer is the component's name. It is the live player. Type Player into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: KeyboardPlayer is the component's name

The preview shows KeyboardPlayer about to be written - and, if Player owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows KeyboardPlayer about to be written - and, if Player owes any methods, an override stub for each

Select the stub and type the live answer: the next word typed, read as an Integer. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the live answer

Step 10: GuessGame — a class

Choose Code > New Construct > Class…

The EK9 IDE once this is done: Choose Code > New Construct > Class…

GuessGame is the class's name. It is the game itself. It asks for its secret and its player as components. Choose Preview.

The EK9 IDE just before choosing Preview: GuessGame is the class's name

The preview shows GuessGame about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows GuessGame about to be written

Select the GuessGame line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the GuessGame line and, above it, type the blank line the book opens the block with

Right-click the GuessGame line and choose Add Field…

The EK9 IDE once this is done: Right-click the GuessGame line and choose Add Field…

Type secrets as SecretSource!. The ! makes it an injection point: the application a program runs under supplies it. Choose OK.

The EK9 IDE just before choosing OK: Type secrets as SecretSource!

The preview shows secrets added - and default operator ? with it, GuessGame's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows secrets added - and default operator ? with it, GuessGame's first field

Right-click the GuessGame line and choose Add Field…

The EK9 IDE once this is done: Right-click the GuessGame line and choose Add Field…

Type player as Player!. The second injection point. Choose OK.

The EK9 IDE just before choosing OK: Type player as Player!

The preview shows player added. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows player added

Right-click the GuessGame line and choose Add Method…

The EK9 IDE once this is done: Right-click the GuessGame line and choose Add Method…

play is the method's name. It answers with the verdict. Set Return type to String. Choose Apply.

The EK9 IDE just before choosing Apply: play is the method's name

Select the stub and type the game: ask for the secret, refuse one off the board, then loop while tries remain and it is not solved. Everything it needs comes from the two components and the small pure functions. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the game

Step 11: liveGame — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

liveGame is the application's name. It is the live wiring: a random secret and a player at the keyboard. The Registrations table holds RandomSecret as SecretSource and KeyboardPlayer as Player. Choose Preview.

The EK9 IDE just before choosing Preview: liveGame is the application's name

The preview shows liveGame about to be written, with a register line for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows liveGame about to be written, with a register line for each

Select the liveGame line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the liveGame line and, above it, type the blank line the book opens the block with

Step 12: Play — a program

Choose Code > New Construct > Program…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

Play is the program's name. It runs the game under the live wiring. Type liveGame into Application. Choose Preview.

The EK9 IDE just before choosing Preview: Play is the program's name

The preview shows Play about to be written, with application of liveGame. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows Play about to be written, with application of liveGame

Select the Play line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the Play line and, above it, type the blank line the book opens the block with

Select the stub and type the body: a GuessGame and its verdict. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the body

Step 13: guess-seam/dev/GuessSeamTests.ek9

Open guess-seam/dev/GuessSeamTests.ek9 in the Project panel - it is already there, with just its module line.

The EK9 IDE once this is done: Open guess-seam/dev/GuessSeamTests.ek9 in the Project panel - it is already there, with just its module line

A test in another module names what it uses. Below the module line, type the references block - the game, the constant, the three functions and the two contracts - in alphabetical order. Saves when typed.

The EK9 IDE once this is done: A test in another module names what it uses

Step 14: FixedSecret — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

FixedSecret is the component's name. It is a test double: the same contract, a number that never changes. Type SecretSource into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: FixedSecret is the component's name

The preview shows FixedSecret about to be written - and, if SecretSource owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows FixedSecret about to be written - and, if SecretSource owes any methods, an override stub for each

Select the FixedSecret line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the FixedSecret line and, above it, type the blank line the book opens the block with

Select the stub and type the fixed answer, 37. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the fixed answer, 37

Step 15: ScriptedPlayer — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

ScriptedPlayer is the component's name. It plays a fixed script of guesses. Type Player into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: ScriptedPlayer is the component's name

The preview shows ScriptedPlayer about to be written - and, if Player owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ScriptedPlayer about to be written - and, if Player owes any methods, an override stub for each

No tool writes a constructor with a body, so type one under the header: the script, and the iterator over it that the next step declares. Saves when typed.

The EK9 IDE once this is done: No tool writes a constructor with a body, so type one under the header

Right-click the ScriptedPlayer line and choose Add Field…

The EK9 IDE once this is done: Right-click the ScriptedPlayer line and choose Add Field…

Type guesses as Iterator of Integer?. Iterator is abstract, so the field cannot start with a value; the constructor above assigns it, which is what lets the compiler accept it unset. Untick 'Take it in a new constructor' - the constructor is the book's own. Choose OK.

The EK9 IDE just before choosing OK: Type guesses as Iterator of Integer?

The preview shows guesses added - and default operator ? with it, ScriptedPlayer's first field. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows guesses added - and default operator ? with it, ScriptedPlayer's first field

Select the override's stub and type it: the next scripted guess while there is one. Saves when typed.

The EK9 IDE once this is done: Select the override's stub and type it

Step 16: HopelessPlayer — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

HopelessPlayer is the component's name. It always guesses 1. Type Player into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: HopelessPlayer is the component's name

The preview shows HopelessPlayer about to be written - and, if Player owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows HopelessPlayer about to be written - and, if Player owes any methods, an override stub for each

Select the stub and type the answer, 1. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the answer, 1

Step 17: OffTheBoardSecret — a component

Choose Code > New Construct > Component…

The EK9 IDE once this is done: Choose Code > New Construct > Component…

OffTheBoardSecret is the component's name. It is a secret the game must refuse. Type SecretSource into Extends. Choose Preview.

The EK9 IDE just before choosing Preview: OffTheBoardSecret is the component's name

The preview shows OffTheBoardSecret about to be written - and, if SecretSource owes any methods, an override stub for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows OffTheBoardSecret about to be written - and, if SecretSource owes any methods, an override stub for each

Select the stub and type the answer, 0 - not on the board. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the answer, 0 - not on the board

Step 18: scriptedGame — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

scriptedGame is the application's name. It wires the fixed secret to the scripted player. The Registrations table holds FixedSecret as SecretSource and ScriptedPlayer as Player. Choose Preview.

The EK9 IDE just before choosing Preview: scriptedGame is the application's name

The preview shows scriptedGame about to be written, with a register line for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows scriptedGame about to be written, with a register line for each

Select the scriptedGame line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the scriptedGame line and, above it, type the blank line the book opens the block with

Step 19: hopelessGame — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

hopelessGame is the application's name. It wires the fixed secret to the player who never improves. The Registrations table holds FixedSecret as SecretSource and HopelessPlayer as Player. Choose Preview.

The EK9 IDE just before choosing Preview: hopelessGame is the application's name

The preview shows hopelessGame about to be written, with a register line for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows hopelessGame about to be written, with a register line for each

Step 20: brokenGame — an application

Choose Code > New Construct > Application…

The EK9 IDE once this is done: Choose Code > New Construct > Application…

brokenGame is the application's name. It wires the impossible secret. The Registrations table holds OffTheBoardSecret as SecretSource and HopelessPlayer as Player. Choose Preview.

The EK9 IDE just before choosing Preview: brokenGame is the application's name

The preview shows brokenGame about to be written, with a register line for each. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows brokenGame about to be written, with a register line for each

Step 21: ThirdGuessWins — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

ThirdGuessWins is the test's name. Type scriptedGame into Application. Choose Preview.

The EK9 IDE just before choosing Preview: ThirdGuessWins is the test's name

The preview shows ThirdGuessWins about to be written, marked @Test, with application of scriptedGame and a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ThirdGuessWins about to be written, marked @Test, with application of scriptedGame and a placeholder assertion

Select the first @Test line and, above it, type the blank line the book opens the block with. Saves when typed.

The EK9 IDE once this is done: Select the first @Test line and, above it, type the blank line the book opens the block with

Select the stub and type the claim: the scripted player wins on the third guess. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the claim

Step 22: RunsOutOfTries — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

RunsOutOfTries is the test's name. Type hopelessGame into Application. Choose Preview.

The EK9 IDE just before choosing Preview: RunsOutOfTries is the test's name

The preview shows RunsOutOfTries about to be written, marked @Test, with application of hopelessGame and a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows RunsOutOfTries about to be written, marked @Test, with application of hopelessGame and a placeholder assertion

Select the stub and type the claim. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the claim

Step 23: ASecretOffTheBoardIsRefused — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

ASecretOffTheBoardIsRefused is the test's name. Type brokenGame into Application. Choose Preview.

The EK9 IDE just before choosing Preview: ASecretOffTheBoardIsRefused is the test's name

The preview shows ASecretOffTheBoardIsRefused about to be written, marked @Test, with application of brokenGame and a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ASecretOffTheBoardIsRefused about to be written, marked @Test, with application of brokenGame and a placeholder assertion

Select the stub and type the claim: playing with that secret throws. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the claim

Step 24: HintsPointTheRightWay — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

HintsPointTheRightWay is the test's name. It needs no application: hint is a pure function. Leave Application empty. Choose Preview.

The EK9 IDE just before choosing Preview: HintsPointTheRightWay is the test's name

The preview shows HintsPointTheRightWay about to be written, marked @Test, with a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows HintsPointTheRightWay about to be written, marked @Test, with a placeholder assertion

Select the stub and type the three claims. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the three claims

Step 25: VerdictCountsTheTries — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

VerdictCountsTheTries is the test's name. It tests the other pure function. Choose Preview.

The EK9 IDE just before choosing Preview: VerdictCountsTheTries is the test's name

The preview shows VerdictCountsTheTries about to be written, marked @Test, with a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows VerdictCountsTheTries about to be written, marked @Test, with a placeholder assertion

Select the stub and type the two claims. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the two claims

Step 26: TheLastTryIsTheSeventh — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

TheLastTryIsTheSeventh is the test's name. It pins the limit. Choose Preview.

The EK9 IDE just before choosing Preview: TheLastTryIsTheSeventh is the test's name

The preview shows TheLastTryIsTheSeventh about to be written, marked @Test, with a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows TheLastTryIsTheSeventh about to be written, marked @Test, with a placeholder assertion

Select the stub and type the two claims. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the two claims

Step 27: LiveSecretStaysOnTheBoard — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

LiveSecretStaysOnTheBoard is the test's name. It tests the live component directly, with no application: a component can be constructed like any class. Choose Preview.

The EK9 IDE just before choosing Preview: LiveSecretStaysOnTheBoard is the test's name

The preview shows LiveSecretStaysOnTheBoard about to be written, marked @Test, with a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows LiveSecretStaysOnTheBoard about to be written, marked @Test, with a placeholder assertion

Select the stub and type the claims. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the claims

Step 28: GameIsSet — a test

Choose Code > New Construct > Test…

The EK9 IDE once this is done: Choose Code > New Construct > Test…

GameIsSet is the test's name. Type scriptedGame into Application. Choose Preview.

The EK9 IDE just before choosing Preview: GameIsSet is the test's name

The preview shows GameIsSet about to be written, marked @Test, with application of scriptedGame and a placeholder assertion. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows GameIsSet about to be written, marked @Test, with application of scriptedGame and a placeholder assertion

Select the stub and type the claim. Saves when typed.

The EK9 IDE once this is done: Select the stub and type the claim

Step 29: Run it

Run ▸ Run Tests (⌘T) runs every test in the project. Choose Run Tests.

The EK9 IDE once this is done: Run ▸ Run Tests (⌘T) runs every test in the project

All eight pass: the same GuessGame, a different Application each time.

The EK9 IDE once this is done: All eight pass

The result

GuessSeam.ek9 and dev/GuessSeamTests.ek9 now read exactly as chapter 34 of EK9 in Depth shows them. The game never changed between live and test: only the Application did. Run Tests runs all eight.

Where this lesson comes from