跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

reflectutil_test.go

133 行 · 3.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package reflectutil
5
6import (
7 "fmt"
8 "slices"
9 "strings"
10 "testing"
11
12 "gotest.tools/v3/assert"
13)
14
15// testStruct is used as the input type for all test cases.
16type testStruct struct {
17 Known string
18 Unknown string
19 Tags []string
20 Meta map[string]string
21}
22
23func TestUnknownNonEmptyFields(t *testing.T) {
24 tests := []struct {
25 name string
26 input any
27 knownFields []string
28 expected []string
29 wantPanic bool
30 }{
31 {
32 // All fields are in the known list — result must be empty.
33 name: "no unknown fields",
34 input: testStruct{Known: "hello", Unknown: "world"},
35 knownFields: []string{"Known", "Unknown"},
36 expected: nil,
37 },
38 {
39 // All fields are zero value — isEmpty() must filter all of them out.
40 name: "zero value fields ignored",
41 input: testStruct{},
42 knownFields: nil,
43 expected: nil,
44 },
45 {
46 // Function accepts *struct in addition to struct — ptr branch in switch.
47 name: "pointer to struct",
48 input: &testStruct{Known: "hello", Unknown: "world"},
49 knownFields: []string{"Known"},
50 expected: []string{"Unknown"},
51 },
52 {
53 // isEmpty() uses v.Len()==0 for slices — zero-length non-nil slice = empty.
54 name: "empty slice ignored",
55 input: testStruct{Tags: []string{}},
56 knownFields: nil,
57 expected: nil,
58 },
59 {
60 // isEmpty() uses v.Len()==0 for maps — zero-length non-nil map = empty.
61 name: "empty map ignored",
62 input: testStruct{Meta: map[string]string{}},
63 knownFields: nil,
64 expected: nil,
65 },
66 {
67 // Non-empty slice is not zero — must appear in result.
68 name: "non-empty slice reported",
69 input: testStruct{Tags: []string{"a", "b"}},
70 knownFields: nil,
71 expected: []string{"Tags"},
72 },
73 {
74 // Non-empty map is not zero — must appear in result.
75 name: "non-empty map reported",
76 input: testStruct{Meta: map[string]string{"key": "val"}},
77 knownFields: nil,
78 expected: []string{"Meta"},
79 },
80 {
81 // All non-empty fields that are not in the known list must be reported.
82 name: "multiple unknown fields",
83 input: testStruct{Known: "hello", Unknown: "world", Tags: []string{"a"}},
84 knownFields: []string{"Known"},
85 expected: []string{"Tags", "Unknown"},
86 },
87 {
88 // Some non-empty fields are known, others are not — only unknown ones returned.
89 name: "mixed known and unknown",
90 input: testStruct{Known: "hello", Unknown: "world"},
91 knownFields: []string{"Known"},
92 expected: []string{"Unknown"},
93 },
94 {
95 // Calling Elem() on a nil pointer causes a panic.
96 name: "nil pointer panics",
97 input: (*testStruct)(nil),
98 wantPanic: true,
99 },
100 {
101 // Non-struct/non-pointer hits the default case which calls panic().
102 name: "non-struct panics",
103 input: "this is a string",
104 wantPanic: true,
105 },
106 }
107
108 for _, tt := range tests {
109 t.Run(tt.name, func(t *testing.T) {
110 if tt.wantPanic {
111 defer func() {
112 r := recover()
113 if r == nil {
114 t.Error("expected panic but did not panic")
115 return
116 }
117 msg := fmt.Sprint(r)
118 if !strings.Contains(msg, "expected pointer or struct") && !strings.Contains(msg, "reflect") {
119 t.Errorf("unexpected panic message: %v", msg)
120 }
121 }()
122 UnknownNonEmptyFields(tt.input, tt.knownFields...)
123 return
124 }
125
126 result := UnknownNonEmptyFields(tt.input, tt.knownFields...)
127
128 slices.Sort(result)
129 slices.Sort(tt.expected)
130 assert.DeepEqual(t, tt.expected, result)
131 })
132 }
133}