package xiaomi import "testing" func TestStrVal(t *testing.T) { tests := []struct { name string m map[string]interface{} key string want string }{ {"string value", map[string]interface{}{"name": "test"}, "name", "test"}, {"missing key", map[string]interface{}{"name": "test"}, "nonexistent", ""}, {"non-string value", map[string]interface{}{"count": 42}, "count", ""}, {"empty map", map[string]interface{}{}, "any", ""}, {"nil map", nil, "any", ""}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := strVal(tt.m, tt.key) if got != tt.want { t.Errorf("strVal() = %q, want %q", got, tt.want) } }) } } func TestIntVal(t *testing.T) { tests := []struct { name string m map[string]interface{} key string want int }{ {"float64 value", map[string]interface{}{"rssi": float64(-55)}, "rssi", -55}, {"int value", map[string]interface{}{"count": 42}, "count", 42}, {"int64 value", map[string]interface{}{"ts": int64(1234567890)}, "ts", 1234567890}, {"missing key", map[string]interface{}{"a": 1}, "b", 0}, {"non-numeric", map[string]interface{}{"name": "hello"}, "name", 0}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := intVal(tt.m, tt.key) if got != tt.want { t.Errorf("intVal() = %d, want %d", got, tt.want) } }) } } func TestBoolVal(t *testing.T) { tests := []struct { name string m map[string]interface{} key string want bool }{ {"true value", map[string]interface{}{"online": true}, "online", true}, {"false value", map[string]interface{}{"online": false}, "online", false}, {"missing key", map[string]interface{}{"a": true}, "b", false}, {"non-bool", map[string]interface{}{"name": "true"}, "name", false}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := boolVal(tt.m, tt.key) if got != tt.want { t.Errorf("boolVal() = %v, want %v", got, tt.want) } }) } } func TestStrSliceVal(t *testing.T) { tests := []struct { name string m map[string]interface{} key string want []string }{ { "string slice", map[string]interface{}{"dids": []interface{}{"did1", "did2"}}, "dids", []string{"did1", "did2"}, }, { "missing key", map[string]interface{}{"a": "b"}, "c", nil, }, { "empty slice", map[string]interface{}{"dids": []interface{}{}}, "dids", []string{}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := strSliceVal(tt.m, tt.key) if len(got) != len(tt.want) { t.Errorf("strSliceVal() len = %d, want %d", len(got), len(tt.want)) return } for i := range got { if got[i] != tt.want[i] { t.Errorf("strSliceVal()[%d] = %q, want %q", i, got[i], tt.want[i]) } } }) } }