chore: 提交高层 xiaomi 封装库及示例
- 新增 xiaomi/examples/ 下 8 个示例程序(用户/家庭查询、设备列表、开关/灯光/空调控制、属性订阅、高级过滤分类、SPEC 解析) - miot_client_sub.go: 重构 SubProp/SubEvent,使用 buildPropTopic/buildEventTopic 支持通配符订阅(siid/piid=0 → +),并修复锁顺序问题(将 RequestRefreshProp 移到 Lock 外) - spec_parser.go: 新增 downloadSpecFile 方法,本地 SPEC 文件缺失时自动从 miot-spec.org 下载 - ARCH_PLAN.md: 架构设计从 Proposed 更新为 Accepted(v1.0→v1.1),补充设备分类/工厂/SPEC 映射等模块设计 - xiaomi/: 新增 miot 上层强类型封装模块,包含 Client 主入口、用户/家庭/设备 API、属性读写、动作调用、订阅通知,以及 devices/ 设备控制抽象(Switch/Light/AirConditioner/Fan/Cover/Humidifier/Vacuum/WaterHeater/Thermostat)和 specs/ SPEC 查询辅助 - 更多 xiaomi 示例(风扇/窗帘/传感器控制)
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
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])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user