robocar-led/pkg/led/led_test.go

215 lines
4.7 KiB
Go
Raw Normal View History

2019-12-14 10:56:22 +00:00
package led
import (
2021-09-01 19:41:28 +00:00
"periph.io/x/conn/v3/gpio"
2019-12-14 10:56:22 +00:00
"sync"
"testing"
"time"
)
func TestColorLed_Red(t *testing.T) {
setLedBackup := setLed
defer func() { setLed = setLedBackup }()
2022-06-15 08:59:18 +00:00
ledColors := make(map[gpio.PinIO]int)
2019-12-14 10:56:22 +00:00
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
2022-06-15 08:59:18 +00:00
ledColors[led] = v
2019-12-14 10:56:22 +00:00
}
l := New()
if l.Red() != 0 {
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 0)
}
2022-06-15 08:59:18 +00:00
if ledColors[l.pinRed] != 0 {
t.Errorf("colorValue: %v, wants %v", ledColors[l.pinRed], 0)
2019-12-14 10:56:22 +00:00
}
2021-12-29 17:16:33 +00:00
l.SetColor(ColorRed)
2019-12-14 10:56:22 +00:00
if l.Red() != 255 {
t.Errorf("%T.Red(): %v, wants %v", l, l.Red(), 255)
}
2022-06-15 08:59:18 +00:00
if ledColors[l.pinRed] != 255 {
t.Errorf("colorValue: %v, wants %v", ledColors[l.pinRed], 255)
2019-12-14 10:56:22 +00:00
}
}
func TestColorLed_Green(t *testing.T) {
setLedBackup := setLed
defer func() { setLed = setLedBackup }()
2022-06-15 08:59:18 +00:00
ledColors := make(map[gpio.PinIO]int)
2019-12-14 10:56:22 +00:00
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
2022-06-15 08:59:18 +00:00
ledColors[led] = v
2019-12-14 10:56:22 +00:00
}
l := New()
if l.Green() != 0 {
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 0)
}
2022-06-15 08:59:18 +00:00
if ledColors[l.pinGreen] != 0 {
t.Errorf("colorValue: %v, wants %v", ledColors[l.pinGreen], 0)
2019-12-14 10:56:22 +00:00
}
2021-12-29 17:16:33 +00:00
l.SetColor(ColorGreen)
2019-12-14 10:56:22 +00:00
if l.Green() != 255 {
t.Errorf("%T.Green(): %v, wants %v", l, l.Green(), 255)
}
2022-06-15 08:59:18 +00:00
if ledColors[l.pinGreen] != 255 {
t.Errorf("colorValue: %v, wants %v", ledColors[l.pinGreen], 255)
2019-12-14 10:56:22 +00:00
}
}
func TestColorLed_Blue(t *testing.T) {
setLedBackup := setLed
defer func() { setLed = setLedBackup }()
2022-06-15 08:59:18 +00:00
ledColors := make(map[gpio.PinIO]int)
2019-12-14 10:56:22 +00:00
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
2022-06-15 08:59:18 +00:00
ledColors[led] = v
2019-12-14 10:56:22 +00:00
}
l := New()
if l.Blue() != 0 {
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 0)
}
2022-06-15 08:59:18 +00:00
if ledColors[l.pinBlue] != 0 {
t.Errorf("colorValue: %v, wants %v", ledColors[l.pinBlue], 0)
2019-12-14 10:56:22 +00:00
}
2021-12-29 17:16:33 +00:00
l.SetColor(ColorBlue)
2019-12-14 10:56:22 +00:00
if l.Blue() != 255 {
t.Errorf("%T.Blue(): %v, wants %v", l, l.Blue(), 255)
}
2022-06-15 08:59:18 +00:00
if ledColors[l.pinBlue] != 255 {
t.Errorf("colorValue: %v, wants %v", ledColors[l.pinBlue], 255)
2019-12-14 10:56:22 +00:00
}
}
func TestColorLed_SetBlink(t *testing.T) {
setLedBackup := setLed
defer func() { setLed = setLedBackup }()
var muFakeValue sync.Mutex
2022-06-15 08:59:18 +00:00
ledColors := make(map[gpio.PinIO]int)
2019-12-14 10:56:22 +00:00
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
muFakeValue.Lock()
defer muFakeValue.Unlock()
2022-06-15 08:59:18 +00:00
ledColors[led] = v
2019-12-14 10:56:22 +00:00
}
2022-06-15 08:59:18 +00:00
readValue := func(p gpio.PinIO) int {
2019-12-14 10:56:22 +00:00
muFakeValue.Lock()
defer muFakeValue.Unlock()
2022-06-15 08:59:18 +00:00
return ledColors[p]
2019-12-14 10:56:22 +00:00
}
l := New()
2021-12-29 17:16:33 +00:00
l.SetColor(ColorBlue)
2022-06-15 08:59:18 +00:00
v := ledColors[l.pinBlue]
2019-12-14 10:56:22 +00:00
if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255)
}
l.SetBlink(100)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255)
}
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 0 {
t.Errorf("colorValue: %v, wants %v", v, 0)
}
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255)
}
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 0 {
t.Errorf("colorValue: %v, wants %v", v, 0)
}
// Stop blink
l.SetBlink(0)
time.Sleep(5 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255)
}
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255)
}
}
func TestColorLed_SetBlinkAndUpdadeColor(t *testing.T) {
setLedBackup := setLed
defer func() { setLed = setLedBackup }()
var muFakeValue sync.Mutex
2022-06-15 08:59:18 +00:00
ledColors := make(map[gpio.PinIO]int)
2019-12-14 10:56:22 +00:00
setLed = func(v int, led gpio.PinIO, mutex *sync.Mutex) {
mutex.Lock()
defer mutex.Unlock()
muFakeValue.Lock()
defer muFakeValue.Unlock()
2022-06-15 08:59:18 +00:00
ledColors[led] = v
2019-12-14 10:56:22 +00:00
}
2022-06-15 08:59:18 +00:00
readValue := func(p gpio.PinIO) int {
2019-12-14 10:56:22 +00:00
muFakeValue.Lock()
defer muFakeValue.Unlock()
2022-06-15 08:59:18 +00:00
return ledColors[p]
2019-12-14 10:56:22 +00:00
}
l := New()
2021-12-29 17:16:33 +00:00
l.SetColor(ColorBlue)
2019-12-14 10:56:22 +00:00
l.SetBlink(100)
2022-06-15 08:59:18 +00:00
v := readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 255 {
t.Errorf("colorValue: %v, wants %v", v, 255)
}
time.Sleep(6 * time.Millisecond)
2021-12-29 17:16:33 +00:00
l.SetColor(ColorBlue)
2019-12-14 10:56:22 +00:00
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
if v != 255 {
2019-12-14 10:56:22 +00:00
t.Errorf("colorValue: %v, wants %v", v, 128)
}
time.Sleep(6 * time.Millisecond)
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
if v != 255 {
2019-12-14 10:56:22 +00:00
t.Errorf("colorValue: %v, wants %v", v, 128)
}
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
2019-12-14 10:56:22 +00:00
if v != 0 {
t.Errorf("colorValue: %v, wants %v", v, 0)
}
// Stop blink
l.SetBlink(0)
time.Sleep(5 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
if v != 255 {
2019-12-14 10:56:22 +00:00
t.Errorf("colorValue: %v, wants %v", v, 128)
}
time.Sleep(12 * time.Millisecond)
2022-06-15 08:59:18 +00:00
v = readValue(l.pinBlue)
if v != 255 {
2019-12-14 10:56:22 +00:00
t.Errorf("colorValue: %v, wants %v", v, 128)
}
}