forked from purpleidea/mgmt
/
misc_test.go
200 lines (159 loc) · 4.79 KB
/
misc_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Mgmt
// Copyright (C) 2013-2016+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"fmt"
"testing"
)
func TestMiscT1(t *testing.T) {
if Dirname("/foo/bar/baz") != "/foo/bar/" {
t.Errorf("Result is incorrect.")
}
if Dirname("/foo/bar/baz/") != "/foo/bar/" {
t.Errorf("Result is incorrect.")
}
if Dirname("/") != "" { // TODO: should this equal "/" or "" ?
t.Errorf("Result is incorrect.")
}
if Basename("/foo/bar/baz") != "baz" {
t.Errorf("Result is incorrect.")
}
if Basename("/foo/bar/baz/") != "baz/" {
t.Errorf("Result is incorrect.")
}
if Basename("/") != "/" { // TODO: should this equal "" or "/" ?
t.Errorf("Result is incorrect.")
}
}
func TestMiscT2(t *testing.T) {
// TODO: compare the output with the actual list
p1 := "/foo/bar/baz"
r1 := []string{"", "foo", "bar", "baz"}
if len(PathSplit(p1)) != len(r1) {
//t.Errorf("Result should be: %q.", r1)
t.Errorf("Result should have a length of: %v.", len(r1))
}
p2 := "/foo/bar/baz/"
r2 := []string{"", "foo", "bar", "baz"}
if len(PathSplit(p2)) != len(r2) {
t.Errorf("Result should have a length of: %v.", len(r2))
}
}
func TestMiscT3(t *testing.T) {
if HasPathPrefix("/foo/bar/baz", "/foo/ba") != false {
t.Errorf("Result should be false.")
}
if HasPathPrefix("/foo/bar/baz", "/foo/bar") != true {
t.Errorf("Result should be true.")
}
if HasPathPrefix("/foo/bar/baz", "/foo/bar/") != true {
t.Errorf("Result should be true.")
}
if HasPathPrefix("/foo/bar/baz/", "/foo/bar") != true {
t.Errorf("Result should be true.")
}
if HasPathPrefix("/foo/bar/baz/", "/foo/bar/") != true {
t.Errorf("Result should be true.")
}
if HasPathPrefix("/foo/bar/baz/", "/foo/bar/baz/dude") != false {
t.Errorf("Result should be false.")
}
}
func TestMiscT4(t *testing.T) {
if PathPrefixDelta("/foo/bar/baz", "/foo/ba") != -1 {
t.Errorf("Result should be -1.")
}
if PathPrefixDelta("/foo/bar/baz", "/foo/bar") != 1 {
t.Errorf("Result should be 1.")
}
if PathPrefixDelta("/foo/bar/baz", "/foo/bar/") != 1 {
t.Errorf("Result should be 1.")
}
if PathPrefixDelta("/foo/bar/baz/", "/foo/bar") != 1 {
t.Errorf("Result should be 1.")
}
if PathPrefixDelta("/foo/bar/baz/", "/foo/bar/") != 1 {
t.Errorf("Result should be 1.")
}
if PathPrefixDelta("/foo/bar/baz/", "/foo/bar/baz/dude") != -1 {
t.Errorf("Result should be -1.")
}
if PathPrefixDelta("/foo/bar/baz/a/b/c/", "/foo/bar/baz") != 3 {
t.Errorf("Result should be 3.")
}
if PathPrefixDelta("/foo/bar/baz/", "/foo/bar/baz") != 0 {
t.Errorf("Result should be 0.")
}
}
func TestMiscT5(t *testing.T) {
if PathIsDir("/foo/bar/baz/") != true {
t.Errorf("Result should be false.")
}
if PathIsDir("/foo/bar/baz") != false {
t.Errorf("Result should be false.")
}
if PathIsDir("/foo/") != true {
t.Errorf("Result should be true.")
}
if PathIsDir("/") != true {
t.Errorf("Result should be true.")
}
}
func TestMiscT6(t *testing.T) {
type foo struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Value int `yaml:"value"`
}
obj := foo{"dude", "sweet", 42}
output, ok := ObjToB64(obj)
if ok != true {
t.Errorf("First result should be true.")
}
var data foo
if B64ToObj(output, &data) != true {
t.Errorf("Second result should be true.")
}
// TODO: there is probably a better way to compare these two...
if fmt.Sprintf("%+v\n", obj) != fmt.Sprintf("%+v\n", data) {
t.Errorf("Strings should match.")
}
}
func TestMiscT7(t *testing.T) {
type Foo struct {
Name string `yaml:"name"`
Type string `yaml:"type"`
Value int `yaml:"value"`
}
type bar struct {
Foo `yaml:",inline"` // anonymous struct must be public!
Comment string `yaml:"comment"`
}
obj := bar{Foo{"dude", "sweet", 42}, "hello world"}
output, ok := ObjToB64(obj)
if ok != true {
t.Errorf("First result should be true.")
}
var data bar
if B64ToObj(output, &data) != true {
t.Errorf("Second result should be true.")
}
// TODO: there is probably a better way to compare these two...
if fmt.Sprintf("%+v\n", obj) != fmt.Sprintf("%+v\n", data) {
t.Errorf("Strings should match.")
}
}