@@ -7,6 +7,54 @@ import (
77)
88
99func TestErrorPlaintextOutputFn (t * testing.T ) {
10+ t .Run ("with code and message" , func (t * testing.T ) {
11+ r := resource {"code" : "conflict" , "message" : "resource already exists" }
12+
13+ result := ErrorPlaintextOutputFn (r )
14+
15+ assert .Equal (t , "resource already exists (code: conflict)" , result )
16+ })
17+
18+ t .Run ("with only a message" , func (t * testing.T ) {
19+ r := resource {"message" : "something went wrong" }
20+
21+ result := ErrorPlaintextOutputFn (r )
22+
23+ assert .Equal (t , "something went wrong" , result )
24+ })
25+
26+ t .Run ("with only a code" , func (t * testing.T ) {
27+ r := resource {"code" : "not_found" , "message" : "" }
28+
29+ result := ErrorPlaintextOutputFn (r )
30+
31+ assert .Equal (t , "an error occurred (code: not_found)" , result )
32+ })
33+
34+ t .Run ("with neither code nor message" , func (t * testing.T ) {
35+ r := resource {}
36+
37+ result := ErrorPlaintextOutputFn (r )
38+
39+ assert .Equal (t , "unknown error occurred" , result )
40+ })
41+
42+ t .Run ("with empty message and nil code" , func (t * testing.T ) {
43+ r := resource {"message" : "" }
44+
45+ result := ErrorPlaintextOutputFn (r )
46+
47+ assert .Equal (t , "unknown error occurred" , result )
48+ })
49+
50+ t .Run ("with suggestion appends suggestion line" , func (t * testing.T ) {
51+ r := resource {"code" : "not_found" , "message" : "Not Found" , "suggestion" : "Try ldcli flags list." }
52+
53+ result := ErrorPlaintextOutputFn (r )
54+
55+ assert .Equal (t , "Not Found (code: not_found)\n Suggestion: Try ldcli flags list." , result )
56+ })
57+
1058 t .Run ("with a non-string message does not panic" , func (t * testing.T ) {
1159 r := resource {"message" : float64 (404 )}
1260
@@ -32,6 +80,91 @@ func TestErrorPlaintextOutputFn(t *testing.T) {
3280 })
3381}
3482
83+ func TestMultiplePlaintextOutputFn (t * testing.T ) {
84+ tests := map [string ]struct {
85+ resource resource
86+ expected string
87+ }{
88+ "with a name and key" : {
89+ resource : resource {
90+ "key" : "test-key" ,
91+ "name" : "test-name" ,
92+ },
93+ expected : "* test-name (test-key)" ,
94+ },
95+ "with only a key" : {
96+ resource : resource {
97+ "key" : "test-key" ,
98+ },
99+ expected : "* test-key" ,
100+ },
101+ "with an email and ID" : {
102+ resource : resource {
103+ "_id" : "test-id" ,
104+ "email" : "test-email" ,
105+ },
106+ expected : "* test-email (test-id)" ,
107+ },
108+ "without any valid field" : {
109+ resource : resource {
110+ "other" : "other-value" ,
111+ },
112+ expected : "* cannot read resource" ,
113+ },
114+ }
115+
116+ for name , tt := range tests {
117+ tt := tt
118+ t .Run (name , func (t * testing.T ) {
119+ out := MultiplePlaintextOutputFn (tt .resource )
120+
121+ assert .Equal (t , tt .expected , out )
122+ })
123+ }
124+ }
125+
126+ func TestConfigPlaintextOutputFn (t * testing.T ) {
127+ tests := map [string ]struct {
128+ resource resource
129+ expected string
130+ }{
131+ "with multiple keys sorts alphabetically" : {
132+ resource : resource {
133+ "zeta" : "last" ,
134+ "alpha" : "first" ,
135+ "mid" : "middle" ,
136+ },
137+ expected : "alpha: first\n mid: middle\n zeta: last" ,
138+ },
139+ "with single key" : {
140+ resource : resource {
141+ "key" : "value" ,
142+ },
143+ expected : "key: value" ,
144+ },
145+ "with empty resource" : {
146+ resource : resource {},
147+ expected : "" ,
148+ },
149+ "with non-string values" : {
150+ resource : resource {
151+ "count" : float64 (42 ),
152+ "enabled" : true ,
153+ },
154+ expected : "count: 42\n enabled: true" ,
155+ },
156+ }
157+
158+ for name , tt := range tests {
159+ tt := tt
160+ t .Run (name , func (t * testing.T ) {
161+ out := ConfigPlaintextOutputFn (tt .resource )
162+
163+ assert .Equal (t , tt .expected , out )
164+ })
165+ }
166+ }
167+
35168func TestSingularPlaintextOutputFn (t * testing.T ) {
36169 tests := map [string ]struct {
37170 resource resource
@@ -77,6 +210,32 @@ func TestSingularPlaintextOutputFn(t *testing.T) {
77210 },
78211 expected : "cannot read resource" ,
79212 },
213+ "with non-string name and key does not panic" : {
214+ resource : resource {
215+ "key" : float64 (123 ),
216+ "name" : true ,
217+ },
218+ expected : "true (123)" ,
219+ },
220+ "with non-string email and id does not panic" : {
221+ resource : resource {
222+ "_id" : float64 (999 ),
223+ "email" : float64 (42 ),
224+ },
225+ expected : "42 (999)" ,
226+ },
227+ "with non-string key only does not panic" : {
228+ resource : resource {
229+ "key" : float64 (456 ),
230+ },
231+ expected : "456" ,
232+ },
233+ "with non-string name only does not panic" : {
234+ resource : resource {
235+ "name" : float64 (789 ),
236+ },
237+ expected : "789" ,
238+ },
80239 }
81240
82241 for name , tt := range tests {
0 commit comments