File: tree_test.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (90 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download
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
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package generic

import (
	"context"
	"testing"

	"code.forgejo.org/f3/gof3/v3/path"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestTreeInit(t *testing.T) {
	tree := NewTree(newTestOptions())
	assert.NotNil(t, tree)

	assert.True(t, tree == tree.GetSelf())
	tree.SetSelf(tree)
	assert.True(t, tree == tree.GetSelf())

	other := NewTree(newTestOptions())
	assert.False(t, other == tree.GetSelf())
	assert.False(t, other.GetSelf() == tree)
	assert.False(t, other.GetSelf() == tree.GetSelf())
}

func TestTreeRoot(t *testing.T) {
	tree := NewTree(newTestOptions())
	root := NewNode()
	tree.SetRoot(root)
	assert.True(t, root == tree.GetRoot())
}

func TestTreePageSize(t *testing.T) {
	tree := NewTree(newTestOptions())
	assert.EqualValues(t, DefaultPageSize, tree.GetPageSize())
	pageSize := int(100)
	tree.GetDriver().SetPageSize(pageSize)
	assert.EqualValues(t, pageSize, tree.GetPageSize())
}

func TestTreeAllocateID(t *testing.T) {
	tree := NewTree(newTestOptions())
	assert.True(t, tree.AllocateID())
}

func TestTreeFactoryDerived(t *testing.T) {
	tree := newTestTree()
	root := tree.Factory(context.Background(), kindTestNodeLevelOne)
	tree.SetRoot(root)
	assert.True(t, root == tree.GetRoot())
	assert.True(t, kindTestNodeLevelOne == root.GetKind())
	assert.True(t, tree.GetSelf() == root.GetTree().GetSelf())

	leveltwo := tree.Factory(context.Background(), kindTestNodeLevelTwo)
	leveltwo.SetParent(root)
	assert.True(t, root == leveltwo.GetParent())
	assert.True(t, kindTestNodeLevelTwo == leveltwo.GetKind())
	assert.True(t, tree.GetSelf() == leveltwo.GetTree().GetSelf())
}

func TestTreeApply(t *testing.T) {
	tree := newTestTree()
	root := tree.Factory(context.Background(), kindTestNodeLevelOne)
	tree.SetRoot(root)

	assert.True(t, tree.Apply(context.Background(), path.NewPath(), nil))
	assert.True(t, tree.Apply(context.Background(), path.NewPathFromString(NewElementNode, "/"),
		NewApplyOptions(func(ctx context.Context, parent, path path.Path, node NodeInterface) {
			require.Equal(t, root, node)
		})),
	)
}

func TestTreeApplyAndGet(t *testing.T) {
	tree := newTestTree()
	root := tree.Factory(context.Background(), kindTestNodeLevelOne)
	tree.SetRoot(root)

	assert.True(t, tree.ApplyAndGet(context.Background(), path.NewPath(), nil))
	assert.True(t, tree.ApplyAndGet(context.Background(), path.NewPathFromString(NewElementNode, "/"),
		NewApplyOptions(func(ctx context.Context, parent, path path.Path, node NodeInterface) {
			require.Equal(t, root, node)
		})),
	)
}