-
Notifications
You must be signed in to change notification settings - Fork 0
/
textar_test.go
55 lines (49 loc) · 1.57 KB
/
textar_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package textar_test
import (
"bytes"
"io/fs"
"testing"
"github.com/ypsu/textar"
)
func TestTextar(t *testing.T) {
srcArchive := []textar.File{
{"file1", []byte("content 1")},
{"file2", []byte("content 2\n")},
{"somedir/file3", []byte("content 3\n== with separator\n")},
{"/file4", nil},
}
data := textar.Format(srcArchive)
t.Logf("Encoded textar:\n%s", data)
dstArchive := textar.Parse(data)
if len(dstArchive) != len(srcArchive) {
t.Fatalf("Archive size = %d, want %d.", len(dstArchive), len(srcArchive))
}
for i := range dstArchive {
if dstArchive[i].Name != srcArchive[i].Name || !bytes.Equal(dstArchive[i].Data, srcArchive[i].Data) {
t.Errorf("dst[i] = {%q, %q}, want {%q, %q}.", dstArchive[i].Name, dstArchive[i].Data, srcArchive[i].Name, srcArchive[i].Data)
}
}
var dir fs.FS
dir = textar.FS(srcArchive)
_, err := fs.ReadFile(dir, "somedir/nonexistent")
if err == nil {
t.Errorf("Reading somedir/nonexistent didn't return an error.")
}
contents, err := fs.ReadFile(dir, "somedir/file3")
if err != nil || bytes.Compare(contents, srcArchive[2].Data) != 0 {
t.Errorf("Inconsistent content in somedir/file3.")
}
matches, _ := fs.Glob(dir, "*")
t.Logf("Glob() output: %q.", matches)
if len(matches) != 4 {
t.Errorf("Glob() = %d, want %d.\n", len(matches), 4)
}
}
func TestIndent(t *testing.T) {
src, indent := []byte("some\nXXindented\nstring"), "XX"
dst := textar.Unindent(textar.Indent(src, indent), indent)
t.Logf("Indented string: %q.", textar.Indent(src, indent))
if !bytes.Equal(dst, src) {
t.Errorf("Unindent() = %q, want %q.", dst, src)
}
}