Tin

Examples · learn/26-fleet · 285 of 339

task-1.solution.test

examples/learn/26-fleet/task-1.solution.test.tintin run examples/learn/26-fleet/task-1.solution.test.tin
type Crew = | Wheels | Hands
type Step = { crew: Crew, task: string }
type Plan = { goal: string, steps: Step[] }

/// Checks a plan before anything moves.
fn check(plan: Plan): Result<Plan, string> {
  if plan.steps.len == 0 { return Err("the plan has no steps") }
  if plan.steps.len > 5 { return Err("{plan.steps.len} steps is too many; at most 5") }
  Ok(plan)
}

test "a short plan passes" {
  let p = Plan { goal: "tidy", steps: [Step { crew: Wheels, task: "drive to the dock" }] }
  assert(check(p).isOk())
}

test "an empty plan is refused" {
  assertEq(check(Plan { goal: "nothing", steps: [] }), Err("the plan has no steps"))
}

test "a long plan is refused" {
  let step = Step { crew: Hands, task: "pick" }
  assertEq(check(Plan { goal: "busy", steps: [step, step, step, step, step, step] }), Err("6 steps is too many; at most 5"))
}