Debian Codegraph @

fmt_test.go

raw ◀ ▶
1 Debian patch touch this file
1 package tblfmt
2
3 import (
4 "encoding/json"
5 "reflect"
6 "regexp"
7 "slices"
8 "strconv"
9 "strings"
10 "testing"
11
12 runewidth "github.com/mattn/go-runewidth"
13 )
14
15 func TestTabwidthCalc(t *testing.T) {
16 tests := []struct {
17 s string
18 offset int
19 tab int
20 exp int
21 }{
22 {"", 0, 8, 0},
23 {" ", 0, 8, 1},
24 {" ", 0, 8, 4},
25 {"\u8888", 0, 8, 2},
26 {"\t", 0, 8, 8},
27 {"\t\t", 0, 8, 16}, // 5
28 {"\t\t ", 0, 8, 17},
29 {" \t\t ", 0, 8, 17},
30 {" \t\t\t ", 0, 8, 25},
31 {"foo\tbar\t", 0, 8, 16},
32 {"\t\t\u8888", 0, 8, 18}, // 10
33 {"\u8888\t\u8888", 0, 8, 10},
34 {"\u8888\t\u8888\t", 0, 8, 16},
35 {"", 1, 8, 0}, // 13
36 {"\t", 1, 4, 3},
37 {" \t", 1, 4, 3},
38 {" \t ", 1, 4, 4},
39 {"\u8888\t\u8888\t", 1, 4, 7},
40 /*
41 ---xxxxxxxxx (width == 9)
42 | 袈 袈 |
43 */
44 {"\u8888\t\t\u8888\t", 3, 2, 9}, // 18
45 /*
46 --------------xxxxxxxxxxxxxxxxxxxxxxxxxxxx (width == 28)
47 | 袈 袈 袈|
48 */
49 {"\u8888\t\u8888\t\t\u8888", 14, 8, 28}, // 19
50 {"袈 袈 袈", 14, 8, 28},
51 }
52 for i, test := range tests {
53 tabs, w := tabpositions(test.s)
54 w += tabwidth(tabs, test.offset, test.tab)
55 if test.exp != w {
56 t.Errorf("test %d %q expected tabwidth(%v, %d, %d) = %d, got: %d", i, test.s, tabs, test.offset, test.tab, test.exp, w)
57 }
58 }
59 }
60
61 var tabRE = regexp.MustCompile("\t")
62
63 // tabpositions returns a list of tab positions in s.
64 func tabpositions(s string) ([][2]int, int) {
65 var tabs [][2]int
66 var last int
67 for _, m := range tabRE.FindAllStringIndex(s, -1) {
68 tabs = append(tabs, [2]int{m[0], runewidth.StringWidth(s[last:m[0]])})
69 last = m[0] + 1
70 }
71 return tabs, runewidth.StringWidth(s[last:])
72 }
73
74 func TestFormatBytesTabs(t *testing.T) {
75 tests := []escTest{
76 v("", 0),
77 v("\u8888\t\u8888", 4),
78 v(" \u8888 \t \u8888 ", 8),
79 }
80 for i, test := range tests {
81 v := FormatBytes([]byte(test.s), nil, 0, false, false, 0, 0)
82 if !reflect.DeepEqual(v, test.exp) {
83 t.Errorf("test %d %q expected %v, got: %v", i, test.s, test.exp, v)
84 width := runewidth.StringWidth(string(v.Buf))
85 if v.Width != width {
86 t.Errorf("test %d %q expected width %d, got: %d", i, test.s, width, v.Width)
87 }
88 if width != test.check {
89 t.Errorf("test %d %q expected check width %d, got: %d", i, test.s, test.check, width)
90 }
91 }
92 }
93 }
94
95 func TestFormatBytesComplex(t *testing.T) {
96 s := `{
97 "2011": "Team Garmin - Cervelo",
98 "2012": "AA Drink - Leontien.nl",
99 "2013": "Boels-Dolmans Cycling Team",
100 "2015": "Boels-Dolmans"
101 }`
102 v := FormatBytes([]byte(s), nil, 0, false, false, 0, 0)
103 if w := v.MaxWidth(0, 8); w != 39 {
104 t.Errorf("expected width of 39, got: %d", w)
105 }
106 }
107
108 func TestFormatJSON(t *testing.T) {
•109 t.Skip("Test fails with Go 1.27 due to different JSON handling")
110 s := strings.Join(
111 []string{
112 "\a",
113 "\b",
114 "\f",
115 "\n",
116 "\r",
117 "\t",
118 "\x1a",
119 "\x2b",
120 "\x3f",
121 "\\",
122 " ",
123 "\x9f",
124 "\xaf",
125 "\xff",
126 "\u1998",
127 "👀",
128 "🤰",
129 "foo",
130 "15\u00f8C",
131 },
132 ";",
133 )
134 exp, err := json.Marshal(s)
135 if err != nil {
136 t.Fatalf("expected no error, got: %v", err)
137 }
138 exp = exp[1 : len(exp)-1]
139 t.Logf("exp: %q", string(exp))
140 v := FormatBytes([]byte(s), nil, 0, true, false, 0, 0)
141 t.Logf("v : %q", v)
142 if b := []byte(v.String()); !slices.Equal(b, exp) {
143 t.Errorf("\nexpected:\n%q\ngot:\n%q", string(exp), string(b))
144 }
145 }
146
147 func TestFormatBytesRaw(t *testing.T) {
148 tests := []struct {
149 s string
150 exp string
151 }{
152 {"", ""},
153 {"a", "a"},
154 {" ", `" "`},
155 {" ", `" "`},
156 {" ", `" "`},
157 {"\n", "\"\n\""},
158 {"\t", "\"\t\""},
159 {",", "\",\""},
160 {",\t", "\",\t\""},
161 {",\t\"", "\",\t\"\"\""},
162 }
163 for i, test := range tests {
164 v := FormatBytes([]byte(test.s), nil, 0, false, true, ',', '"')
165 buf := v.Buf
166 if v.Quoted {
167 buf = append([]byte{'"'}, append(buf, '"')...)
168 }
169 if string(buf) != test.exp {
170 t.Errorf("test %d %q expected %q == %q", i, test.s, string(buf), test.exp)
171 }
172 }
173 }
174
175 type escTest struct {
176 s string
177 exp *Value
178 check int
179 }
180
181 func quote(s string) string {
182 s = strconv.Quote(s)
183 s = s[1 : len(s)-1]
184 s = strings.ReplaceAll(s, `\t`, "\t")
185 s = strings.ReplaceAll(s, `\n`, "\n")
186 return s
187 }
188
189 func v(s string, check int) escTest {
190 c := quote(s)
191 tabs, width := tabpositions(c)
192 var buf []byte
193 if len(c) != 0 {
194 buf = []byte(c)
195 }
196 v := &Value{
197 Buf: buf,
198 Tabs: [][][2]int{tabs},
199 Width: width,
200 }
201 return escTest{s, v, check}
202 }