1package printer_test
2
3import (
4	"testing"
5
6	"github.com/goccy/go-yaml/lexer"
7	"github.com/goccy/go-yaml/printer"
8)
9
10func Test_Printer(t *testing.T) {
11	yml := `---
12text: aaaa
13text2: aaaa
14 bbbb
15 cccc
16 dddd
17 eeee
18text3: ffff
19 gggg
20 hhhh
21 iiii
22 jjjj
23bool: true
24number: 10
25anchor: &x 1
26alias: *x
27`
28	t.Run("print starting from tokens[3]", func(t *testing.T) {
29		tokens := lexer.Tokenize(yml)
30		var p printer.Printer
31		actual := "\n" + p.PrintErrorToken(tokens[3], false)
32		expect := `
33   1 | ---
34>  2 | text: aaaa
35             ^
36   3 | text2: aaaa
37   4 |  bbbb
38   5 |  cccc
39   6 |  dddd
40   7 |  eeee
41   8 | `
42		if actual != expect {
43			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
44		}
45	})
46	t.Run("print stargin from tokens[4]", func(t *testing.T) {
47		tokens := lexer.Tokenize(yml)
48		var p printer.Printer
49		actual := "\n" + p.PrintErrorToken(tokens[4], false)
50		expect := `
51   1 | ---
52   2 | text: aaaa
53>  3 | text2: aaaa
54   4 |  bbbb
55   5 |  cccc
56   6 |  dddd
57   7 |  eeee
58       ^
59`
60		if actual != expect {
61			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
62		}
63	})
64	t.Run("print starting from tokens[6]", func(t *testing.T) {
65		tokens := lexer.Tokenize(yml)
66		var p printer.Printer
67		actual := "\n" + p.PrintErrorToken(tokens[6], false)
68		expect := `
69   1 | ---
70   2 | text: aaaa
71>  3 | text2: aaaa
72   4 |  bbbb
73   5 |  cccc
74   6 |  dddd
75   7 |  eeee
76              ^
77   8 | text3: ffff
78   9 |  gggg
79  10 |  hhhh
80  11 |  iiii
81  12 |  jjjj
82  13 | `
83		if actual != expect {
84			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
85		}
86	})
87	t.Run("print error token with document header", func(t *testing.T) {
88		tokens := lexer.Tokenize(`---
89a:
90 b:
91  c:
92   d: e
93   f: g
94   h: i
95
96---
97`)
98		expect := `
99   3 |  b:
100   4 |   c:
101   5 |    d: e
102>  6 |    f: g
103             ^
104   7 |    h: i
105   8 |
106   9 | ---`
107		var p printer.Printer
108		actual := "\n" + p.PrintErrorToken(tokens[12], false)
109		if actual != expect {
110			t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expect, actual)
111		}
112	})
113	t.Run("output with color", func(t *testing.T) {
114		t.Run("token6", func(t *testing.T) {
115			tokens := lexer.Tokenize(yml)
116			var p printer.Printer
117			t.Logf("\n%s", p.PrintErrorToken(tokens[6], true))
118		})
119		t.Run("token9", func(t *testing.T) {
120			tokens := lexer.Tokenize(yml)
121			var p printer.Printer
122			t.Logf("\n%s", p.PrintErrorToken(tokens[9], true))
123		})
124		t.Run("token12", func(t *testing.T) {
125			tokens := lexer.Tokenize(yml)
126			var p printer.Printer
127			t.Logf("\n%s", p.PrintErrorToken(tokens[12], true))
128		})
129	})
130	t.Run("print error message", func(t *testing.T) {
131		var p printer.Printer
132		src := "message"
133		msg := p.PrintErrorMessage(src, false)
134		if msg != src {
135			t.Fatal("unexpected result")
136		}
137		p.PrintErrorMessage(src, true)
138	})
139}
140
141func TestPrinter_Anchor(t *testing.T) {
142	expected := `
143anchor: &x 1
144alias: *x`
145	tokens := lexer.Tokenize(expected)
146	var p printer.Printer
147	got := p.PrintTokens(tokens)
148	if expected != got {
149		t.Fatalf("unexpected output: expect:[%s]\n actual:[%s]", expected, got)
150	}
151}
152