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