Files
xiaomihome/xiaomi/convert_test.go
T
4566704 2b7de69403 refactor(logger): 日志改用 logrus 全局包级调用,移除 Logger 注入接口
为什么:并入宿主项目后须与宿主日志体系一致——宿主统一用 pkg/logger 初始化
logrus 全局实例并直接 logrus.Xxx 包级调用;包一层 Logger 接口会让 logrus 的
caller 定位(报告调用文件:行号)失准。

改动:
- 删除 logger 包(Logger 接口 / Default / SetDefault,默认本就是 logrus.StandardLogger)
- 11 个结构体移除 lgr logger.Logger 字段、SetLogger 方法及构造中的 lgr 初始化
- 192 处 lgr.Xxxf 调用改为 logrus.Xxxf,日志消息文本保持不变
- xiaomi.Client 移除 Logger() 访问器,air_conditioner 回读日志去掉判空调用直连 logrus
- miot_i18n.go 两处标准库 log.Printf 改为 logrus.Errorf,统一日志出口
- go.mod:logrus 从 indirect 提升为直接依赖
- goimports/goformat 全树规范化:此前部分文件未 gofmt(单行 if、对齐),本次顺带
  格式化,纯空白/换行变化,无逻辑改动

验证:go build / go vet / go test ./... 全部通过(bridge、miot、xiaomi、devices、specs)
2026-09-15 20:16:26 +08:00

114 lines
2.7 KiB
Go

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])
}
}
})
}
}