Skip to content
mutastic

Swift edition

Getting started

This runs mutastic on samples/pricing, a small Swift package in mutastic’s repository whose tests pass and reach every line, and still miss something. Then it closes the gap and proves it.

The Swift edition isn’t released yet, so it’s built from its source. You need Swift 6.0 or later (on macOS, Xcode 16 or later), rustup and just:

Terminal window
git clone https://github.com/mutastic/mutastic-swift.git
cd mutastic-swift
just install

That builds it and puts it in ~/.mutastic/swift-1.5.0/. From 1.5.0, mutastic, the command the other editions install, runs it in any Swift package or Xcode project, and fetches it the first time. Until then, run it by its own name: these pages write mutastic, as the commands it offers do, and ~/.mutastic/swift-1.5.0/mutastic-swift takes the same words. Installing mutastic has the details.

From the repository’s root:

Terminal window
mutastic run samples/pricing

mutastic copies the package into its .build/mutastic, writes every mutant into the copy behind a switch, and builds it once. It runs each test once to see what it reaches, then runs each mutant against the tests that reach it. One mutant gets past them:

Terminal window
Missed ────────────────────────────────────────────────────────────
❯ Sources/Pricing/Pricing.swift 1 missed in 1 function
✗ MISSED no test checks discount() at total == 100
┌─ Sources/Pricing/Pricing.swift:12
│
11 │ public func discount(total: Int) -> Int {
12 │ if total >= 100 {
│ ^^ >= changed to >
13 │ return total / 10
│
= mutant - if total >= 100 {
+ if total > 100 {
= operator mutastic/boundary · https://docs.mutastic.io/swift/operators/conditions/#boundary
= note: every test calls discount() away from the boundary
inputs seen: 50, 250 (none is 100)
covered by PricingTests.bigOrdersGetTenPercentOff(), PricingTests.smallOrdersPayFullPrice() (all 2 covering tests still pass)
= help: test discount() with total = 100 and assert the result
// in Tests/PricingTests/PricingTests.swift
@Test func discountAtTheTotalEdge() {
#expect(Pricing().discount(total: 100) == 10) // today 10 · with this mutant 0
}
check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m86f9a31d samples/pricing passes
reproduce mutastic run --only=m86f9a31d samples/pricing
or ignore mutastic ignore m86f9a31d --reason="<why>" samples/pricing
Next: mutastic verify <id> samples/pricing · mutastic run --only=<id> samples/pricing · mutastic explain <id> samples/pricing
Files 0 clean | 1 with misses
Mutants ✓ 10 caught · ✗ 1 missed (11)
Worst at Pricing.discount() 1 ✗
Build one build of every mutant, 1 round (build)
Test runs 14 (33 without coverage selection)
Start at 22:28:54
Duration 7.3s (setup 35ms, build 7.1s, coverage 22ms, mutants 115ms, checking 22ms, reports 23ms)
Tests 3 on swift 6.3.3 · 4 workers
Reports samples/pricing/.build/mutastic/mutation.json · samples/pricing/.build/mutastic/mutation.html
Score 90.9% PASS 1 missed, above the threshold of 80.

On your own package, run mutastic run inside it, or mutastic run path/to/package.

Each line with a missed mutant gets a card.

  • The gap, first: no test checks discount() at total == 100, so nothing tells >= from >.
  • The change that got through, under the code, and again as a diff.
  • The operator that made it, with a link to its section in Operators.
  • The evidence: the tests that reach the line, which all still pass with the change in.
  • What to do: test discount() with total = 100, and assert the result. The test to add is written out, discountAtTheTotalEdge(), with what the code gives today: 10, where the mutant gives 0.
  • The commands: done when proves the fix, and reproduce runs just this mutant again. Each names this package, so it works as it’s copied. m86f9a31d is the mutant’s id; it stays the same while the code around it moves.

The summary says how the rest went. The score is high enough to pass, but the run still shows the one it missed: a passing score isn’t a reason to leave a gap you can close.

discount(total: 100) gives 10 today. Is that right? The doc comment says 10% off orders of 100 or more, so yes. Add a test that says so to samples/pricing/Tests/PricingTests/PricingTests.swift:

@Test func discountAtTheTotalEdge() {
#expect(Pricing().discount(total: 100) == 10)
}

With > in place of >=, discount(total: 100) is 0, and this test fails. When today’s value isn’t the one the code should give, keep the test and write the right value: then it’s the code that needs fixing.

Run the command the card offered as done when:

Terminal window
mutastic verify m86f9a31d samples/pricing
Terminal window
mutastic: m86f9a31d is caught by PricingTests.discountAtTheTotalEdge()

The mutant is caught: a test now fails when >= becomes >. verify exits with 0 when every mutant it names is caught, and 1 when one isn’t, so a script or a coding agent can use it as the check that a gap is closed.

Terminal window
mutastic run samples/pricing
Terminal window
Files 1 clean
Mutants ✓ 11 caught (11)
Build one build of every mutant, 1 round (build)
Test runs 19 (44 without coverage selection)
Start at 22:29:05
Duration 1.3s (setup 34ms, build 988ms, coverage 23ms, mutants 135ms, reports 24ms)
Tests 4 on swift 6.3.3 · 4 workers
Reports samples/pricing/.build/mutastic/mutation.json · samples/pricing/.build/mutastic/mutation.html
Score 100.0% PASS Every mutant was caught. ▲ 9.1 vs last run (code changed)

Every mutant is caught.