1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 func (b *buffer) init(n int) { b.a = make([]byte, n)[0:0] }
19 func (b *buffer) clear() { b.a = b.a[0:0] }
21 func (b *buffer) Write(buf []byte) (written int, err os.Error) {
26 for i := 0; i < m; i++ {
30 panic("buffer.Write: buffer too small")
35 func (b *buffer) String() string { return string(b.a) }
37 func write(t *testing.T, testname string, w *Writer, src string) {
38 written, err := io.WriteString(w, src)
40 t.Errorf("--- test: %s\n--- src:\n%q\n--- write error: %v\n", testname, src, err)
42 if written != len(src) {
43 t.Errorf("--- test: %s\n--- src:\n%q\n--- written = %d, len(src) = %d\n", testname, src, written, len(src))
47 func verify(t *testing.T, testname string, w *Writer, b *buffer, src, expected string) {
50 t.Errorf("--- test: %s\n--- src:\n%q\n--- flush error: %v\n", testname, src, err)
55 t.Errorf("--- test: %s\n--- src:\n%q\n--- found:\n%q\n--- expected:\n%q\n", testname, src, res, expected)
59 func check(t *testing.T, testname string, minwidth, tabwidth, padding int, padchar byte, flags uint, src, expected string) {
64 w.Init(&b, minwidth, tabwidth, padding, padchar, flags)
67 title := testname + " (written all at once)"
69 write(t, title, &w, src)
70 verify(t, title, &w, &b, src, expected)
73 title = testname + " (written byte-by-byte)"
75 for i := 0; i < len(src); i++ {
76 write(t, title, &w, src[i:i+1])
78 verify(t, title, &w, &b, src, expected)
80 // write using Fibonacci slice sizes
81 title = testname + " (written in fibonacci slices)"
83 for i, d := 0, 0; i < len(src); {
84 write(t, title, &w, src[i:i+d])
90 verify(t, title, &w, &b, src, expected)
93 var tests = []struct {
95 minwidth, tabwidth, padding int
116 8, 0, 1, '.', StripEscape,
130 8, 0, 1, '.', StripEscape,
144 8, 0, 1, '.', StripEscape,
145 "\xff\"foo\t\n\tbar\"\xff",
152 "\xff\"foo\t\n\tbar\"\xff",
153 "\xff\"foo\t\n\tbar\"\xff",
158 8, 0, 1, '.', StripEscape,
159 "abc\xff\tdef", // unterminated escape
166 "abc\xff\tdef", // unterminated escape
187 "\t", // '\t' terminates an empty cell on last line - nothing to print
193 8, 0, 1, '.', AlignRight,
194 "\t", // '\t' terminates an empty cell on last line - nothing to print
228 8, 0, 1, '.', AlignRight,
272 "e) foo..bar.....\n",
277 8, 0, 1, '.', FilterHTML,
278 "f) f<o\t<b>bar</b>\t\n",
279 "f) f<o..<b>bar</b>.....\n",
284 8, 0, 1, '.', FilterHTML,
285 "g) f<o\t<b>bar</b>\t non-terminated entity &",
286 "g) f<o..<b>bar</b>..... non-terminated entity &",
291 8, 0, 1, '.', FilterHTML | Debug,
292 "g) f<o\t<b>bar</b>\t non-terminated entity &",
293 "g) f<o..|<b>bar</b>.....| non-terminated entity &",
307 "11\t222\t3333\t44444\n",
315 1, 0, 0, '.', FilterHTML,
316 "1\t2<!---\f--->\t3\t4\n" + // \f inside HTML is ignored
317 "11\t222\t3333\t44444\n",
319 "1.2<!---\f--->..3...4\n" +
326 "1\t2\t3\t4\f" + // \f causes a newline and flush
327 "11\t222\t3333\t44444\n",
336 "1\t2\t3\t4\f" + // \f causes a newline and flush
337 "11\t222\t3333\t44444\n",
341 "11|222|3333|44444\n",
348 "1....2....3....4\n",
355 "1....2....3....4....\n",
362 "aa\t\u672c\u672c\u672c\tcccc\tddddd\n" +
365 "本.......b.......c\n" +
366 "aa......本本本.....cccc....ddddd\n" +
372 8, 0, 1, ' ', AlignRight,
374 "aa\tèèè\tcccc\tddddd\t\n" +
378 " aa èèè cccc ddddd\n" +
401 "a_______b_______c\n" +
402 "aa______bbb_____cccc\n" +
409 "4444\t日本語\t22\t1\t333\n" +
414 "666666\t666666\t666666\t4444\n" +
415 "1\t1\t999999999\t0000000000\n",
417 "4444------日本語-22--1---333\n" +
420 "------------------88888888\n" +
422 "666666-666666-666666----4444\n" +
423 "1------1------999999999-0000000000\n",
429 "4444\t333\t22\t1\t333\n" +
434 "666666\t666666\t666666\t4444\n" +
435 "1\t1\t999999999\t0000000000\n",
437 "4444........333...22...1...333\n" +
440 "....................88888888\n" +
442 "666666...666666...666666......4444\n" +
443 "1........1........999999999...0000000000\n",
448 8, 8, 1, '\t', FilterHTML,
449 "4444\t333\t22\t1\t333\n" +
454 "666666\t666666\t666666\t4444\n" +
455 "1\t1\t<font color=red attr=日本語>999999999</font>\t0000000000\n",
457 "4444\t\t333\t22\t1\t333\n" +
460 "\t\t\t\t88888888\n" +
462 "666666\t666666\t666666\t\t4444\n" +
463 "1\t1\t<font color=red attr=日本語>999999999</font>\t0000000000\n",
468 1, 0, 2, ' ', AlignRight,
469 ".0\t.3\t2.4\t-5.1\t\n" +
470 "23.0\t12345678.9\t2.4\t-989.4\t\n" +
471 "5.1\t12.0\t2.4\t-7.0\t\n" +
472 ".0\t0.0\t332.0\t8908.0\t\n" +
473 ".0\t-.3\t456.4\t22.1\t\n" +
474 ".0\t1.2\t44.4\t-13.3\t\t",
476 " .0 .3 2.4 -5.1\n" +
477 " 23.0 12345678.9 2.4 -989.4\n" +
478 " 5.1 12.0 2.4 -7.0\n" +
479 " .0 0.0 332.0 8908.0\n" +
480 " .0 -.3 456.4 22.1\n" +
481 " .0 1.2 44.4 -13.3",
486 1, 0, 2, ' ', AlignRight | Debug,
487 ".0\t.3\t2.4\t-5.1\t\n" +
488 "23.0\t12345678.9\t2.4\t-989.4\t\n" +
489 "5.1\t12.0\t2.4\t-7.0\t\n" +
490 ".0\t0.0\t332.0\t8908.0\t\n" +
491 ".0\t-.3\t456.4\t22.1\t\n" +
492 ".0\t1.2\t44.4\t-13.3\t\t",
494 " .0| .3| 2.4| -5.1|\n" +
495 " 23.0| 12345678.9| 2.4| -989.4|\n" +
496 " 5.1| 12.0| 2.4| -7.0|\n" +
497 " .0| 0.0| 332.0| 8908.0|\n" +
498 " .0| -.3| 456.4| 22.1|\n" +
499 " .0| 1.2| 44.4| -13.3|",
511 4, 0, 0, '.', DiscardEmptyColumns,
512 "a\t\tb", // htabs - do not discard column
518 4, 0, 0, '.', DiscardEmptyColumns,
525 4, 0, 0, '.', AlignRight | DiscardEmptyColumns,
532 100, 100, 0, '\t', 0,
548 100, 100, 0, '\t', DiscardEmptyColumns,
564 100, 100, 0, '\t', DiscardEmptyColumns | Debug,
575 "a\t|b\t|c\t|d\t|e\n",
580 100, 100, 0, '\t', DiscardEmptyColumns,
581 "a\tb\t\td\n" + // hard tabs - do not discard column
596 100, 100, 0, '\t', DiscardEmptyColumns | Debug,
597 "a\tb\t\td\n" + // hard tabs - do not discard column
604 "a\t|b\t|\t|d\t|e\n" +
607 "a\t|b\t|c\t|d\t|e\n",
611 func Test(t *testing.T) {
612 for _, e := range tests {
613 check(t, e.testname, e.minwidth, e.tabwidth, e.padding, e.padchar, e.flags, e.src, e.expected)