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 if !bytes.Equal(output.Bytes(), []byte(test.Output)) {
58 t.Errorf("test %d.%d: expected output: %q, got %q", n+1, i, test.Output, output)
59 }
60 }
61 }
62 }
63
64 func TestUnix2DOS(t *testing.T) {
65 buf := make([]byte, 10)
66 output := bytes.NewBuffer(make([]byte, 0, 100))
67 w := &writer{output}
68 for n, test := range []struct {
69 Input, Output string
70 }{
71 {
72 "Hello",
73 "Hello",
74 },
75 {
76 "Hello\n",
77 "Hello\r\n",
78 },
79 {
80 "Hello\nWorld",
81 "Hello\r\nWorld",
82 },
83 {
84 "qwertyuiop\nasdfghjkl\nzxcvbnm\n",
85 "qwertyuiop\r\nasdfghjkl\r\nzxcvbnm\r\n",
86 },
87 } {
88 for i := 1; i < 10; i++ {
89 output.Reset()
90 io.CopyBuffer(w, Unix2DOS(strings.NewReader(test.Input)), buf[:i])
91 if !bytes.Equal(output.Bytes(), []byte(test.Output)) {
92 t.Errorf("test %d.%d: expected output: %q, got %q", n+1, i, test.Output, output)
93 }
94 }
95 }
96 }
97