Debian Codegraph @

utils/merkletrie/noder/noder_test.go

raw
1 package noder
2
3 import (
4 "testing"
5
6 . "gopkg.in/check.v1"
7 )
8
9 func Test(t *testing.T) { TestingT(t) }
10
11 type NoderSuite struct{}
12
13 var _ = Suite(&NoderSuite{})
14
15 type noderMock struct {
16 name string
17 hash []byte
18 isDir bool
19 children []Noder
20 }
21
22 func (n noderMock) String() string { return n.Name() }
23 func (n noderMock) Hash() []byte { return n.hash }
24 func (n noderMock) Name() string { return n.name }
25 func (n noderMock) IsDir() bool { return n.isDir }
26 func (n noderMock) Children() ([]Noder, error) { return n.children, nil }
27 func (n noderMock) NumChildren() (int, error) { return len(n.children), nil }
28 func (n noderMock) Skip() bool { return false }
29
30 // Returns a sequence with the noders 3, 2, and 1 from the
31 // following diagram:
32 //
33 // 3
34 // |
35 // 2
36 // |
37 // 1
38 // / \
39 // c1 c2
40 //
41 // This is also the path of "1".
42 func nodersFixture() []Noder {
43 n1 := &noderMock{
44 name: "1",
45 hash: []byte{0x00, 0x01, 0x02},
46 isDir: true,
47 children: childrenFixture(),
48 }
49 n2 := &noderMock{name: "2"}
50 n3 := &noderMock{name: "3"}
51 return []Noder{n3, n2, n1}
52 }
53
54 // Returns a collection of 2 noders: c1, c2.
55 func childrenFixture() []Noder {
56 c1 := &noderMock{name: "c1"}
57 c2 := &noderMock{name: "c2"}
58 return []Noder{c1, c2}
59 }
60
61 // returns nodersFixture as the path of "1".
62 func pathFixture() Path {
63 return Path(nodersFixture())
64 }
65
66 func (s *NoderSuite) TestString(c *C) {
67 c.Assert(pathFixture().String(), Equals, "3/2/1")
68 }
69
70 func (s *NoderSuite) TestLast(c *C) {
71 c.Assert(pathFixture().Last().Name(), Equals, "1")
72 }
73
74 func (s *NoderSuite) TestPathImplementsNoder(c *C) {
75 p := pathFixture()
76 c.Assert(p.Name(), Equals, "1")
77 c.Assert(p.Hash(), DeepEquals, []byte{0x00, 0x01, 0x02})
78 c.Assert(p.IsDir(), Equals, true)
79
80 children, err := p.Children()
81 c.Assert(err, IsNil)
82 c.Assert(children, DeepEquals, childrenFixture())
83
84 numChildren, err := p.NumChildren()
85 c.Assert(err, IsNil)
86 c.Assert(numChildren, Equals, 2)
87 }