-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpath_test.go
159 lines (148 loc) · 3.23 KB
/
fpath_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
148
149
150
151
152
153
154
155
156
157
158
159
package fpath
import (
"fmt"
"reflect"
"testing"
)
func TestJoin(t *testing.T) {
type args struct {
elem []string
}
tests := []struct {
name string
args args
want Path
}{
{"empty", args{[]string{"", ""}}, Path("")},
{"/", args{[]string{"", "/"}}, Path("/")},
{"../a/b", args{[]string{"..", "a", "b"}}, Path("../a/b")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Join(tt.args.elem...); *got != tt.want {
t.Errorf("Join() = %v, want %v", got, tt.want)
}
})
}
}
func TestNew(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
want Path
}{
{"simple", args{"a/b"}, Path("a/b")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := New(tt.args.path); *got != tt.want {
t.Errorf("New() = %v, want %v", got, tt.want)
}
})
}
}
func TestPath_Join(t *testing.T) {
type args struct {
elem []string
}
tests := []struct {
name string
p Path
args args
want Path
}{
{"empty", Path(""), args{[]string{"", ""}}, Path("")},
{"a/b/c", Path("a"), args{[]string{"b", "c"}}, Path("a/b/c")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.Join(tt.args.elem...); *got != tt.want {
t.Errorf("Join() = %v, want %v", got, tt.want)
}
})
}
}
func TestPath_Abs(t *testing.T) {
tests := []struct {
name string
p Path
want Path
}{
{"/a/b/c", Path("/a/../../x"), Path("/x")},
{"Cwd/a/b/c", Path("a/../x"), *Cwd().Join("/x")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.Abs(); !reflect.DeepEqual(*got, tt.want) {
t.Errorf("Abs() = %v, want %v", got, tt.want)
}
})
}
}
func TestPath_Parents(t *testing.T) {
type args struct {
level int
}
tests := []struct {
name string
p Path
args args
want Path
}{
{":empty", Path(""), args{ 0}, Path(".")},
{":/", Path("/"), args{ 0}, Path("/")},
{":/a", Path("/a"), args{ 0}, Path("/")},
{":/a/b", Path("/a/b"), args{ 1}, Path("/")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.p.Parents(tt.args.level); !reflect.DeepEqual(*got, tt.want) {
t.Errorf("Parents() = %v, want %v", got, tt.want)
} else {
fmt.Println(got)
}
})
}
}
func TestPath_Name(t *testing.T) {
p := New("$HOME/t.js").Expand()
fmt.Println(p)
fmt.Println(p.Dir())
fmt.Println(p.Base())
fmt.Println(p.Stem())
fmt.Println(p.Ext())
fmt.Println(p.WithPrefix("new-"))
fmt.Println(p.WithSuffix(".gz"))
}
func TestValue_Array(t *testing.T) {
type args struct {
sep string
}
tests := []struct {
name string
v Value
args args
want []Value
}{
{"single: 1", Value("1"), args{" "}, []Value{Value("1")}},
{"multiple: 1 2", Value("1 2"), args{" "} , []Value{Value("1"), Value("2")}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.v.Array(tt.args.sep); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Array() = %v, want %v", got, tt.want)
}
})
}
}
func TestValue_LoadValueMap(t *testing.T) {
inp,_ := New("data.txt").Open(ForReading)
kv := LoadValueMap(inp, ":", true, true, true)
for k,v := range kv {
fmt.Println(k, "=", v.Int())
}
fmt.Println(kv["User"].Path().ListDir(true))
}