From 12263dcb53edc61065958be27fa0e5223c238a00 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Mon, 12 Nov 2018 19:06:32 -0500 Subject: [PATCH] Stop using t.Fail() and FailNow This is bad habit and maked diagnosing test failure hard. It's not something we want to encourag,e so remove all traces from the code. --- devices/apa102/apa102_test.go | 23 +++++++++++-------- devices/apa102/temperature_test.go | 2 +- devices/lepton/image14bit/intensity14_test.go | 7 ++++-- devices/ssd1306/image1bit/image1bit_test.go | 11 +++++---- experimental/devices/nrzled/nrz_spi_test.go | 8 +++---- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/devices/apa102/apa102_test.go b/devices/apa102/apa102_test.go index 6aeee30..63538bf 100644 --- a/devices/apa102/apa102_test.go +++ b/devices/apa102/apa102_test.go @@ -289,18 +289,21 @@ func TestRamp(t *testing.T) { } } for i, line := range data { - if i != int(line.input) || line.expected != ramp(line.input, maxOut) { - t.Fail() + if i != int(line.input) { + t.Fatalf("expected ordered inputs; %d != %d", i, line.input) + } + if actual := ramp(line.input, maxOut); actual != line.expected { + t.Fatalf("%d: %v != %v", i, line.expected, actual) } } - if 0x00 != ramp(0x00, 0xFF) { - t.Fail() + if v := ramp(0x00, 0xFF); v != 0x00 { + t.Fatal(v) } - if 0x21 != ramp(0x7F, 0xFF) { - t.Fail() + if v := ramp(0x7F, 0xFF); v != 0x21 { + t.Fatal(v) } - if 0xFF != ramp(0xFF, 0xFF) { - t.Fail() + if v := ramp(0xFF, 0xFF); v != 0xFF { + t.Fatal(v) } } @@ -567,8 +570,8 @@ func TestWrites(t *testing.T) { } func TestDevColor(t *testing.T) { - if (&Dev{}).ColorModel() != color.NRGBAModel { - t.Fail() + if c := (&Dev{}).ColorModel(); c != color.NRGBAModel { + t.Fatal(c) } } diff --git a/devices/apa102/temperature_test.go b/devices/apa102/temperature_test.go index 2cd14f0..d20fd23 100644 --- a/devices/apa102/temperature_test.go +++ b/devices/apa102/temperature_test.go @@ -19,7 +19,7 @@ func TestToRGBFast_limits(t *testing.T) { func BenchmarkToRGBFast(b *testing.B) { for i := 0; i < b.N; i++ { if r, g, blue := toRGBFast(30000); r != 159 || g != 191 || blue != 255 { - b.FailNow() + b.Fatal(r, g, blue) } } } diff --git a/devices/lepton/image14bit/intensity14_test.go b/devices/lepton/image14bit/intensity14_test.go index 1d72ea0..ef5771e 100644 --- a/devices/lepton/image14bit/intensity14_test.go +++ b/devices/lepton/image14bit/intensity14_test.go @@ -16,8 +16,11 @@ func TestIntensity14(t *testing.T) { if r, g, b, a := Intensity14(0).RGBA(); r != 0 || g != r || b != r || a != 65535 { t.Fatal(r, g, b, a) } - if Intensity14(16383).String() != "Intensity14(16383)" || Intensity14(0).String() != "Intensity14(0)" { - t.Fail() + if s := Intensity14(16383).String(); s != "Intensity14(16383)" { + t.Fatal(s) + } + if s := Intensity14(0).String(); s != "Intensity14(0)" { + t.Fatal(s) } if Intensity14(8192) != convertIntensity14(Intensity14(8192)) { t.Fatal("failed to convert Intensity14 correctly") diff --git a/devices/ssd1306/image1bit/image1bit_test.go b/devices/ssd1306/image1bit/image1bit_test.go index d8c851d..c2a4867 100644 --- a/devices/ssd1306/image1bit/image1bit_test.go +++ b/devices/ssd1306/image1bit/image1bit_test.go @@ -17,11 +17,14 @@ func TestBit(t *testing.T) { if r, g, b, a := Off.RGBA(); r != 0 || g != r || b != r || a != 65535 { t.Fatal(r, g, b, a) } - if On.String() != "On" || Off.String() != "Off" { - t.Fail() + if s := On.String(); s != "On" { + t.Fatal(s) } - if On != convertBit(On) { - t.Fail() + if s := Off.String(); s != "Off" { + t.Fatal(s) + } + if b := convertBit(On); b != On { + t.Fatal(b) } } diff --git a/experimental/devices/nrzled/nrz_spi_test.go b/experimental/devices/nrzled/nrz_spi_test.go index 931e354..2a23036 100644 --- a/experimental/devices/nrzled/nrz_spi_test.go +++ b/experimental/devices/nrzled/nrz_spi_test.go @@ -231,14 +231,14 @@ func TestWrites(t *testing.T) { t.Logf("(%d) Got: %#02v\tWant: %#02v\n", i, got[i], tt.want[i]) } } - t.Fail() + t.Fatal("expectation failure") } } } func TestDevColor(t *testing.T) { - if (&Dev{}).ColorModel() != color.NRGBAModel { - t.Fail() + if c := (&Dev{}).ColorModel(); c != color.NRGBAModel { + t.Fatal(c) } } @@ -342,7 +342,7 @@ func TestDraws(t *testing.T) { t.Logf("(%d) Got: %#02v\tWant: %#02v\n", i, got[i], tt.want[i]) } } - t.Fail() + t.Fatal("expectation failure") } } }