robocar-led/pkg/part/part_test.go

180 lines
5.3 KiB
Go
Raw Normal View History

2019-12-14 10:56:22 +00:00
package part
import (
"github.com/cyrilix/robocar-base/testtools"
2022-01-03 10:06:47 +00:00
"github.com/cyrilix/robocar-led/pkg/led"
2020-01-01 18:30:34 +00:00
"github.com/cyrilix/robocar-protobuf/go/events"
2019-12-14 10:56:22 +00:00
mqtt "github.com/eclipse/paho.mqtt.golang"
2022-01-03 10:07:28 +00:00
"google.golang.org/protobuf/proto"
2019-12-14 10:56:22 +00:00
"testing"
"time"
)
type fakeLed struct {
2022-01-03 10:06:47 +00:00
color led.Color
blink bool
}
func (f *fakeLed) SetColor(color led.Color) {
f.color = color
2019-12-14 10:56:22 +00:00
}
func (f *fakeLed) SetBlink(freq float64) {
if freq > 0 {
f.blink = true
} else {
f.blink = false
}
}
func TestLedPart_OnDriveMode(t *testing.T) {
2022-01-03 10:06:47 +00:00
l := fakeLed{}
2023-03-09 18:42:47 +00:00
p := LedPart{led: &l, speedZone: events.SpeedZone_FAST}
2019-12-14 10:56:22 +00:00
cases := []struct {
2022-01-03 10:06:47 +00:00
msg mqtt.Message
color led.Color
2019-12-14 10:56:22 +00:00
}{
2022-01-03 10:06:47 +00:00
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_USER}), led.ColorGreen},
2023-10-15 10:27:07 +00:00
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_COPILOT}), led.ColorAqua},
2022-01-03 10:06:47 +00:00
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_PILOT}), led.ColorBlue},
{testtools.NewFakeMessageFromProtobuf("drive", &events.DriveModeMessage{DriveMode: events.DriveMode_INVALID}), led.ColorBlue},
2019-12-14 10:56:22 +00:00
}
for _, c := range cases {
p.onDriveMode(nil, c.msg)
time.Sleep(1 * time.Millisecond)
2020-03-01 17:48:48 +00:00
var msg events.DriveModeMessage
err := proto.Unmarshal(c.msg.Payload(), &msg)
if err != nil {
t.Errorf("unable to unmarshal drive mode message: %v", err)
}
value := msg.DriveMode
2022-01-03 10:06:47 +00:00
if l.color != c.color {
2023-05-10 17:52:44 +00:00
t.Errorf("driveMode(%v)=invalid value for expectedColor: %v, wants %v", value, l.color, c.color)
2019-12-14 10:56:22 +00:00
}
}
}
2023-03-09 18:42:47 +00:00
2019-12-14 10:56:22 +00:00
func TestLedPart_OnRecord(t *testing.T) {
led := fakeLed{}
p := LedPart{led: &led}
cases := []struct {
msg mqtt.Message
record bool
blink bool
}{
2020-01-01 18:30:34 +00:00
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: false}), true, false},
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: true}), false, true},
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: false}), true, false},
{testtools.NewFakeMessageFromProtobuf("record", &events.SwitchRecordMessage{Enabled: true}), false, true},
2019-12-14 10:56:22 +00:00
}
for _, c := range cases {
p.onRecord(nil, c.msg)
if led.blink != c.blink {
2020-03-01 17:48:48 +00:00
var msg events.SwitchRecordMessage
err := proto.Unmarshal(c.msg.Payload(), &msg)
2019-12-14 10:56:22 +00:00
if err != nil {
2020-03-01 17:48:48 +00:00
t.Errorf("unable to unmarshal %T message: %v", msg, err)
2019-12-14 10:56:22 +00:00
}
2020-03-01 17:48:48 +00:00
value := msg.Enabled
2019-12-14 10:56:22 +00:00
t.Errorf("onRecord(%v): %v, wants %v", value, c.record, led.blink)
}
}
}
2023-03-09 18:42:47 +00:00
func TestLedPart_OnSpeedZone(t *testing.T) {
l := fakeLed{}
2023-05-10 17:52:44 +00:00
p := LedPart{led: &l, mode: LedModeSpeedZone, driveMode: events.DriveMode_PILOT}
2023-03-09 18:42:47 +00:00
cases := []struct {
msg mqtt.Message
color led.Color
}{
{testtools.NewFakeMessageFromProtobuf("speedzone", &events.SpeedZoneMessage{SpeedZone: events.SpeedZone_SLOW}), led.ColorRed},
{testtools.NewFakeMessageFromProtobuf("speedzone", &events.SpeedZoneMessage{SpeedZone: events.SpeedZone_NORMAL}), led.ColorYellow},
{testtools.NewFakeMessageFromProtobuf("speedzone", &events.SpeedZoneMessage{SpeedZone: events.SpeedZone_FAST}), led.ColorBlue},
{testtools.NewFakeMessageFromProtobuf("speedzone", &events.SpeedZoneMessage{SpeedZone: events.SpeedZone_UNKNOWN}), led.ColorWhite},
}
for _, c := range cases {
p.onSpeedZone(nil, c.msg)
time.Sleep(1 * time.Millisecond)
var msg events.SpeedZoneMessage
err := proto.Unmarshal(c.msg.Payload(), &msg)
if err != nil {
t.Errorf("unable to unmarshal drive mode message: %v", err)
}
value := msg.GetSpeedZone()
if l.color != c.color {
2023-05-10 17:52:44 +00:00
t.Errorf("driveMode(%v)=invalid value for expectedColor: %v, wants %v", value, l.color, c.color)
2023-03-09 18:42:47 +00:00
}
}
}
2023-05-10 17:52:44 +00:00
func TestLedPart_OnThrottle(t *testing.T) {
cases := []struct {
name string
msg mqtt.Message
expectedColor led.Color
}{
{"throttle stop",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: 0.}),
led.ColorBlue,
},
{
"throttle normal",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: 0.5}),
led.ColorBlue,
},
{
"near zero",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.01}),
led.ColorBlue,
},
{
"slow brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.06}),
led.ColorWhite,
2023-05-10 17:52:44 +00:00
},
{
"normal brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.4}),
led.ColorYellow,
},
{
"normal high brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -0.6}),
led.ColorRed,
2023-05-10 17:52:44 +00:00
},
{
"high brake",
testtools.NewFakeMessageFromProtobuf("throttle", &events.ThrottleMessage{Throttle: -1.}),
led.ColorPurple,
2023-05-10 17:52:44 +00:00
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
l := fakeLed{}
p := LedPart{led: &l, mode: LedModeBrake, driveMode: events.DriveMode_PILOT}
p.onThrottle(nil, c.msg)
time.Sleep(1 * time.Millisecond)
var msg events.ThrottleMessage
err := proto.Unmarshal(c.msg.Payload(), &msg)
if err != nil {
t.Errorf("unable to unmarshal drive mode message: %v", err)
}
value := msg.GetThrottle()
if l.color != c.expectedColor {
t.Errorf("driveMode(%v)=invalid value for expectedColor: %v, wants %v", value, l.color, c.expectedColor)
}
})
}
}