aboutsummaryrefslogtreecommitdiffstats
path: root/utils/merkletrie/noder/path_test.go
blob: f49f028d3b27f64cc1b6fdad31276ed92d470dd1 (plain) (blame)
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
package noder

import (
	"golang.org/x/text/unicode/norm"
	. "gopkg.in/check.v1"
)

type PathSuite struct{}

var _ = Suite(&PathSuite{})

func (s *PathSuite) TestShortFile(c *C) {
	f := &noderMock{
		name:  "1",
		isDir: false,
	}
	p := Path([]Noder{f})
	c.Assert(p.String(), Equals, "1")
}

func (s *PathSuite) TestShortDir(c *C) {
	d := &noderMock{
		name:     "1",
		isDir:    true,
		children: NoChildren,
	}
	p := Path([]Noder{d})
	c.Assert(p.String(), Equals, "1")
}

func (s *PathSuite) TestLongFile(c *C) {
	n3 := &noderMock{
		name:  "3",
		isDir: false,
	}
	n2 := &noderMock{
		name:     "2",
		isDir:    true,
		children: []Noder{n3},
	}
	n1 := &noderMock{
		name:     "1",
		isDir:    true,
		children: []Noder{n2},
	}
	p := Path([]Noder{n1, n2, n3})
	c.Assert(p.String(), Equals, "1/2/3")
}

func (s *PathSuite) TestLongDir(c *C) {
	n3 := &noderMock{
		name:     "3",
		isDir:    true,
		children: NoChildren,
	}
	n2 := &noderMock{
		name:     "2",
		isDir:    true,
		children: []Noder{n3},
	}
	n1 := &noderMock{
		name:     "1",
		isDir:    true,
		children: []Noder{n2},
	}
	p := Path([]Noder{n1, n2, n3})
	c.Assert(p.String(), Equals, "1/2/3")
}

func (s *PathSuite) TestCompareDepth1(c *C) {
	p1 := Path([]Noder{&noderMock{name: "a"}})
	p2 := Path([]Noder{&noderMock{name: "b"}})
	c.Assert(p1.Compare(p2), Equals, -1)
	c.Assert(p2.Compare(p1), Equals, 1)

	p1 = Path([]Noder{&noderMock{name: "a"}})
	p2 = Path([]Noder{&noderMock{name: "a"}})
	c.Assert(p1.Compare(p2), Equals, 0)
	c.Assert(p2.Compare(p1), Equals, 0)

	p1 = Path([]Noder{&noderMock{name: "a.go"}})
	p2 = Path([]Noder{&noderMock{name: "a"}})
	c.Assert(p1.Compare(p2), Equals, 1)
	c.Assert(p2.Compare(p1), Equals, -1)
}

func (s *PathSuite) TestCompareDepth2(c *C) {
	p1 := Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "b"},
	})
	p2 := Path([]Noder{
		&noderMock{name: "b"},
		&noderMock{name: "a"},
	})
	c.Assert(p1.Compare(p2), Equals, -1)
	c.Assert(p2.Compare(p1), Equals, 1)

	p1 = Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "b"},
	})
	p2 = Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "b"},
	})
	c.Assert(p1.Compare(p2), Equals, 0)
	c.Assert(p2.Compare(p1), Equals, 0)

	p1 = Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "b"},
	})
	p2 = Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "a"},
	})
	c.Assert(p1.Compare(p2), Equals, 1)
	c.Assert(p2.Compare(p1), Equals, -1)
}

func (s *PathSuite) TestCompareMixedDepths(c *C) {
	p1 := Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "b"},
	})
	p2 := Path([]Noder{&noderMock{name: "b"}})
	c.Assert(p1.Compare(p2), Equals, -1)
	c.Assert(p2.Compare(p1), Equals, 1)

	p1 = Path([]Noder{
		&noderMock{name: "b"},
		&noderMock{name: "b"},
	})
	p2 = Path([]Noder{&noderMock{name: "b"}})
	c.Assert(p1.Compare(p2), Equals, 1)
	c.Assert(p2.Compare(p1), Equals, -1)

	p1 = Path([]Noder{&noderMock{name: "a.go"}})
	p2 = Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "a.go"},
	})
	c.Assert(p1.Compare(p2), Equals, 1)
	c.Assert(p2.Compare(p1), Equals, -1)

	p1 = Path([]Noder{&noderMock{name: "b.go"}})
	p2 = Path([]Noder{
		&noderMock{name: "a"},
		&noderMock{name: "a.go"},
	})
	c.Assert(p1.Compare(p2), Equals, 1)
	c.Assert(p2.Compare(p1), Equals, -1)
}

func (s *PathSuite) TestCompareNormalization(c *C) {
	p1 := Path([]Noder{&noderMock{name: norm.Form(norm.NFKC).String("페")}})
	p2 := Path([]Noder{&noderMock{name: norm.Form(norm.NFKD).String("페")}})
	c.Assert(p1.Compare(p2), Equals, 1)
	c.Assert(p2.Compare(p1), Equals, -1)
	p1 = Path([]Noder{&noderMock{name: "TestAppWithUnicodéPath"}})
	p2 = Path([]Noder{&noderMock{name: "TestAppWithUnicodéPath"}})
	c.Assert(p1.Compare(p2), Equals, -1)
	c.Assert(p2.Compare(p1), Equals, 1)
}