aboutsummaryrefslogtreecommitdiffstats
path: root/utils/merkletrie/internal/frame/frame_test.go
blob: 9cc0994c53bf4ac38315af391e194e2172c7b098 (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
package frame

import (
	"fmt"
	"testing"

	"srcd.works/go-git.v4/utils/merkletrie/internal/fsnoder"
	"srcd.works/go-git.v4/utils/merkletrie/noder"

	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type FrameSuite struct{}

var _ = Suite(&FrameSuite{})

func (s *FrameSuite) TestNewFrameFromEmptyDir(c *C) {
	A, err := fsnoder.New("A()")
	c.Assert(err, IsNil)

	frame, err := New(A)
	c.Assert(err, IsNil)

	expectedString := `[]`
	c.Assert(frame.String(), Equals, expectedString)

	first, ok := frame.First()
	c.Assert(first, IsNil)
	c.Assert(ok, Equals, false)

	first, ok = frame.First()
	c.Assert(first, IsNil)
	c.Assert(ok, Equals, false)

	l := frame.Len()
	c.Assert(l, Equals, 0)
}

func (s *FrameSuite) TestNewFrameFromNonEmpty(c *C) {
	//        _______A/________
	//        |     /  \       |
	//        x    y    B/     C/
	//                         |
	//                         z
	root, err := fsnoder.New("A(x<> y<> B() C(z<>))")
	c.Assert(err, IsNil)
	frame, err := New(root)
	c.Assert(err, IsNil)

	expectedString := `["B", "C", "x", "y"]`
	c.Assert(frame.String(), Equals, expectedString)

	l := frame.Len()
	c.Assert(l, Equals, 4)

	checkFirstAndDrop(c, frame, "B", true)
	l = frame.Len()
	c.Assert(l, Equals, 3)

	checkFirstAndDrop(c, frame, "C", true)
	l = frame.Len()
	c.Assert(l, Equals, 2)

	checkFirstAndDrop(c, frame, "x", true)
	l = frame.Len()
	c.Assert(l, Equals, 1)

	checkFirstAndDrop(c, frame, "y", true)
	l = frame.Len()
	c.Assert(l, Equals, 0)

	checkFirstAndDrop(c, frame, "", false)
	l = frame.Len()
	c.Assert(l, Equals, 0)

	checkFirstAndDrop(c, frame, "", false)
}

func checkFirstAndDrop(c *C, f *Frame, expectedNodeName string, expectedOK bool) {
	first, ok := f.First()
	c.Assert(ok, Equals, expectedOK)
	if expectedOK {
		c.Assert(first.Name(), Equals, expectedNodeName)
	}

	f.Drop()
}

// a mock noder that returns error when Children() is called
type errorNoder struct{}

func (e *errorNoder) Hash() []byte   { return nil }
func (e *errorNoder) Name() string   { return "" }
func (e *errorNoder) String() string { return "" }
func (e *errorNoder) IsDir() bool    { return true }
func (e *errorNoder) Children() ([]noder.Noder, error) {
	return nil, fmt.Errorf("mock error")
}
func (e *errorNoder) NumChildren() (int, error) {
	return 0, fmt.Errorf("mock error")
}

func (s *FrameSuite) TestNewFrameErrors(c *C) {
	_, err := New(&errorNoder{})
	c.Assert(err, Not(IsNil))
}