Files
gof3/tree/generic/main_test.go
2024-02-24 12:09:33 +01:00

143 lines
2.8 KiB
Go

// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT
package generic
import (
"context"
"lab.forgefriends.org/friendlyforgeformat/gof3/f3"
"lab.forgefriends.org/friendlyforgeformat/gof3/logger"
"lab.forgefriends.org/friendlyforgeformat/gof3/options"
options_logger "lab.forgefriends.org/friendlyforgeformat/gof3/options/logger"
)
type Storage map[string]int
type testTree struct {
Tree
}
type testOptions struct {
options.Options
options_logger.OptionsLogger
}
func newTestOptions() options.Interface {
opts := &testOptions{}
opts.SetName("test")
l := logger.NewLogger()
l.SetLevel(logger.Trace)
opts.SetLogger(l)
return opts
}
type noopNodeDriver struct {
NullDriver
}
func (o *noopNodeDriver) ListPage(context.Context, int) ChildrenSlice {
return ChildrenSlice{}
}
func (o *noopNodeDriver) GetIDFromName(ctx context.Context, name string) NodeID {
return NilID
}
func (o *noopNodeDriver) Equals(context.Context, NodeInterface) bool {
return false
}
func (o *noopNodeDriver) Put(context.Context) NodeID {
return o.GetNode().GetID()
}
func (o *noopNodeDriver) Patch(context.Context) {
}
func (o *noopNodeDriver) NewFormat() f3.Interface {
f := f3.NewCommon(string(NilID))
return &f
}
func (o *noopNodeDriver) ToFormat() f3.Interface {
f := f3.NewCommon(string(o.GetNode().GetID()))
return &f
}
func (o *noopNodeDriver) FromFormat(f f3.Interface) {
o.GetNode().SetID(NodeID(f.GetID()))
}
func newNoopNodeDriver() NodeDriverInterface {
return &noopNodeDriver{}
}
type testTreeDriver struct {
NullTreeDriver
}
func newTestTreeDriver() TreeDriverInterface {
return &testTreeDriver{}
}
func (o *testTreeDriver) Factory(ctx context.Context, kind Kind) NodeDriverInterface {
d := newNoopNodeDriver()
d.SetTreeDriver(o)
return d
}
func NewTestTree() TreeInterface {
tree := &testTree{}
tree.Init(tree, newTestOptions())
tree.Trace("init done")
tree.SetDriver(newTestTreeDriver())
tree.Register(KindTestNodeLevelOne, func(ctx context.Context, kind Kind) NodeInterface {
node := &TestNodeLevelOne{}
return node.Init(node)
})
tree.Register(KindTestNodeLevelTwo, func(ctx context.Context, kind Kind) NodeInterface {
node := &TestNodeLevelTwo{}
return node.Init(node)
})
return tree
}
type testNodeInterface interface {
GetV() int
}
type testNode struct {
Node
v int
}
func (o *testNode) GetV() int {
return o.v
}
func (o *testNode) Equals(ctx context.Context, other NodeInterface) bool {
if !o.Node.Equals(ctx, other) {
return false
}
return o.GetV() == other.(testNodeInterface).GetV()
}
func NewTestNode(v int) NodeInterface {
node := &testNode{v: v}
return node.Init(node)
}
var KindTestNodeLevelOne = Kind("levelone")
type TestNodeLevelOne struct {
testNode
}
var KindTestNodeLevelTwo = Kind("leveltwo")
type TestNodeLevelTwo struct {
testNode
}