dos2unix - writer_test.go
1 package dos2unix
2
3 import (
4 "bytes"
5 "io"
6 "strings"
7 "testing"
8 )
9
10 type reader struct {
11 io.Reader
12 }
13
14 func TestDOS2UnixWriter(t *testing.T) {
15 buf := make([]byte, 10)
16 output := bytes.NewBuffer(make([]byte, 0, 100))
17 for n, test := range []struct {
18 Input, Output string
19 }{
20 {
21 "Hello",
22 "Hello",
23 },
24 {
25 "Hello\r\n",
26 "Hello\n",
27 },
28 {
29 "Hello\r\nWorld",
30 "Hello\nWorld",
31 },
32 {
33 "qwertyuiop\r\nasdfghjkl\r\nzxcvbnm\r\n",
34 "qwertyuiop\nasdfghjkl\nzxcvbnm\n",
35 },
36 {
37 "qwertyuiop\rasdfgkl\rzxcbnm\r",
38 "qwertyuiop\rasdfgkl\rzxcbnm\r",
39 },
40 } {
41 for i := 1; i < 10; i++ {
42 output.Reset()
43 d := DOS2UnixWriter(output)
44 _, err := io.CopyBuffer(d, reader{strings.NewReader(test.Input)}, buf[:i])
45 if err != nil {
46 t.Errorf("test %d.%d: unexpected write error: %s", n+1, i, err)
47 } else if err := d.Flush(); err != nil {
48 t.Errorf("test %d.%d: unexpected flush error: %s", n+1, i, err)
49 } else if !bytes.Equal(output.Bytes(), []byte(test.Output)) {
50 t.Errorf("test %d.%d: expected output: %q, got %q", n+1, i, test.Output, output)
51 }
52 }
53 }
54 }
55
56 func TestUnix2DOSWriter(t *testing.T) {
57 buf := make([]byte, 10)
58 output := bytes.NewBuffer(make([]byte, 0, 100))
59 for n, test := range []struct {
60 Input, Output string
61 }{
62 {
63 "Hello",
64 "Hello",
65 },
66 {
67 "Hello\n",
68 "Hello\r\n",
69 },
70 {
71 "Hello\nWorld",
72 "Hello\r\nWorld",
73 },
74 {
75 "qwertyuiop\nasdfghjkl\nzxcvbnm\n",
76 "qwertyuiop\r\nasdfghjkl\r\nzxcvbnm\r\n",
77 },
78 } {
79 for i := 1; i < 10; i++ {
80 output.Reset()
81 _, err := io.CopyBuffer(Unix2DOSWriter(output), reader{strings.NewReader(test.Input)}, buf[:i])
82 if err != nil {
83 t.Errorf("test %d.%d: unexpected error: %s", n+1, i, err)
84 } else if !bytes.Equal(output.Bytes(), []byte(test.Output)) {
85 t.Errorf("test %d.%d: expected output: %q, got %q", n+1, i, test.Output, output)
86 }
87 }
88 }
89 }
90