Skip to content

Commit 79f9f75

Browse files
chore(cli): additional test cases for ShowJSONIterator
1 parent c37cb3e commit 79f9f75

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

pkg/cmd/cmdutil_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
1313
"github.com/tidwall/gjson"
14+
15+
"github.com/beeper/desktop-api-cli/internal/jsonview"
1416
)
1517

1618
func TestStreamOutput(t *testing.T) {
@@ -190,3 +192,79 @@ func TestFormatJSON(t *testing.T) {
190192
require.Equal(t, `{"id":"abc123"}`+"\n", string(formatted))
191193
})
192194
}
195+
196+
func TestShowJSONIterator(t *testing.T) {
197+
t.Parallel()
198+
199+
t.Run("RawMultipleItems", func(t *testing.T) {
200+
t.Parallel()
201+
202+
iter := &sliceIterator[map[string]any]{items: []map[string]any{
203+
{"id": "abc", "name": "first"},
204+
{"id": "def", "name": "second"},
205+
}}
206+
captured := captureShowJSONIterator(t, iter, "raw", "", -1)
207+
assert.Equal(t, `{"id":"abc","name":"first"}`+"\n"+`{"id":"def","name":"second"}`+"\n", captured)
208+
})
209+
210+
t.Run("RawWithTransform", func(t *testing.T) {
211+
t.Parallel()
212+
213+
iter := &sliceIterator[map[string]any]{items: []map[string]any{
214+
{"id": "abc", "name": "first"},
215+
{"id": "def", "name": "second"},
216+
}}
217+
captured := captureShowJSONIterator(t, iter, "raw", "id", -1)
218+
assert.Equal(t, `"abc"`+"\n"+`"def"`+"\n", captured)
219+
})
220+
221+
t.Run("LimitItems", func(t *testing.T) {
222+
t.Parallel()
223+
224+
iter := &sliceIterator[map[string]any]{items: []map[string]any{
225+
{"id": "abc"},
226+
{"id": "def"},
227+
{"id": "ghi"},
228+
}}
229+
captured := captureShowJSONIterator(t, iter, "raw", "", 2)
230+
assert.Equal(t, `{"id":"abc"}`+"\n"+`{"id":"def"}`+"\n", captured)
231+
})
232+
}
233+
234+
// sliceIterator is a simple iterator over a slice for testing.
235+
type sliceIterator[T any] struct {
236+
index int
237+
items []T
238+
}
239+
240+
func (it *sliceIterator[T]) Next() bool {
241+
it.index++
242+
return it.index <= len(it.items)
243+
}
244+
245+
func (it *sliceIterator[T]) Current() T {
246+
return it.items[it.index-1]
247+
}
248+
249+
func (it *sliceIterator[T]) Err() error {
250+
return nil
251+
}
252+
253+
var _ jsonview.Iterator[any] = (*sliceIterator[any])(nil)
254+
255+
// captureShowJSONIterator runs ShowJSONIterator and captures the output written to a file.
256+
func captureShowJSONIterator[T any](t *testing.T, iter jsonview.Iterator[T], format, transform string, itemsToDisplay int64) string {
257+
t.Helper()
258+
259+
r, w, err := os.Pipe()
260+
require.NoError(t, err)
261+
defer r.Close()
262+
263+
err = ShowJSONIterator(w, "test", iter, format, transform, itemsToDisplay)
264+
w.Close()
265+
require.NoError(t, err)
266+
267+
var buf bytes.Buffer
268+
_, _ = buf.ReadFrom(r)
269+
return buf.String()
270+
}

0 commit comments

Comments
 (0)