[refactor] Move mode in generic types module
This commit is contained in:
		
							
								
								
									
										36
									
								
								types/mode.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								types/mode.go
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										40
									
								
								types/mode_test.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user