Skip to content

Commit c29d956

Browse files
authored
Merge pull request #1385 from EngineeringKiosk/test/go-tags-find-helpers
test: add unit tests for tag finding helpers
2 parents e22aec5 + 3955bf3 commit c29d956

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestConvertToStringSlice(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input any
11+
expected []string
12+
}{
13+
{
14+
name: "nil input",
15+
input: nil,
16+
expected: nil,
17+
},
18+
{
19+
name: "[]any with strings",
20+
input: []any{"Go", "Rust", "Python"},
21+
expected: []string{"Go", "Rust", "Python"},
22+
},
23+
{
24+
name: "[]any with mixed types filters non-strings",
25+
input: []any{"Go", 42, "Rust", true, "Python"},
26+
expected: []string{"Go", "Rust", "Python"},
27+
},
28+
{
29+
name: "[]any empty slice",
30+
input: []any{},
31+
expected: []string{},
32+
},
33+
{
34+
name: "[]string passthrough",
35+
input: []string{"one", "two"},
36+
expected: []string{"one", "two"},
37+
},
38+
{
39+
name: "unexpected type returns nil",
40+
input: "just a string",
41+
expected: nil,
42+
},
43+
{
44+
name: "int type returns nil",
45+
input: 42,
46+
expected: nil,
47+
},
48+
}
49+
50+
for _, tt := range tests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
got := convertToStringSlice(tt.input)
53+
if tt.expected == nil {
54+
if got != nil {
55+
t.Errorf("convertToStringSlice(%v) = %v, want nil", tt.input, got)
56+
}
57+
return
58+
}
59+
if len(got) != len(tt.expected) {
60+
t.Fatalf("convertToStringSlice(%v) returned %d items, want %d", tt.input, len(got), len(tt.expected))
61+
}
62+
for i, want := range tt.expected {
63+
if got[i] != want {
64+
t.Errorf("convertToStringSlice(%v)[%d] = %q, want %q", tt.input, i, got[i], want)
65+
}
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestGetAllTagsWithoutDescription(t *testing.T) {
72+
tests := []struct {
73+
name string
74+
tagDescriptions map[string]*TagDescription
75+
tags map[string]int
76+
expected map[string]bool
77+
}{
78+
{
79+
name: "all tags have complete descriptions",
80+
tagDescriptions: map[string]*TagDescription{"Go": {ShortDesc: "short", LongDesc: "long"}},
81+
tags: map[string]int{"Go": 5},
82+
expected: map[string]bool{},
83+
},
84+
{
85+
name: "tag missing from description file",
86+
tagDescriptions: map[string]*TagDescription{},
87+
tags: map[string]int{"Rust": 3},
88+
expected: map[string]bool{"Rust": true},
89+
},
90+
{
91+
name: "tag with empty ShortDesc",
92+
tagDescriptions: map[string]*TagDescription{"Go": {ShortDesc: "", LongDesc: "long"}},
93+
tags: map[string]int{"Go": 5},
94+
expected: map[string]bool{"Go": true},
95+
},
96+
{
97+
name: "tag with empty LongDesc",
98+
tagDescriptions: map[string]*TagDescription{"Go": {ShortDesc: "short", LongDesc: ""}},
99+
tags: map[string]int{"Go": 5},
100+
expected: map[string]bool{"Go": true},
101+
},
102+
{
103+
name: "both empty inputs",
104+
tagDescriptions: map[string]*TagDescription{},
105+
tags: map[string]int{},
106+
expected: map[string]bool{},
107+
},
108+
{
109+
name: "mixed: some described, some missing, some incomplete",
110+
tagDescriptions: map[string]*TagDescription{
111+
"Go": {ShortDesc: "short", LongDesc: "long"},
112+
"Rust": {ShortDesc: "", LongDesc: "long"},
113+
"Python": {ShortDesc: "short", LongDesc: ""},
114+
},
115+
tags: map[string]int{"Go": 5, "Rust": 3, "Python": 2, "Java": 1},
116+
expected: map[string]bool{"Rust": true, "Python": true, "Java": true},
117+
},
118+
}
119+
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
got := getAllTagsWithoutDescription(tt.tagDescriptions, tt.tags)
123+
if len(got) != len(tt.expected) {
124+
t.Fatalf("getAllTagsWithoutDescription() returned %d items, want %d\n got: %v\n want: %v", len(got), len(tt.expected), got, tt.expected)
125+
}
126+
for tag := range tt.expected {
127+
if !got[tag] {
128+
t.Errorf("expected tag %q to be in result, but it was not", tag)
129+
}
130+
}
131+
})
132+
}
133+
}

0 commit comments

Comments
 (0)