package main import "testing" // ---------- propString ---------- func TestPropString(t *testing.T) { cases := []struct { in interface{} want string }{ {nil, ""}, {"", ""}, {" hello ", "hello"}, {"张 三", "张 三"}, {float64(42), "42"}, {true, "true"}, {false, "false"}, {[]interface{}{"a", "b"}, "[a b]"}, // 数组 → fmt.Sprintf 字面量(propString 注释说仅 / [] / map[] 判空) {map[string]interface{}{"k": 1}, "map[k:1]"}, // map → 同上 } for _, c := range cases { if got := propString(c.in); got != c.want { t.Errorf("propString(%v) = %q, want %q", c.in, got, c.want) } } } // ---------- resolveDisplayName ---------- func TestResolveDisplayName(t *testing.T) { props := map[string]interface{}{ "name": " 张三 ", "label": "Label", "title": "Title", "phone": "13800000000", "nested": map[string]interface{}{"k": 1}, } // 1. 显式 title_property 优先 if got := resolveDisplayName("phone", props); got != "13800000000" { t.Errorf("title_property=phone → %q, want 13800000000", got) } // 2. title_property 未命中 → 走回退链 if got := resolveDisplayName("does_not_exist", props); got != "张三" { t.Errorf("title_property 缺失 → %q, want name 回退值", got) } // 3. 回退链顺序:name → title → real_name → display_name → label onlyLabel := map[string]interface{}{"label": " L "} if got := resolveDisplayName("", onlyLabel); got != "L" { t.Errorf("仅 label → %q, want L", got) } noName := map[string]interface{}{"real_name": "R", "title": "T"} if got := resolveDisplayName("", noName); got != "T" { t.Errorf("无 name + title+real_name → %q, want T (title 优先于 real_name)", got) } // 4. 全部为空 → 返回 "" if got := resolveDisplayName("", map[string]interface{}{}); got != "" { t.Errorf("空 props → %q, want ''", got) } // 5. title_property 指向空值 → 回退链 missing := map[string]interface{}{"title": "from-title"} if got := resolveDisplayName("phone", missing); got != "from-title" { t.Errorf("title_property=phone(phone 为空) → %q, want from-title 回退", got) } }