Debian Codegraph @

dependencies/pkg/mod/github.com/goccy/[email protected]/printer/printer_test.go

raw
1 package printer_test
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/goccy/go-yaml/lexer"
8 "github.com/goccy/go-yaml/printer"
9 )
10
11 func Test_Printer(t *testing.T) {
12 yml := `---
13 text: aaaa
14 text2: aaaa
15 bbbb
16 cccc
17 dddd
18 eeee
19 text3: ffff
20 gggg
21 hhhh
22 iiii
23 jjjj
24 bool: true
25 number: 10
26 anchor: &x 1
27 alias: *x
28 `
29 t.Run("print starting from tokens[3]", func(t *testing.T) {
30 tokens := lexer.Tokenize(yml)
31 var p printer.Printer
32 actual := "\n" + p.PrintErrorToken(tokens[3], false)
33 expect := `
34 1 | ---
35 > 2 | text: aaaa
36 ^
37 3 | text2: aaaa
38 4 | bbbb
39 5 | cccc
40 6 | dddd
41 7 | eeee
42 8 | `
43 if actual != expect {
44 t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
45 }
46 })
47 t.Run("print starting from tokens[4]", func(t *testing.T) {
48 tokens := lexer.Tokenize(yml)
49 var p printer.Printer
50 actual := "\n" + p.PrintErrorToken(tokens[4], false)
51 expect := `
52 1 | ---
53 2 | text: aaaa
54 > 3 | text2: aaaa
55 4 | bbbb
56 5 | cccc
57 6 | dddd
58 7 | eeee
59 ^
60 `
61 if actual != expect {
62 t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
63 }
64 })
65 t.Run("print starting from tokens[6]", func(t *testing.T) {
66 tokens := lexer.Tokenize(yml)
67 var p printer.Printer
68 actual := "\n" + p.PrintErrorToken(tokens[6], false)
69 expect := `
70 1 | ---
71 2 | text: aaaa
72 > 3 | text2: aaaa
73 4 | bbbb
74 5 | cccc
75 6 | dddd
76 7 | eeee
77 ^
78 8 | text3: ffff
79 9 | gggg
80 10 | hhhh
81 11 | iiii
82 12 | jjjj
83 13 | `
84 if actual != expect {
85 t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
86 }
87 })
88 t.Run("print error token with document header", func(t *testing.T) {
89 tokens := lexer.Tokenize(`---
90 a:
91 b:
92 c:
93 d: e
94 f: g
95 h: i
96
97 ---
98 `)
99 expect := `
100 3 | b:
101 4 | c:
102 5 | d: e
103 > 6 | f: g
104 ^
105 7 | h: i
106 8 |
107 9 | ---`
108 var p printer.Printer
109 actual := "\n" + p.PrintErrorToken(tokens[12], false)
110 if actual != expect {
111 t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
112 }
113 })
114 t.Run("output with color", func(t *testing.T) {
115 t.Run("token6", func(t *testing.T) {
116 tokens := lexer.Tokenize(yml)
117 var p printer.Printer
118 t.Logf("\n%s", p.PrintErrorToken(tokens[6], true))
119 })
120 t.Run("token9", func(t *testing.T) {
121 tokens := lexer.Tokenize(yml)
122 var p printer.Printer
123 t.Logf("\n%s", p.PrintErrorToken(tokens[9], true))
124 })
125 t.Run("token12", func(t *testing.T) {
126 tokens := lexer.Tokenize(yml)
127 var p printer.Printer
128 t.Logf("\n%s", p.PrintErrorToken(tokens[12], true))
129 })
130 })
131 t.Run("print error message", func(t *testing.T) {
132 var p printer.Printer
133 src := "message"
134 msg := p.PrintErrorMessage(src, false)
135 if msg != src {
136 t.Fatal("unexpected result")
137 }
138 p.PrintErrorMessage(src, true)
139 })
140 }
141
142 func TestPrinter_Anchor(t *testing.T) {
143 expected := `
144 anchor: &x 1
145 alias: *x`
146 tokens := lexer.Tokenize(expected)
147 var p printer.Printer
148 got := p.PrintTokens(tokens)
149 if expected != got {
150 t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expected, got)
151 }
152 }
153
154 func Test_Printer_Multiline(t *testing.T) {
155 yml := `
156 text1: 'aaaa
157 bbbb
158 cccc'
159 text2: "ffff
160 gggg
161 hhhh"
162 text3: hello
163 `
164 tc := []struct {
165 token int
166 want string
167 }{
168 {
169 token: 2,
170 want: `
171 > 2 | text1: 'aaaa
172 3 | bbbb
173 4 | cccc'
174 ^
175 5 | text2: "ffff
176 6 | gggg
177 7 | hhhh"`,
178 },
179 {token: 3,
180 want: `
181 2 | text1: 'aaaa
182 3 | bbbb
183 4 | cccc'
184 > 5 | text2: "ffff
185 6 | gggg
186 7 | hhhh"
187 ^
188 8 | text3: hello`,
189 },
190 {token: 5,
191 want: `
192 2 | text1: 'aaaa
193 3 | bbbb
194 4 | cccc'
195 > 5 | text2: "ffff
196 6 | gggg
197 7 | hhhh"
198 ^
199 8 | text3: hello`,
200 },
201 {token: 6,
202 want: `
203 5 | text2: "ffff
204 6 | gggg
205 7 | hhhh"
206 > 8 | text3: hello
207 ^
208 `,
209 },
210 }
211 for _, tt := range tc {
212 name := fmt.Sprintf("print starting from tokens[%d]", tt.token)
213 t.Run(name, func(t *testing.T) {
214 tokens := lexer.Tokenize(yml)
215 var p printer.Printer
216 got := "\n" + p.PrintErrorToken(tokens[tt.token], false)
217 want := tt.want
218 if got != want {
219 t.Fatalf("PrintErrorToken() got: %s\n want:%s\n", want, got)
220 }
221 })
222 }
223 }