-
Notifications
You must be signed in to change notification settings - Fork 0
/
files_test.go
147 lines (126 loc) · 3.34 KB
/
files_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"os"
"testing"
"time"
)
type dummyOsFile struct {
name string
isDir bool
isSym bool
}
func newDummyOsFile(name string, isDir bool) os.FileInfo {
return &dummyOsFile{
name: name,
isDir: isDir,
isSym: false,
}
}
func (f *dummyOsFile) Name() string {
return f.name
}
func (f *dummyOsFile) Size() int64 {
panic("not implemented")
}
func (f *dummyOsFile) Mode() os.FileMode {
if f.isSym {
return os.ModeSymlink
}
return os.ModeDir
}
func (f *dummyOsFile) ModTime() time.Time {
panic("not implemented")
}
func (f *dummyOsFile) IsDir() bool {
return f.isDir
}
func (f *dummyOsFile) Sys() interface{} {
panic("not implemented")
}
func TestFileInfo_FileType(t *testing.T) {
fi1 := NewFileInfo(newDummyOsFile("readme", false), nil, false)
fi2 := NewFileInfo(newDummyOsFile("test.go", false), nil, false)
fi3 := NewFileInfo(newDummyOsFile("test.html.erb", false), nil, false)
fo := NewFileInfo(newDummyOsFile("directory", true), nil, false)
inputs := []struct {
file FileInfo
expect string
}{
{fi1, "readme"},
{fi2, "go"},
{fi3, "erb"}, // TODO: Change "html.erb"?
{fo, ""},
}
for _, input := range inputs {
if input.file.FileType() != input.expect {
t.Errorf("file.FileType() expected %s, but got %s", input.expect, input.file.(*file).FileType())
}
}
}
func TestFileInfo_Path(t *testing.T) {
d1 := NewFileInfo(newDummyOsFile("test1", true), nil, false)
d2 := NewFileInfo(newDummyOsFile("test2", true), d1, false)
// test.go
fi1 := NewFileInfo(newDummyOsFile("test.go", false), nil, false)
// test1 -> test.go
fi2 := NewFileInfo(newDummyOsFile("test.go", false), d1, false)
// test1 -> test2 -> test.go
fi3 := NewFileInfo(newDummyOsFile("test.go", false), d2, false)
inputs := []struct {
file FileInfo
expect string
}{
{fi1, "test.go"},
{fi2, "test1/test.go"},
{fi3, "test1/test2/test.go"},
}
for _, input := range inputs {
if input.file.Path() != input.expect {
t.Errorf("file.Path() expected %s, but got %s", input.expect, input.file.Path())
}
}
}
func TestFileInfo_Parent(t *testing.T) {
d1 := NewFileInfo(newDummyOsFile("test1", true), nil, false)
f1 := NewFileInfo(newDummyOsFile("test.go", false), nil, false)
f2 := NewFileInfo(newDummyOsFile("test.go", false), d1, false)
inputs := []struct {
file FileInfo
parent FileInfo
ok bool
}{
{f1, nil, false},
{f2, d1, true},
}
for _, input := range inputs {
p, ok := input.file.Parent()
if p != input.parent {
t.Errorf("FileInfo %v parent expect %v, but got %v", input.file, input.parent, p)
}
if ok != input.ok {
t.Errorf("FileInfo %v Parent() ok expect %v, but got %v", input.file, input.ok, ok)
}
}
}
func TestFileInfo_ChildPrefix(t *testing.T) {
root := NewFileInfo(newDummyOsFile("root", true), nil, false)
f1 := NewFileInfo(newDummyOsFile("test.go", false), nil, false)
f2 := NewFileInfo(newDummyOsFile("test.go", false), root, false)
d1 := NewFileInfo(newDummyOsFile("test", true), root, false)
d2 := NewFileInfo(newDummyOsFile("test", true), root, true)
inputs := []struct {
file FileInfo
childPrefix string
}{
{root, ""},
{f1, ""},
{f2, ""},
{d1, "│ "},
{d2, " "},
}
for _, input := range inputs {
if input.file.ChildPrefix() != input.childPrefix {
t.Errorf("FileInfo.ChildPrefix expect %s, but got %s", input.childPrefix, input.file.ChildPrefix())
}
}
}