From ae533ffdfc5152373a1ec3c6347d6ec19a9f4456 Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Wed, 15 Jun 2022 10:59:18 +0200 Subject: [PATCH] cicd: fix unit tests --- pkg/led/led_test.go | 86 ++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/pkg/led/led_test.go b/pkg/led/led_test.go index aa6f48a..eea99b1 100644 --- a/pkg/led/led_test.go +++ b/pkg/led/led_test.go @@ -11,27 +11,27 @@ func TestColorLed_Red(t *testing.T) { setLedBackup := setLed defer func() { setLed = setLedBackup }() - var colorValue int + ledColors := make(map[gpio.PinIO]int) setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) { mutex.Lock() defer mutex.Unlock() - colorValue = v + ledColors[led] = v } l := New() if l.Red() != 0 { t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 0) } - if colorValue != 0 { - t.Errorf("colorValue: %v, wants %v", colorValue, 0) + if ledColors[l.pinRed] != 0 { + t.Errorf("colorValue: %v, wants %v", ledColors[l.pinRed], 0) } l.SetColor(ColorRed) if l.Red() != 255 { t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255) } - if colorValue != 255 { - t.Errorf("colorValue: %v, wants %v", colorValue, 255) + if ledColors[l.pinRed] != 255 { + t.Errorf("colorValue: %v, wants %v", ledColors[l.pinRed], 255) } } @@ -39,27 +39,27 @@ func TestColorLed_Green(t *testing.T) { setLedBackup := setLed defer func() { setLed = setLedBackup }() - var colorValue int + ledColors := make(map[gpio.PinIO]int) setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) { mutex.Lock() defer mutex.Unlock() - colorValue = v + ledColors[led] = v } l := New() if l.Green() != 0 { t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 0) } - if colorValue != 0 { - t.Errorf("colorValue: %v, wants %v", colorValue, 0) + if ledColors[l.pinGreen] != 0 { + t.Errorf("colorValue: %v, wants %v", ledColors[l.pinGreen], 0) } l.SetColor(ColorGreen) if l.Green() != 255 { t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255) } - if colorValue != 255 { - t.Errorf("colorValue: %v, wants %v", colorValue, 255) + if ledColors[l.pinGreen] != 255 { + t.Errorf("colorValue: %v, wants %v", ledColors[l.pinGreen], 255) } } @@ -67,27 +67,27 @@ func TestColorLed_Blue(t *testing.T) { setLedBackup := setLed defer func() { setLed = setLedBackup }() - var colorValue int + ledColors := make(map[gpio.PinIO]int) setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) { mutex.Lock() defer mutex.Unlock() - colorValue = v + ledColors[led] = v } l := New() if l.Blue() != 0 { t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 0) } - if colorValue != 0 { - t.Errorf("colorValue: %v, wants %v", colorValue, 0) + if ledColors[l.pinBlue] != 0 { + t.Errorf("colorValue: %v, wants %v", ledColors[l.pinBlue], 0) } l.SetColor(ColorBlue) if l.Blue() != 255 { t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255) } - if colorValue != 255 { - t.Errorf("colorValue: %v, wants %v", colorValue, 255) + if ledColors[l.pinBlue] != 255 { + t.Errorf("colorValue: %v, wants %v", ledColors[l.pinBlue], 255) } } @@ -96,43 +96,43 @@ func TestColorLed_SetBlink(t *testing.T) { defer func() { setLed = setLedBackup }() var muFakeValue sync.Mutex - var colorValue int + ledColors := make(map[gpio.PinIO]int) setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) { mutex.Lock() defer mutex.Unlock() muFakeValue.Lock() defer muFakeValue.Unlock() - colorValue = v + ledColors[led] = v } - readValue := func() int { + readValue := func(p gpio.PinIO) int { muFakeValue.Lock() defer muFakeValue.Unlock() - return colorValue + return ledColors[p] } l := New() l.SetColor(ColorBlue) - v := readValue() + v := ledColors[l.pinBlue] if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 255) } l.SetBlink(100) - v = readValue() + v = readValue(l.pinBlue) if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 255) } time.Sleep(12 * time.Millisecond) - v = readValue() + v = readValue(l.pinBlue) if v != 0 { t.Errorf("colorValue: %v, wants %v", v, 0) } time.Sleep(12 * time.Millisecond) - v = readValue() + v = readValue(l.pinBlue) if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 255) } time.Sleep(12 * time.Millisecond) - v = readValue() + v = readValue(l.pinBlue) if v != 0 { t.Errorf("colorValue: %v, wants %v", v, 0) } @@ -140,12 +140,12 @@ func TestColorLed_SetBlink(t *testing.T) { // Stop blink l.SetBlink(0) time.Sleep(5 * time.Millisecond) - v = readValue() + v = readValue(l.pinBlue) if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 255) } time.Sleep(12 * time.Millisecond) - v = readValue() + v = readValue(l.pinBlue) if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 255) } @@ -156,45 +156,45 @@ func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) { defer func() { setLed = setLedBackup }() var muFakeValue sync.Mutex - var colorValue int + ledColors := make(map[gpio.PinIO]int) setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) { mutex.Lock() defer mutex.Unlock() muFakeValue.Lock() defer muFakeValue.Unlock() - colorValue = v + ledColors[led] = v } - readValue := func() int { + readValue := func(p gpio.PinIO) int { muFakeValue.Lock() defer muFakeValue.Unlock() - return colorValue + return ledColors[p] } l := New() l.SetColor(ColorBlue) l.SetBlink(100) - v := readValue() + v := readValue(l.pinBlue) if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 255) } time.Sleep(6 * time.Millisecond) l.SetColor(ColorBlue) - v = readValue() - if v != 128 { + v = readValue(l.pinBlue) + if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 128) } time.Sleep(6 * time.Millisecond) time.Sleep(12 * time.Millisecond) - v = readValue() - if v != 128 { + v = readValue(l.pinBlue) + if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 128) } time.Sleep(12 * time.Millisecond) - v = readValue() + v = readValue(l.pinBlue) if v != 0 { t.Errorf("colorValue: %v, wants %v", v, 0) } @@ -202,13 +202,13 @@ func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) { // Stop blink l.SetBlink(0) time.Sleep(5 * time.Millisecond) - v = readValue() - if v != 128 { + v = readValue(l.pinBlue) + if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 128) } time.Sleep(12 * time.Millisecond) - v = readValue() - if v != 128 { + v = readValue(l.pinBlue) + if v != 255 { t.Errorf("colorValue: %v, wants %v", v, 128) } }