Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
146 lines (119 loc) · 3.26 KB
/
Copy pathmain_test.go
File metadata and controls
146 lines (119 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"io"
"os"
"testing"
"time"
tea "charm.land/bubbletea/v2"
"github.com/charmbracelet/colorprofile"
"github.com/charmbracelet/x/exp/teatest/v2"
"github.com/stretchr/testify/require"
"github.com/antonmedv/fx/internal/jsonx"
)
type options struct {
showSizes bool
showLineNumbers bool
height int // terminal rows; 0 means 40
}
func prepare(t *testing.T, opts ...options) *teatest.TestModel {
file, err := os.Open("testdata/example.json")
require.NoError(t, err)
json, err := io.ReadAll(file)
require.NoError(t, err)
head, err := jsonx.Parse(json)
require.NoError(t, err)
m := &model{
viewState: viewState{
top: head,
head: head,
bottom: head,
totalLines: head.Bottom().LineNumber,
search: newSearch(),
},
eof: true,
wrap: true,
showCursor: true,
searchInput: newInput(),
commandInput: newInput(),
}
height := 40
if len(opts) > 0 {
m.showSizes = opts[0].showSizes
m.showLineNumbers = opts[0].showLineNumbers
if opts[0].height > 0 {
height = opts[0].height
}
}
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(80, height),
teatest.WithProgramOptions(
tea.WithColorProfile(colorprofile.ANSI),
// The renderer picks cursor moves by TERM; pin it so the golden
// files match on CI, where TERM is unset.
tea.WithEnvironment([]string{"TERM=xterm-256color"}),
),
)
return tm
}
func read(t *testing.T, tm *teatest.TestModel) []byte {
var out []byte
teatest.WaitFor(t,
tm.Output(),
func(b []byte) bool {
out = b
return bytes.Contains(b, []byte("{"))
},
teatest.WithCheckInterval(time.Millisecond*100),
teatest.WithDuration(time.Second),
)
return out
}
func TestOutput(t *testing.T) {
tm := prepare(t)
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(press("q"))
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
// TestCursorValueDecodesJsonEscapesInWrappedArrayString verifies that preview
// and print decode JSON string escapes even after the viewer wraps an array item.
func TestCursorValueDecodesJsonEscapesInWrappedArrayString(t *testing.T) {
head, err := jsonx.Parse([]byte(`["before\ud83d\udd55\nsecond line after"]`))
require.NoError(t, err)
jsonx.Wrap(head, 12)
require.NotNil(t, head.Next)
require.NotNil(t, head.Next.ChunkEnd)
m := &model{
viewState: viewState{
head: head,
cursor: 1,
},
}
require.Equal(t, "before\U0001F555\nsecond line after", m.cursorValue())
m.cursor = 2
require.Equal(t, "before\U0001F555\nsecond line after", m.cursorValue())
}
func TestNavigation(t *testing.T) {
tm := prepare(t)
tm.Send(press("down"))
tm.Send(press("down"))
tm.Send(press("down"))
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(press("q"))
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestCollapseRecursive(t *testing.T) {
tm := prepare(t)
tm.Send(press("shift+left"))
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(press("q"))
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
func TestCollapseRecursiveWithSizes(t *testing.T) {
tm := prepare(t, options{showSizes: true})
tm.Send(press("shift+left"))
teatest.RequireEqualOutput(t, read(t, tm))
tm.Send(press("q"))
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}