Created
June 26, 2018 16:27
-
-
Save prestonvanloon/6dafc335b758fc33d883788503737fa2 to your computer and use it in GitHub Desktop.
Mutation testing example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data represents some arbitrary thing in the universe. | |
type Data struct { | |
prices []int | |
checked bool | |
} | |
// Moon? returns true if the price is indicative of the thing going to the moon. | |
func Moon(data *Data) bool { | |
// Is the next line important? | |
// Would any test fail if this line were mutated to | |
// _ = true ? | |
data.checked = true | |
if data.prices[0] > 0 { | |
return true | |
} | |
return false | |
} | |
// TestMoon covers 100% lines of code, but it doesn't test any side effects such | |
// as the one on line 15. | |
func TestMoon(t *testing.T) { | |
tests := []struct { | |
data *Data | |
want bool | |
}{ | |
{ | |
data: &Data{prices: []int{0}}, | |
want: false, | |
}, | |
{ | |
data: &Data{prices: []int{1}}, | |
want: true, | |
}, | |
} | |
for _, tt := range tests { | |
got := Moon(tt.data) | |
if got != tt.want { | |
t.Errorf("Moon(%+v) = %t, wanted %t", tt.data, got, tt.want) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment