Working test-first: the refactor
A refactor is the step where a green suite tells you least. The code takes a new shape and the tests still pass, but passing says they ran the new code, not that they check it. Most test-first guides name the step and move on. This one does it.
Two more cycles grow the rule from Working test-first until the code says the same thing three times. Then the refactor, with mutastic before it and after it, so you know the tests still pin every behaviour of the new shape, not only that they pass. Working test-first, under Concepts, says why mutastic comes where it does.
The spec, a tier at a time: loyal customers get 5 off orders of 30 or more, 10 off orders of 100 or more, and 20 off orders of 250 or more.
Before you start
Section titled “Before you start”Start where Working test-first ends: loyaltyCredit in the pricing sample,
with its three tests, and that cycle committed. Each cycle here ends with a commit too:
--since=HEAD runs the mutants on the lines changed since the last one.
10 off from 100
Section titled “10 off from 100”- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
A test for the new tier:
// 10 off orders of 100 or more.@Testvoid loyalCustomersGetTenOffFrom100() { assertEquals(10, new Pricing().loyaltyCredit(150));}At the end of the tests module:
// 10 off orders of 100 or more.#[test]fn loyal_customers_get_ten_off_from_100() { assert_eq!(Pricing::new().loyalty_credit(150), 10);}At the end of samples/pricing/Tests/PricingTests/PricingTests.swift:
// 10 off orders of 100 or more.@Test func loyalCustomersGetTenOffFrom100() { #expect(Pricing().loyaltyCredit(total: 150) == 10)}It fails, as it should: the code gives 5.
mutastic run samples/pricingmutastic: com.example.pricing.PricingTest#loyalCustomersGetTenOffFrom100 fails without any mutant, so mutastic can't judge one.It failed: expected: <10> but was: <5>To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests=PricingTest#loyalCustomersGetTenOffFrom100Its output is in samples/pricing/build/mutastic/worker.log. To run it alone: samples/pricing/gradlew -p samples/pricing test --tests 'com.example.pricing.PricingTest.loyalCustomersGetTenOffFrom100'mutastic run fixtures/pricingmutastic: tests::loyal_customers_get_ten_off_from_100 fails without any mutant, so mutastic can't judge one.It panicked: assertion `left == right` failed: left: 5, right: 10mutastic runs each test alone: a test that passes with the others, but fails alone, leans on one of them.To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests=tests::loyal_customers_get_ten_off_from_100Its output is in fixtures/pricing/target/mutastic/tests.log. To run it alone: cargo test --manifest-path fixtures/pricing/Cargo.toml -p pricing --lib -- --exact tests::loyal_customers_get_ten_off_from_100mutastic run samples/pricingmutastic: PricingTests.loyalCustomersGetTenOffFrom100() fails without any mutant, so mutastic can't judge one.It failed: Expectation failed: (Pricing().loyaltyCredit(total: 150) → 5) == 10To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests='PricingTests.loyalCustomersGetTenOffFrom100()'Its output is in samples/pricing/.build/mutastic/tests.log. To run it alone: swift test --package-path samples/pricing --filter 'PricingTests\.loyalCustomersGetTenOffFrom100\(\)'- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
The least code that passes is a second if, above the first, as the higher tier wins. In place of
the method:
/// For loyal customers: 5 off orders of 30 or more, and 10 off from 100.public int loyaltyCredit(int total) { if (total >= 100) { return 10; } if (total >= 30) { return 5; } return 0;}/// For loyal customers: 5 off orders of 30 or more, and 10 off from 100.pub fn loyalty_credit(&self, total: u32) -> u32 { if total >= 100 { return 10; } if total >= 30 { return 5; } 0}/// For loyal customers: 5 off orders of 30 or more, and 10 off from 100.public func loyaltyCredit(total: Int) -> Int { if total >= 100 { return 10 } if total >= 30 { return 5 } return 0}The whole suite passes.
- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
mutastic on the cycle’s lines:
mutastic run samples/pricing --since=HEAD Missed ────────────────────────────────────────────────────────────
❯ src/main/java/com/example/pricing/Pricing.java 1 missed in 1 method
✗ MISSED no test checks loyaltyCredit() at total == 100 ┌─ src/main/java/com/example/pricing/Pricing.java:33 │ 32 │ public int loyaltyCredit(int total) { 33 │ if (total >= 100) { │ ^^ >= changed to > 34 │ return 10; │ = mutant - if (total >= 100) { + if (total > 100) { = operator mutastic/boundary · https://docs.mutastic.io/java/operators/conditions/#boundary = note: every test calls loyaltyCredit() away from the boundary inputs seen: 29, 30, 50, 150 (none is 100) covered by PricingTest#loyalCustomersGetFiveOff, PricingTest#loyalCustomersGetTenOffFrom100, PricingTest#loyaltyCreditAtTheTotalEdge and 1 more (all 4 covering tests still pass) = help: test loyaltyCredit() with total = 100 and assert the result
// in PricingTest @Test void loyaltyCreditAtTheTotalEdge2() { assertEquals(10, new Pricing().loyaltyCredit(100)); // today 10 · with this mutant 5 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m809c9c9b samples/pricing passes reproduce mutastic run --only=m809c9c9b samples/pricing or ignore mutastic ignore m809c9c9b --reason="<why>" samples/pricing
Next: mutastic verify <id> samples/pricing · mutastic run --only=<id> samples/pricing · mutastic explain <id> samples/pricing
Classes 0 clean | 1 with misses Mutants ✓ 3 caught · ✗ 1 missed (4) Changed 4 lines in 1 file since HEAD Worst at Pricing.loyaltyCredit() 1 ✗ Test runs 8 (28 without coverage selection) Start at 03:35:21 Duration 1.9s (setup 0ms, build 479ms, coverage 239ms, mutants 92ms, checking 1.1s, reports 15ms) Tests 7 on JDK 17 · 1 worker Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.mutastic run fixtures/pricing --since=HEAD Missed ────────────────────────────────────────────────────────────
❯ src/lib.rs 1 missed in 1 function
✗ MISSED no test checks loyalty_credit() at total == 100 ┌─ src/lib.rs:42 │ 41 │ pub fn loyalty_credit(&self, total: u32) -> u32 { 42 │ if total >= 100 { │ ^^ >= changed to > 43 │ return 10; │ = mutant - if total >= 100 { + if total > 100 { = operator mutastic/boundary · https://docs.mutastic.io/rust/operators/conditions/#boundary = note: every test calls loyalty_credit() away from the boundary inputs seen: 29, 30, 50, 150 (none is 100) covered by tests::loyal_customers_get_five_off, tests::loyal_customers_get_ten_off_from_100, tests::loyalty_credit_at_the_total_edge and 1 more (all 4 covering tests still pass) = help: test loyalty_credit() with total = 100 and assert the result
// in mod tests #[test] fn loyalty_credit_at_the_total_edge_2() { assert_eq!(Pricing::new().loyalty_credit(100), 10); // today 10 · with this mutant 5 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m0d64ba6c fixtures/pricing passes reproduce mutastic run --only=m0d64ba6c fixtures/pricing or ignore mutastic ignore m0d64ba6c --reason="<why>" fixtures/pricing
Next: mutastic verify <id> fixtures/pricing · mutastic run --only=<id> fixtures/pricing · mutastic explain <id> fixtures/pricing
Files 0 clean | 1 with misses Mutants ✓ 3 caught · ✗ 1 missed (4) Changed 10 lines in 1 file since HEAD Worst at Pricing::loyalty_credit() 1 ✗ Build one build of every mutant, 2 rounds (check and build) Test runs 13 (28 without coverage selection) Start at 09:44:02 Duration 5.7s (setup 1.6s, build 3.9s, coverage 18ms, mutants 56ms, checking 30ms, reports 12ms) Tests 7 on rustc 1.100.0 · 4 workers Reports fixtures/pricing/target/mutastic/mutation.json · fixtures/pricing/target/mutastic/mutation.html Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.mutastic run samples/pricing --since=HEAD Missed ────────────────────────────────────────────────────────────
❯ Sources/Pricing/Pricing.swift 1 missed in 1 function
✗ MISSED no test checks loyaltyCredit() at total == 100 ┌─ Sources/Pricing/Pricing.swift:28 │ 27 │ public func loyaltyCredit(total: Int) -> Int { 28 │ if total >= 100 { │ ^^ >= changed to > 29 │ return 10 │ = mutant - if total >= 100 { + if total > 100 { = operator mutastic/boundary · https://docs.mutastic.io/swift/operators/conditions/#boundary = note: every test calls loyaltyCredit() away from the boundary inputs seen: 29, 30, 50, 150 (none is 100) covered by PricingTests.loyalCustomersGetFiveOff(), PricingTests.loyalCustomersGetTenOffFrom100(), PricingTests.loyaltyCreditAtTheTotalEdge() and 1 more (all 4 covering tests still pass) = help: test loyaltyCredit() with total = 100 and assert the result
// in Tests/PricingTests/PricingTests.swift @Test func loyaltyCreditAtTheTotalEdge2() { #expect(Pricing().loyaltyCredit(total: 100) == 10) // today 10 · with this mutant 5 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m8a0288b4 samples/pricing passes reproduce mutastic run --only=m8a0288b4 samples/pricing or ignore mutastic ignore m8a0288b4 --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 ✓ 3 caught · ✗ 1 missed (4) Changed 4 lines in 1 file since HEAD Worst at Pricing.loyaltyCredit() 1 ✗ Build one build of every mutant, 1 round (build) Test runs 13 (28 without coverage selection) Start at 22:29:15 Duration 1.1s (setup 30ms, build 897ms, coverage 44ms, mutants 70ms, checking 21ms, reports 24ms) Tests 7 on swift 6.3.3 · 4 workers Reports samples/pricing/.build/mutastic/mutation.json · samples/pricing/.build/mutastic/mutation.html Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.The new test is at 150, so nothing checks the edge at 100. The new lines’ other mutants are caught by the tests there are: the one at 50 wants 5, not 10, and the new one wants 10, not 5 or 0.
- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
The card’s test, with its value checked against the spec: an order of exactly 100 gets 10 off. The
card’s name for it ends in 2, as the edge at 30 has the first name, so name it for its edge:
@Testvoid loyaltyCreditAtThe100Edge() { assertEquals(10, new Pricing().loyaltyCredit(100));}mutastic verify m809c9c9b samples/pricingmutastic: m809c9c9b is caught by com.example.pricing.PricingTest#loyaltyCreditAtThe100Edge#[test]fn loyalty_credit_at_the_100_edge() { assert_eq!(Pricing::new().loyalty_credit(100), 10);}mutastic verify m0d64ba6c fixtures/pricingmutastic: m0d64ba6c is caught by tests::loyalty_credit_at_the_100_edge@Test func loyaltyCreditAtThe100Edge() { #expect(Pricing().loyaltyCredit(total: 100) == 10)}mutastic verify m8a0288b4 samples/pricingmutastic: m8a0288b4 is caught by PricingTests.loyaltyCreditAtThe100Edge()- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
Two ifs are not yet a pattern, so there’s nothing to refactor. Commit.
20 off from 250
Section titled “20 off from 250”- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
The same cycle, a tier up. The test:
// 20 off orders of 250 or more.@Testvoid loyalCustomersGetTwentyOffFrom250() { assertEquals(20, new Pricing().loyaltyCredit(300));}mutastic run samples/pricingmutastic: com.example.pricing.PricingTest#loyalCustomersGetTwentyOffFrom250 fails without any mutant, so mutastic can't judge one.It failed: expected: <20> but was: <10>To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests=PricingTest#loyalCustomersGetTwentyOffFrom250Its output is in samples/pricing/build/mutastic/worker.log. To run it alone: samples/pricing/gradlew -p samples/pricing test --tests 'com.example.pricing.PricingTest.loyalCustomersGetTwentyOffFrom250'// 20 off orders of 250 or more.#[test]fn loyal_customers_get_twenty_off_from_250() { assert_eq!(Pricing::new().loyalty_credit(300), 20);}mutastic run fixtures/pricingmutastic: tests::loyal_customers_get_twenty_off_from_250 fails without any mutant, so mutastic can't judge one.It panicked: assertion `left == right` failed: left: 10, right: 20mutastic runs each test alone: a test that passes with the others, but fails alone, leans on one of them.To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests=tests::loyal_customers_get_twenty_off_from_250Its output is in fixtures/pricing/target/mutastic/tests.log. To run it alone: cargo test --manifest-path fixtures/pricing/Cargo.toml -p pricing --lib -- --exact tests::loyal_customers_get_twenty_off_from_250// 20 off orders of 250 or more.@Test func loyalCustomersGetTwentyOffFrom250() { #expect(Pricing().loyaltyCredit(total: 300) == 20)}mutastic run samples/pricingmutastic: PricingTests.loyalCustomersGetTwentyOffFrom250() fails without any mutant, so mutastic can't judge one.It failed: Expectation failed: (Pricing().loyaltyCredit(total: 300) → 10) == 20To run without it until it's fixed, list it under exclude-tests: in mutastic.yaml with a reason, or for this run add --exclude-tests='PricingTests.loyalCustomersGetTwentyOffFrom250()'Its output is in samples/pricing/.build/mutastic/tests.log. To run it alone: swift test --package-path samples/pricing --filter 'PricingTests\.loyalCustomersGetTwentyOffFrom250\(\)'- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
A third if, above the others:
/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.public int loyaltyCredit(int total) { if (total >= 250) { return 20; } if (total >= 100) { return 10; } if (total >= 30) { return 5; } return 0;}/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.pub fn loyalty_credit(&self, total: u32) -> u32 { if total >= 250 { return 20; } if total >= 100 { return 10; } if total >= 30 { return 5; } 0}/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.public func loyaltyCredit(total: Int) -> Int { if total >= 250 { return 20 } if total >= 100 { return 10 } if total >= 30 { return 5 } return 0}The whole suite passes.
- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
mutastic run samples/pricing --since=HEAD Missed ────────────────────────────────────────────────────────────
❯ src/main/java/com/example/pricing/Pricing.java 1 missed in 1 method
✗ MISSED no test checks loyaltyCredit() at total == 250 ┌─ src/main/java/com/example/pricing/Pricing.java:33 │ 32 │ public int loyaltyCredit(int total) { 33 │ if (total >= 250) { │ ^^ >= changed to > 34 │ return 20; │ = mutant - if (total >= 250) { + if (total > 250) { = operator mutastic/boundary · https://docs.mutastic.io/java/operators/conditions/#boundary = note: every test calls loyaltyCredit() away from the boundary inputs seen: 29, 30, 50, 100, 150, 300 (none is 250) covered by PricingTest#loyalCustomersGetFiveOff, PricingTest#loyalCustomersGetTenOffFrom100, PricingTest#loyalCustomersGetTwentyOffFrom250 and 3 more (all 6 covering tests still pass) = help: test loyaltyCredit() with total = 250 and assert the result
// in PricingTest @Test void loyaltyCreditAtTheTotalEdge2() { assertEquals(20, new Pricing().loyaltyCredit(250)); // today 20 · with this mutant 10 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m809c9c9b samples/pricing passes reproduce mutastic run --only=m809c9c9b samples/pricing or ignore mutastic ignore m809c9c9b --reason="<why>" samples/pricing
Next: mutastic verify <id> samples/pricing · mutastic run --only=<id> samples/pricing · mutastic explain <id> samples/pricing
Classes 0 clean | 1 with misses Mutants ✓ 3 caught · ✗ 1 missed (4) Changed 4 lines in 1 file since HEAD Worst at Pricing.loyaltyCredit() 1 ✗ Test runs 12 (36 without coverage selection) Start at 03:35:28 Duration 1.8s (setup 0ms, build 515ms, coverage 221ms, mutants 75ms, checking 1.0s, reports 7ms) Tests 9 on JDK 17 · 1 worker Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.mutastic run fixtures/pricing --since=HEAD Missed ────────────────────────────────────────────────────────────
❯ src/lib.rs 1 missed in 1 function
✗ MISSED no test checks loyalty_credit() at total == 250 ┌─ src/lib.rs:42 │ 41 │ pub fn loyalty_credit(&self, total: u32) -> u32 { 42 │ if total >= 250 { │ ^^ >= changed to > 43 │ return 20; │ = mutant - if total >= 250 { + if total > 250 { = operator mutastic/boundary · https://docs.mutastic.io/rust/operators/conditions/#boundary = note: every test calls loyalty_credit() away from the boundary inputs seen: 29, 30, 50, 100, 150, 300 (none is 250) covered by tests::loyal_customers_get_five_off, tests::loyal_customers_get_ten_off_from_100, tests::loyal_customers_get_twenty_off_from_250 and 3 more (all 6 covering tests still pass) = help: test loyalty_credit() with total = 250 and assert the result
// in mod tests #[test] fn loyalty_credit_at_the_total_edge_2() { assert_eq!(Pricing::new().loyalty_credit(250), 20); // today 20 · with this mutant 10 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m97fbc9b1 fixtures/pricing passes reproduce mutastic run --only=m97fbc9b1 fixtures/pricing or ignore mutastic ignore m97fbc9b1 --reason="<why>" fixtures/pricing
Next: mutastic verify <id> fixtures/pricing · mutastic run --only=<id> fixtures/pricing · mutastic explain <id> fixtures/pricing
Files 0 clean | 1 with misses Mutants ✓ 3 caught · ✗ 1 missed (4) Changed 10 lines in 1 file since HEAD Worst at Pricing::loyalty_credit() 1 ✗ Build one build of every mutant, 2 rounds (check and build) Test runs 19 (36 without coverage selection) Start at 09:44:22 Duration 5.2s (setup 1.2s, build 3.6s, coverage 66ms, mutants 193ms, checking 23ms, reports 4ms) Tests 9 on rustc 1.100.0 · 4 workers Reports fixtures/pricing/target/mutastic/mutation.json · fixtures/pricing/target/mutastic/mutation.html Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.mutastic run samples/pricing --since=HEAD Missed ────────────────────────────────────────────────────────────
❯ Sources/Pricing/Pricing.swift 1 missed in 1 function
✗ MISSED no test checks loyaltyCredit() at total == 250 ┌─ Sources/Pricing/Pricing.swift:28 │ 27 │ public func loyaltyCredit(total: Int) -> Int { 28 │ if total >= 250 { │ ^^ >= changed to > 29 │ return 20 │ = mutant - if total >= 250 { + if total > 250 { = operator mutastic/boundary · https://docs.mutastic.io/swift/operators/conditions/#boundary = note: every test calls loyaltyCredit() away from the boundary inputs seen: 29, 30, 50, 100, 150, 300 (none is 250) covered by PricingTests.loyalCustomersGetFiveOff(), PricingTests.loyalCustomersGetTenOffFrom100(), PricingTests.loyalCustomersGetTwentyOffFrom250() and 3 more (all 6 covering tests still pass) = help: test loyaltyCredit() with total = 250 and assert the result
// in Tests/PricingTests/PricingTests.swift @Test func loyaltyCreditAtTheTotalEdge2() { #expect(Pricing().loyaltyCredit(total: 250) == 20) // today 20 · with this mutant 10 } check today's value is the one you intend: the test pins what the code does now
done when mutastic verify m8a0288b4 samples/pricing passes reproduce mutastic run --only=m8a0288b4 samples/pricing or ignore mutastic ignore m8a0288b4 --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 ✓ 3 caught · ✗ 1 missed (4) Changed 4 lines in 1 file since HEAD Worst at Pricing.loyaltyCredit() 1 ✗ Build one build of every mutant, 1 round (build) Test runs 19 (36 without coverage selection) Start at 22:29:21 Duration 1.1s (setup 33ms, build 890ms, coverage 64ms, mutants 70ms, checking 21ms, reports 26ms) Tests 9 on swift 6.3.3 · 4 workers Reports samples/pricing/.build/mutastic/mutation.json · samples/pricing/.build/mutastic/mutation.html Score 75.0% FAIL Below the threshold of 80: 1 missed, 0 not covered.- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
The same gap, at the new edge. An order of exactly 250 gets 20 off:
@Testvoid loyaltyCreditAtThe250Edge() { assertEquals(20, new Pricing().loyaltyCredit(250));}mutastic verify m809c9c9b samples/pricingmutastic: m809c9c9b is caught by com.example.pricing.PricingTest#loyaltyCreditAtThe250EdgeThe id is the last cycle’s: a Java id counts a mutant’s place in its method, and the first >= is
at 250 now. It names the mutant there; the one at 100 is another id. See
Mutant ids.
#[test]fn loyalty_credit_at_the_250_edge() { assert_eq!(Pricing::new().loyalty_credit(250), 20);}mutastic verify m97fbc9b1 fixtures/pricingmutastic: m97fbc9b1 is caught by tests::loyalty_credit_at_the_250_edge@Test func loyaltyCreditAtThe250Edge() { #expect(Pricing().loyaltyCredit(total: 250) == 20)}mutastic verify m8a0288b4 samples/pricingmutastic: m8a0288b4 is caught by PricingTests.loyaltyCreditAtThe250Edge()Don’t commit yet: this cycle has a refactor in it.
Refactor
Section titled “Refactor”- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
The three ifs say one thing three times: from this total up, this credit, the highest tier first.
A fourth tier would be a fourth copy. The tiers are data, so the refactor is to hold them as data, a
table, read by one loop.
Start from green
Section titled “Start from green”A refactor starts where the cycle is done: the suite passes, and every mutant on the cycle’s lines is caught.
mutastic run samples/pricing --since=HEAD Classes 1 clean Mutants ✓ 4 caught (4) Changed 4 lines in 1 file since HEAD Test runs 8 (40 without coverage selection) Start at 19:09:22 Duration 1.6s (setup 0ms, build 411ms, coverage 214ms, mutants 48ms, checking 956ms, reports 7ms) Tests 10 on JDK 17 · 1 worker Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html Score 100.0% PASS Every mutant was caught.mutastic run fixtures/pricing --since=HEAD Files 1 clean Mutants ✓ 4 caught (4) Changed 15 lines in 1 file since HEAD Build one build of every mutant, 2 rounds (check and build) Test runs 23 (40 without coverage selection) Start at 18:54:29 Duration 355ms (setup 23ms, build 296ms, coverage 6ms, mutants 6ms, reports 1ms) Tests 10 on rustc 1.99.1 · 4 workers Reports fixtures/pricing/target/mutastic/mutation.json · fixtures/pricing/target/mutastic/mutation.html Score 100.0% PASS Every mutant was caught.mutastic run samples/pricing --since=HEAD Files 1 clean Mutants ✓ 4 caught (4) Changed 4 lines in 1 file since HEAD Build one build of every mutant, 1 round (build) Test runs 23 (40 without coverage selection) Start at 22:29:24 Duration 1.1s (setup 33ms, build 894ms, coverage 64ms, mutants 50ms, reports 27ms) Tests 10 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.The four are the new tier’s: its >= changed to >, its condition always true and always false,
and its 20 returned as 0.
Change the structure
Section titled “Change the structure”In place of the method, the table and the loop:
/// A loyalty tier: from this total up, this credit.private record Tier(int from, int credit) {}
/// The loyalty tiers, highest first.private static final List<Tier> LOYALTY_TIERS = List.of(new Tier(250, 20), new Tier(100, 10), new Tier(30, 5));
/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.public int loyaltyCredit(int total) { for (Tier tier : LOYALTY_TIERS) { if (total >= tier.from()) { return tier.credit(); } } return 0;}And import java.util.List; at the top of the file.
/// The loyalty tiers, highest first: from this total up, this credit.const LOYALTY_TIERS: [(u32, u32); 3] = [(250, 20), (100, 10), (30, 5)];
/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.pub fn loyalty_credit(&self, total: u32) -> u32 { for (from, credit) in Self::LOYALTY_TIERS { if total >= from { return credit; } } 0}/// The loyalty tiers, highest first: from this total up, this credit.private static let loyaltyTiers = [ (from: 250, credit: 20), (from: 100, credit: 10), (from: 30, credit: 5),]
/// For loyal customers: 5 off orders of 30 or more, 10 off from 100, and 20 off from 250.public func loyaltyCredit(total: Int) -> Int { for tier in Self.loyaltyTiers where total >= tier.from { return tier.credit } return 0}No test changes: a refactor changes structure only, and the tests are what say it kept the behaviour. The whole suite passes.
Run mutastic again
Section titled “Run mutastic again”mutastic run samples/pricing --since=HEAD Classes 1 clean Mutants ✓ 6 caught · 2 skipped (2 arid) (8) Changed 14 lines in 1 file since HEAD Test runs 10 (60 without coverage selection) Start at 19:09:24 Duration 1.7s (setup 0ms, build 448ms, coverage 194ms, mutants 64ms, checking 947ms, reports 13ms) Tests 10 on JDK 17 · 1 worker Reports samples/pricing/build/mutastic/mutation.json · samples/pricing/build/mutastic/mutation.html Score 100.0% PASS Every mutant was caught.mutastic run fixtures/pricing --since=HEAD Files 1 clean Mutants ✓ 4 caught (4) Changed 19 lines in 1 file since HEAD Build one build of every mutant, 2 rounds (check and build) Test runs 27 (40 without coverage selection) Start at 18:54:30 Duration 370ms (setup 22ms, build 316ms, coverage 6ms, mutants 4ms, reports 1ms) Tests 10 on rustc 1.99.1 · 4 workers Reports fixtures/pricing/target/mutastic/mutation.json · fixtures/pricing/target/mutastic/mutation.html Score 100.0% PASS Every mutant was caught.mutastic run samples/pricing --since=HEAD Files 1 clean Mutants ✓ 4 caught (4) Changed 8 lines in 1 file since HEAD Build one build of every mutant, 1 round (build) Test runs 27 (40 without coverage selection) Start at 22:29:25 Duration 1.2s (setup 32ms, build 940ms, coverage 64ms, mutants 49ms, reports 27ms) Tests 10 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.The table and the loop are new since HEAD, so these are the new shape’s mutants. The report,
mutation.html, shows each on its line:
- The loop’s
hasNext()always false, so no tier is read, and always true, so it reads past the last. total >= tier.from()changed tototal > tier.from(): one mutant for the edge of every tier, where theifs had one each.total >= tier.from()always true, and always false.tier.credit()returned as0.
The two skipped are the record’s from() and credit(), each returned as 0: code with nothing
to decide, which mutastic leaves out as arid.
total >= fromchanged tototal > from: one mutant for the edge of every tier, where theifs had one each.total >= fromalways true, and always false.creditreturned as0.
The loop itself gives none.
total >= tier.fromchanged tototal > tier.from: one mutant for the edge of every tier, where theifs had one each.total >= tier.fromalways true, and always false.tier.creditreturned as0.
The loop itself gives none.
The table’s numbers have no mutants, as the numbers in the ifs had none: the tests at each tier
and its edge are what check them. The loop depends on the table’s order, highest first, as the
ifs did on theirs, and the tests pin that too: with the lowest tier first, an order of 150 would
get 5, and the test that wants 10 fails.
Every one is caught. The tests still pin every behaviour of the new shape, with none of them changed: the refactor is safe.
The ids aren’t a guide to what changed. Some are the ones before the refactor, on different
mutants: md0b32a4f was the if at 250 always true, and is the loop’s hasNext() always true now,
as a Java id counts a mutant’s place in its method (see Mutant ids). Compare
what’s caught, not ids.
The ids aren’t the ones before the refactor. An id is a hash of what its mutant is, and every mutant here is on code that’s new (see Mutant ids). Compare what’s caught, not ids.
The ids are the ones before the refactor, where the change is the same. A Swift mutant’s id names
its function, its change, and which of that change it is in the function: the loop’s >= is the
first in loyaltyCredit, as the first if’s was, so it’s m8a0288b4 again, as at every tier
above (see Mutant ids). An id follows the change, not the line: compare what’s
caught, not ids.
- Red
- Green
- Mutate
- Triangulate
- Refactor
- Commit
Then commit. The commit is the next cycle’s HEAD.
Had a mutant been missed
Section titled “Had a mutant been missed”Before the refactor, the tests caught every mutant on the cycle’s lines. A mutant missed after it
means one of two things. The refactor wasn’t only structure: it added behaviour no test asks for,
such as a default for a case the ifs never had. Or the tests stopped pinning something: a test
was changed or taken out with the refactor, which a refactor never needs.
Either way, don’t write a test for the miss there and then. Undo that part of the change. If the behaviour is one you want, add it test first, in a cycle of its own, where its test fails until the code does it.