-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopher_test.go
79 lines (67 loc) · 1.37 KB
/
gopher_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
package main
import (
"bytes"
"testing"
)
func TestSerializeLine(t *testing.T) {
line := gopherline{
Ftype: 'h',
Text: "abc",
Path: "def",
Host: "xyz",
Port: 70,
}
expected := "habc\tdef\txyz\t70\r\n"
buf := new(bytes.Buffer)
line.serialize(buf)
if buf.String() != expected {
t.Error("gopherline.serialize does not match expected")
}
}
func TestSerializeDir(t *testing.T) {
dir := gopherdir{
&gopherline{'h', "abc", "def", "xyz", 70},
&gopherline{'i', "123", "456", "789", 80},
}
expected := "habc\tdef\txyz\t70\r\ni123\t456\t789\t80\r\n.\r\n"
buf := new(bytes.Buffer)
dir.serialize(buf)
if buf.String() != expected {
t.Error("gopherdir.serialize does not match expected")
}
}
func TestTopath(t *testing.T) {
cases := [][3]string{
[3]string{"/x/y", "z", "/x/y/z"},
[3]string{"x/y/", "z/a/b/c", "x/y/z/a/b/c"},
}
for _, c := range cases {
if topath(c[0], c[1]) != c[2] {
t.Errorf("topath failed: (%s, %s) -> %s", c[0], c[1], c[2])
}
}
}
func TestContains(t *testing.T) {
ok_cases := []string{
"a",
"a/../",
"a/b/../",
"a/b/../../",
}
fail_cases := []string{
"../a",
"../",
"../../",
"a/b/../../../",
}
for _, c := range ok_cases {
if !contains(c) {
t.Errorf("contains failed: (%s) -> false", c)
}
}
for _, c := range fail_cases {
if contains(c) {
t.Errorf("contains failed: (%s) -> true", c)
}
}
}