| 1 |
package tblfmt |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/xo/tblfmt/internal" |
| 10 |
) |
| 11 |
|
| 12 |
func TestEncodeTableAll(t *testing.T) { |
| 13 |
t.Parallel() |
| 14 |
exp := ` my table |
| 15 |
╔══════════════════════╦══════════════════════════╦═══╗ |
| 16 |
║ author_id ║ name ║ z ║ |
| 17 |
╠══════════════════════╬══════════════════════════╬═══╣ |
| 18 |
║ 14 ║ a b c d ║ x ║ |
| 19 |
║ 15 ║ aoeu ↵║ ║ |
| 20 |
║ ║ test ↵║ ║ |
| 21 |
║ ║ ║ ║ |
| 22 |
╚══════════════════════╩══════════════════════════╩═══╝ |
| 23 |
(2 rows) |
| 24 |
|
| 25 |
my table |
| 26 |
╔══════════════════════╦═══════════════════════════╦════════════════════════╗ |
| 27 |
║ author_id ║ name ║ z ║ |
| 28 |
╠══════════════════════╬═══════════════════════════╬════════════════════════╣ |
| 29 |
║ 16 ║ foo\bbar ║ ║ |
| 30 |
║ 17 ║ 袈 袈 袈 ║ ║ |
| 31 |
║ 18 ║ a b \r ↵║ a ↵║ |
| 32 |
║ ║ a ║ ║ |
| 33 |
║ 19 ║ 袈 袈 袈↵║ ║ |
| 34 |
║ ║ ║ ║ |
| 35 |
║ 20 ║ javascript ║ { ↵║ |
| 36 |
║ ║ ║ "test21": "a value",↵║ |
| 37 |
║ ║ ║ "test22": "foo\bbar"↵║ |
| 38 |
║ ║ ║ } ║ |
| 39 |
║ 23 ║ slice ║ [ ↵║ |
| 40 |
║ ║ ║ "a", ↵║ |
| 41 |
║ ║ ║ "b" ↵║ |
| 42 |
║ ║ ║ ] ║ |
| 43 |
╚══════════════════════╩═══════════════════════════╩════════════════════════╝ |
| 44 |
(6 rows) |
| 45 |
|
| 46 |
my table |
| 47 |
╔══════════════════════╦═══════════════════════════╦════════════════════════╗ |
| 48 |
║ author_id ║ name ║ z ║ |
| 49 |
╠══════════════════════╬═══════════════════════════╬════════════════════════╣ |
| 50 |
║ 38 ║ a b c d ║ x ║ |
| 51 |
║ 39 ║ aoeu ↵║ ║ |
| 52 |
║ ║ test ↵║ ║ |
| 53 |
║ ║ ║ ║ |
| 54 |
║ 40 ║ foo\bbar ║ ║ |
| 55 |
║ 41 ║ 袈 袈 袈 ║ ║ |
| 56 |
║ 42 ║ a b \r ↵║ a ↵║ |
| 57 |
║ ║ a ║ ║ |
| 58 |
║ 43 ║ 袈 袈 袈↵║ ║ |
| 59 |
║ ║ ║ ║ |
| 60 |
║ 44 ║ javascript ║ { ↵║ |
| 61 |
║ ║ ║ "test45": "a value",↵║ |
| 62 |
║ ║ ║ "test46": "foo\bbar"↵║ |
| 63 |
║ ║ ║ } ║ |
| 64 |
║ 47 ║ slice ║ [ ↵║ |
| 65 |
║ ║ ║ "a", ↵║ |
| 66 |
║ ║ ║ "b" ↵║ |
| 67 |
║ ║ ║ ] ║ |
| 68 |
╚══════════════════════╩═══════════════════════════╩════════════════════════╝ |
| 69 |
(8 rows) |
| 70 |
` |
| 71 |
buf := new(bytes.Buffer) |
| 72 |
if err := EncodeTableAll(buf, internal.Multi(), WithBorder(2), WithLineStyle(UnicodeDoubleLineStyle()), WithTitle("my table"), WithWidths(20, 20)); err != nil { |
| 73 |
t.Fatalf("expected no error, got: %v", err) |
| 74 |
} |
| 75 |
actual := buf.String() |
| 76 |
if actual != exp { |
| 77 |
t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestEncodeJSONAll(t *testing.T) { |
| 82 |
t.Parallel() |
| 83 |
exp := `[{"author_id":14,"name":"a\tb\tc\td","z":"x"},{"author_id":15,"name":"aoeu\ntest\n","z":null}], |
| 84 |
[{"author_id":16,"name":"foo\bbar","z":null},{"author_id":17,"name":"袈\t袈\t\t袈","z":null},{"author_id":18,"name":"a\tb\t\r\n\ta","z":"a\n"},{"author_id":19,"name":"袈\t袈\t\t袈\n","z":null},{"author_id":20,"name":"javascript","z":{ |
| 85 |
"test21": "a value", |
| 86 |
"test22": "foo\bbar" |
| 87 |
}},{"author_id":23,"name":"slice","z":[ |
| 88 |
"a", |
| 89 |
"b" |
| 90 |
]}], |
| 91 |
[{"author_id":38,"name":"a\tb\tc\td","z":"x"},{"author_id":39,"name":"aoeu\ntest\n","z":null},{"author_id":40,"name":"foo\bbar","z":null},{"author_id":41,"name":"袈\t袈\t\t袈","z":null},{"author_id":42,"name":"a\tb\t\r\n\ta","z":"a\n"},{"author_id":43,"name":"袈\t袈\t\t袈\n","z":null},{"author_id":44,"name":"javascript","z":{ |
| 92 |
"test45": "a value", |
| 93 |
"test46": "foo\bbar" |
| 94 |
}},{"author_id":47,"name":"slice","z":[ |
| 95 |
"a", |
| 96 |
"b" |
| 97 |
]}] |
| 98 |
` |
| 99 |
buf := new(bytes.Buffer) |
| 100 |
if err := EncodeJSONAll(buf, internal.Multi()); err != nil { |
| 101 |
t.Fatalf("expected no error, got: %v", err) |
| 102 |
} |
| 103 |
actual := buf.String() |
| 104 |
if actual != exp { |
| 105 |
t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
func TestEncodeTemplateAll(t *testing.T) { |
| 110 |
t.Parallel() |
| 111 |
exp := `Row 0: |
| 112 |
author_id = "14" |
| 113 |
name = "a b c d" |
| 114 |
z = "x" |
| 115 |
Row 1: |
| 116 |
author_id = "15" |
| 117 |
name = "aoeu |
| 118 |
test |
| 119 |
" |
| 120 |
z = "<nil>" |
| 121 |
|
| 122 |
Row 0: |
| 123 |
author_id = "16" |
| 124 |
name = "foo\bbar" |
| 125 |
z = "<nil>" |
| 126 |
Row 1: |
| 127 |
author_id = "17" |
| 128 |
name = "袈 袈 袈" |
| 129 |
z = "<nil>" |
| 130 |
Row 2: |
| 131 |
author_id = "18" |
| 132 |
name = "a b \r |
| 133 |
a" |
| 134 |
z = "a |
| 135 |
" |
| 136 |
Row 3: |
| 137 |
author_id = "19" |
| 138 |
name = "袈 袈 袈 |
| 139 |
" |
| 140 |
z = "<nil>" |
| 141 |
Row 4: |
| 142 |
author_id = "20" |
| 143 |
name = "javascript" |
| 144 |
z = "{ |
| 145 |
"test21": "a value", |
| 146 |
"test22": "foo\bbar" |
| 147 |
}" |
| 148 |
Row 5: |
| 149 |
author_id = "23" |
| 150 |
name = "slice" |
| 151 |
z = "[ |
| 152 |
"a", |
| 153 |
"b" |
| 154 |
]" |
| 155 |
|
| 156 |
Row 0: |
| 157 |
author_id = "38" |
| 158 |
name = "a b c d" |
| 159 |
z = "x" |
| 160 |
Row 1: |
| 161 |
author_id = "39" |
| 162 |
name = "aoeu |
| 163 |
test |
| 164 |
" |
| 165 |
z = "<nil>" |
| 166 |
Row 2: |
| 167 |
author_id = "40" |
| 168 |
name = "foo\bbar" |
| 169 |
z = "<nil>" |
| 170 |
Row 3: |
| 171 |
author_id = "41" |
| 172 |
name = "袈 袈 袈" |
| 173 |
z = "<nil>" |
| 174 |
Row 4: |
| 175 |
author_id = "42" |
| 176 |
name = "a b \r |
| 177 |
a" |
| 178 |
z = "a |
| 179 |
" |
| 180 |
Row 5: |
| 181 |
author_id = "43" |
| 182 |
name = "袈 袈 袈 |
| 183 |
" |
| 184 |
z = "<nil>" |
| 185 |
Row 6: |
| 186 |
author_id = "44" |
| 187 |
name = "javascript" |
| 188 |
z = "{ |
| 189 |
"test45": "a value", |
| 190 |
"test46": "foo\bbar" |
| 191 |
}" |
| 192 |
Row 7: |
| 193 |
author_id = "47" |
| 194 |
name = "slice" |
| 195 |
z = "[ |
| 196 |
"a", |
| 197 |
"b" |
| 198 |
]" |
| 199 |
` |
| 200 |
buf := new(bytes.Buffer) |
| 201 |
if err := EncodeTemplateAll(buf, internal.Multi(), WithExecutor(testWriteTemplateTo), WithEmpty("<nil>")); err != nil { |
| 202 |
t.Fatalf("expected no error, got: %v", err) |
| 203 |
} |
| 204 |
actual := buf.String() |
| 205 |
if actual != exp { |
| 206 |
t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
func TestEncodeUnalignedAll(t *testing.T) { |
| 211 |
t.Parallel() |
| 212 |
exp := `author_id|name|z |
| 213 |
14|a b c d|x |
| 214 |
15|aoeu |
| 215 |
test |
| 216 |
| |
| 217 |
(2 rows) |
| 218 |
|
| 219 |
author_id|name|z |
| 220 |
16|foo` + "\b" + `bar| |
| 221 |
17|袈 袈 袈| |
| 222 |
18|a b ` + "\r" + ` |
| 223 |
a|a |
| 224 |
|
| 225 |
19|袈 袈 袈 |
| 226 |
| |
| 227 |
20|javascript|{ |
| 228 |
"test21": "a value", |
| 229 |
"test22": "foo\bbar" |
| 230 |
} |
| 231 |
23|slice|[ |
| 232 |
"a", |
| 233 |
"b" |
| 234 |
] |
| 235 |
(6 rows) |
| 236 |
|
| 237 |
author_id|name|z |
| 238 |
38|a b c d|x |
| 239 |
39|aoeu |
| 240 |
test |
| 241 |
| |
| 242 |
40|foo` + "\b" + `bar| |
| 243 |
41|袈 袈 袈| |
| 244 |
42|a b ` + "\r" + ` |
| 245 |
a|a |
| 246 |
|
| 247 |
43|袈 袈 袈 |
| 248 |
| |
| 249 |
44|javascript|{ |
| 250 |
"test45": "a value", |
| 251 |
"test46": "foo\bbar" |
| 252 |
} |
| 253 |
47|slice|[ |
| 254 |
"a", |
| 255 |
"b" |
| 256 |
] |
| 257 |
(8 rows) |
| 258 |
` |
| 259 |
buf := new(bytes.Buffer) |
| 260 |
if err := EncodeUnalignedAll(buf, internal.Multi()); err != nil { |
| 261 |
t.Fatalf("expected no error, got: %v", err) |
| 262 |
} |
| 263 |
actual := buf.String() |
| 264 |
if actual != exp { |
| 265 |
t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual) |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
func TestEncodeCSVAll(t *testing.T) { |
| 270 |
t.Parallel() |
| 271 |
exp := `author_id,name,z |
| 272 |
14,"a b c d",x |
| 273 |
15,"aoeu |
| 274 |
test |
| 275 |
", |
| 276 |
|
| 277 |
author_id,name,z |
| 278 |
16,foo` + "\b" + `bar, |
| 279 |
17,"袈 袈 袈", |
| 280 |
18,"a b ` + "\r" + ` |
| 281 |
a","a |
| 282 |
" |
| 283 |
19,"袈 袈 袈 |
| 284 |
", |
| 285 |
20,javascript,"{ |
| 286 |
""test21"": ""a value"", |
| 287 |
""test22"": ""foo\bbar"" |
| 288 |
}" |
| 289 |
23,slice,"[ |
| 290 |
""a"", |
| 291 |
""b"" |
| 292 |
]" |
| 293 |
|
| 294 |
author_id,name,z |
| 295 |
38,"a b c d",x |
| 296 |
39,"aoeu |
| 297 |
test |
| 298 |
", |
| 299 |
40,foo` + "\b" + `bar, |
| 300 |
41,"袈 袈 袈", |
| 301 |
42,"a b ` + "\r" + ` |
| 302 |
a","a |
| 303 |
" |
| 304 |
43,"袈 袈 袈 |
| 305 |
", |
| 306 |
44,javascript,"{ |
| 307 |
""test45"": ""a value"", |
| 308 |
""test46"": ""foo\bbar"" |
| 309 |
}" |
| 310 |
47,slice,"[ |
| 311 |
""a"", |
| 312 |
""b"" |
| 313 |
]" |
| 314 |
` |
| 315 |
buf := new(bytes.Buffer) |
| 316 |
if err := EncodeCSVAll(buf, internal.Multi()); err != nil { |
| 317 |
t.Fatalf("expected no error, got: %v", err) |
| 318 |
} |
| 319 |
actual := buf.String() |
| 320 |
if actual != exp { |
| 321 |
t.Errorf("expected:\n%q\n---\ngot:\n%q", exp, actual) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func testWriteTemplateTo(w io.Writer, tpl *Template) error { |
| 326 |
// {{ $headers := .Headers }}{{ range $i, $r := .Rows }}Row {{ $i }}:{{ range $j, $c := $r }} |
| 327 |
// {{ index $headers $j }} = "{{ $c }}"{{ end }} |
| 328 |
// {{ end }} |
| 329 |
for i, r := range tpl.Rows { |
| 330 |
fmt.Fprintf(w, "Row %d:\n", i) |
| 331 |
for j, c := range r { |
| 332 |
fmt.Fprintf(w, " %s = \"%s\"\n", tpl.Headers[j], c) |
| 333 |
} |
| 334 |
} |
| 335 |
return nil |
| 336 |
} |