[mqtt] Implements mqtt publish tooling
This commit is contained in:
37
mode/mode.go
Normal file
37
mode/mode.go
Normal file
@ -0,0 +1,37 @@
|
||||
package mode
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
type DriveMode int
|
||||
|
||||
const (
|
||||
DriveModeInvalid = -1
|
||||
DriveModeUser = iota
|
||||
DriveModePilot
|
||||
)
|
||||
|
||||
func ToString(mode DriveMode) string {
|
||||
switch mode {
|
||||
case DriveModeUser:
|
||||
return "user"
|
||||
case DriveModePilot:
|
||||
return "pilot"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func ParseString(val string) DriveMode {
|
||||
switch val {
|
||||
case "user":
|
||||
return DriveModeUser
|
||||
case "pilot":
|
||||
return DriveModePilot
|
||||
default:
|
||||
log.Printf("invalid DriveMode: %v", val)
|
||||
return DriveModeInvalid
|
||||
|
||||
}
|
||||
}
|
40
mode/mode_test.go
Normal file
40
mode/mode_test.go
Normal file
@ -0,0 +1,40 @@
|
||||
package mode
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestToString(t *testing.T) {
|
||||
cases := []struct{
|
||||
value DriveMode
|
||||
expected string
|
||||
}{
|
||||
{DriveModeUser, "user"},
|
||||
{DriveModePilot, "pilot"},
|
||||
{DriveModeInvalid, ""},
|
||||
}
|
||||
|
||||
for _, c := range cases{
|
||||
val := ToString(c.value)
|
||||
if val != c.expected{
|
||||
t.Errorf("ToString(%v): %v, wants %v", c.value, val, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseString(t *testing.T) {
|
||||
cases := []struct{
|
||||
value string
|
||||
expected DriveMode
|
||||
}{
|
||||
{"user", DriveModeUser},
|
||||
{"pilot",DriveModePilot},
|
||||
{"", DriveModeInvalid},
|
||||
{"invalid", DriveModeInvalid},
|
||||
}
|
||||
|
||||
for _, c := range cases{
|
||||
val := ParseString(c.value)
|
||||
if val != c.expected{
|
||||
t.Errorf("ParseString(%v): %v, wants %v", c.value, val, c.expected)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user