summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/github.com/goccy/go-yaml@v1.9.6/printer/printer_test.go
blob: 2afa74f1830f8bef0c54450a29c089271d739f75 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package printer_test

import (
	"fmt"
	"testing"

	"github.com/goccy/go-yaml/lexer"
	"github.com/goccy/go-yaml/printer"
)

func Test_Printer(t *testing.T) {
	yml := `---
text: aaaa
text2: aaaa
 bbbb
 cccc
 dddd
 eeee
text3: ffff
 gggg
 hhhh
 iiii
 jjjj
bool: true
number: 10
anchor: &x 1
alias: *x
`
	t.Run("print starting from tokens[3]", func(t *testing.T) {
		tokens := lexer.Tokenize(yml)
		var p printer.Printer
		actual := "\n" + p.PrintErrorToken(tokens[3], false)
		expect := `
   1 | ---
>  2 | text: aaaa
             ^
   3 | text2: aaaa
   4 |  bbbb
   5 |  cccc
   6 |  dddd
   7 |  eeee
   8 | `
		if actual != expect {
			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
		}
	})
	t.Run("print starting from tokens[4]", func(t *testing.T) {
		tokens := lexer.Tokenize(yml)
		var p printer.Printer
		actual := "\n" + p.PrintErrorToken(tokens[4], false)
		expect := `
   1 | ---
   2 | text: aaaa
>  3 | text2: aaaa
   4 |  bbbb
   5 |  cccc
   6 |  dddd
   7 |  eeee
       ^
`
		if actual != expect {
			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
		}
	})
	t.Run("print starting from tokens[6]", func(t *testing.T) {
		tokens := lexer.Tokenize(yml)
		var p printer.Printer
		actual := "\n" + p.PrintErrorToken(tokens[6], false)
		expect := `
   1 | ---
   2 | text: aaaa
>  3 | text2: aaaa
   4 |  bbbb
   5 |  cccc
   6 |  dddd
   7 |  eeee
              ^
   8 | text3: ffff
   9 |  gggg
  10 |  hhhh
  11 |  iiii
  12 |  jjjj
  13 | `
		if actual != expect {
			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
		}
	})
	t.Run("print error token with document header", func(t *testing.T) {
		tokens := lexer.Tokenize(`---
a:
 b:
  c:
   d: e
   f: g
   h: i

---
`)
		expect := `
   3 |  b:
   4 |   c:
   5 |    d: e
>  6 |    f: g
             ^
   7 |    h: i
   8 | 
   9 | ---`
		var p printer.Printer
		actual := "\n" + p.PrintErrorToken(tokens[12], false)
		if actual != expect {
			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
		}
	})
	t.Run("output with color", func(t *testing.T) {
		t.Run("token6", func(t *testing.T) {
			tokens := lexer.Tokenize(yml)
			var p printer.Printer
			t.Logf("\n%s", p.PrintErrorToken(tokens[6], true))
		})
		t.Run("token9", func(t *testing.T) {
			tokens := lexer.Tokenize(yml)
			var p printer.Printer
			t.Logf("\n%s", p.PrintErrorToken(tokens[9], true))
		})
		t.Run("token12", func(t *testing.T) {
			tokens := lexer.Tokenize(yml)
			var p printer.Printer
			t.Logf("\n%s", p.PrintErrorToken(tokens[12], true))
		})
	})
	t.Run("print error message", func(t *testing.T) {
		var p printer.Printer
		src := "message"
		msg := p.PrintErrorMessage(src, false)
		if msg != src {
			t.Fatal("unexpected result")
		}
		p.PrintErrorMessage(src, true)
	})
}

func TestPrinter_Anchor(t *testing.T) {
	expected := `
anchor: &x 1
alias: *x`
	tokens := lexer.Tokenize(expected)
	var p printer.Printer
	got := p.PrintTokens(tokens)
	if expected != got {
		t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expected, got)
	}
}

func Test_Printer_Multiline(t *testing.T) {
	yml := `
text1: 'aaaa
 bbbb
 cccc'
text2: "ffff
 gggg
 hhhh"
text3: hello
`
	tc := []struct {
		token int
		want  string
	}{
		{
			token: 2,
			want: `
>  2 | text1: 'aaaa
   3 |  bbbb
   4 |  cccc'
              ^
   5 | text2: "ffff
   6 |  gggg
   7 |  hhhh"`,
		},
		{token: 3,
			want: `
   2 | text1: 'aaaa
   3 |  bbbb
   4 |  cccc'
>  5 | text2: "ffff
   6 |  gggg
   7 |  hhhh"
       ^
   8 | text3: hello`,
		},
		{token: 5,
			want: `
   2 | text1: 'aaaa
   3 |  bbbb
   4 |  cccc'
>  5 | text2: "ffff
   6 |  gggg
   7 |  hhhh"
              ^
   8 | text3: hello`,
		},
		{token: 6,
			want: `
   5 | text2: "ffff
   6 |  gggg
   7 |  hhhh"
>  8 | text3: hello
       ^
`,
		},
	}
	for _, tt := range tc {
		name := fmt.Sprintf("print starting from tokens[%d]", tt.token)
		t.Run(name, func(t *testing.T) {
			tokens := lexer.Tokenize(yml)
			var p printer.Printer
			got := "\n" + p.PrintErrorToken(tokens[tt.token], false)
			want := tt.want
			if got != want {
				t.Fatalf("PrintErrorToken() got: %s\n want:%s\n", want, got)
			}
		})
	}
}