Debian Codegraph @

txtar/archive_test.go

raw symbols SCIP
1 // Copyright 2018 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.
4
5 package txtar
6
7 import (
8 "bytes"
9 "fmt"
10 "reflect"
11 "testing"
12 )
13
14 func TestParse(t *testing.T) {
15 var tests = []struct {
16 name string
17 text string
18 parsed *Archive
19 }{
20 {
21 name: "basic",
22 text: `comment1
23 comment2
24 -- file1 --
25 File 1 text.
26 -- foo ---
27 More file 1 text.
28 -- file 2 --
29 File 2 text.
30 -- empty --
31 -- noNL --
32 hello world
33 -- empty filename line --
34 some content
35 -- --`,
36 parsed: &Archive{
37 Comment: []byte("comment1\ncomment2\n"),
38 Files: []File{
39 {"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
40 {"file 2", []byte("File 2 text.\n")},
41 {"empty", []byte{}},
42 {"noNL", []byte("hello world\n")},
43 {"empty filename line", []byte("some content\n-- --\n")},
44 },
45 },
46 },
47 {
48 name: "crlf",
49 text: "comment\r\n-- file --\r\ndata\r\n",
50 parsed: &Archive{
51 Comment: []byte("comment\r\n"),
52 Files: []File{{"file", []byte("data\r\n")}},
53 },
54 },
55 {
56 name: "utf8 and comment",
57 text: `# This is a test comment
58 -- hello.txt --
59 Hello
60 -- unicode.txt --
61 Go语言
62 `,
63 parsed: &Archive{
64 Comment: []byte("# This is a test comment\n"),
65 Files: []File{
66 {"hello.txt", []byte("Hello\n")},
67 {"unicode.txt", []byte("Go语言\n")},
68 },
69 },
70 },
71 }
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 a := Parse([]byte(tt.text))
75 if !reflect.DeepEqual(a, tt.parsed) {
76 t.Fatalf("Parse: wrong output:\nhave:\n%s\nwant:\n%s", shortArchive(a), shortArchive(tt.parsed))
77 }
78 text := Format(a)
79 a = Parse(text)
80 if !reflect.DeepEqual(a, tt.parsed) {
81 t.Fatalf("Parse after Format: wrong output:\nhave:\n%s\nwant:\n%s", shortArchive(a), shortArchive(tt.parsed))
82 }
83 })
84 }
85 }
86
87 func TestFormat(t *testing.T) {
88 var tests = []struct {
89 name string
90 input *Archive
91 wanted string
92 }{
93 {
94 name: "basic",
95 input: &Archive{
96 Comment: []byte("comment1\ncomment2\n"),
97 Files: []File{
98 {"file1", []byte("File 1 text.\n-- foo ---\nMore file 1 text.\n")},
99 {"file 2", []byte("File 2 text.\n")},
100 {"empty", []byte{}},
101 {"noNL", []byte("hello world")},
102 },
103 },
104 wanted: `comment1
105 comment2
106 -- file1 --
107 File 1 text.
108 -- foo ---
109 More file 1 text.
110 -- file 2 --
111 File 2 text.
112 -- empty --
113 -- noNL --
114 hello world
115 `,
116 },
117 }
118 for _, tt := range tests {
119 t.Run(tt.name, func(t *testing.T) {
120 result := Format(tt.input)
121 if string(result) != tt.wanted {
122 t.Errorf("Wrong output. \nGot:\n%s\nWant:\n%s\n", string(result), tt.wanted)
123 }
124 })
125 }
126 }
127
128 func shortArchive(a *Archive) string {
129 var buf bytes.Buffer
130 fmt.Fprintf(&buf, "comment: %q\n", a.Comment)
131 for _, f := range a.Files {
132 fmt.Fprintf(&buf, "file %q: %q\n", f.Name, f.Data)
133 }
134 return buf.String()
135 }