[refactor] Move mode in generic types module

This commit is contained in:
2019-12-21 18:13:58 +01:00
parent a0dedb056b
commit 37e90572fd
4 changed files with 11 additions and 12 deletions

36
types/mode.go Normal file
View File

@ -0,0 +1,36 @@
package types
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
types/mode_test.go Normal file
View File

@ -0,0 +1,40 @@
package types
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)
}
}
}