-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfst_test.go
110 lines (93 loc) · 2.24 KB
/
fst_test.go
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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package fst_test
import (
"reflect"
"testing"
"github.com/iainjreid/fst-go"
)
type TestNode struct {
name string
children []*TestNode
annotations []*TestAnnotation
}
type TestAnnotation struct {
key string
value string
}
func (t *TestNode) Append(node *TestNode) {
t.children = append(t.children, node)
}
func (t *TestNode) Annotate(annotation *TestAnnotation) {
t.annotations = append(t.annotations, annotation)
}
var parent = fst.New(func(string) *TestNode {
return &TestNode{
name: "parent",
}
})
// TestAppend calls [Builder.Append], to ensure that it correctly adds a new
// child to the parent node.
func TestAppend(t *testing.T) {
var subject = parent.Append(fst.New(func(string) *TestNode {
return &TestNode{
name: "child",
}
}))
var expected = &TestNode{
name: "parent",
children: []*TestNode{
{
name: "child",
},
},
}
if !reflect.DeepEqual(subject.Build("68yvwz"), expected) {
t.Fatal("result should be equal to expected output")
}
}
// TestAnnotate calls [Buidler.Annotate], to ensure that it correctly adds an
// annotation to the parent node.
func TestAnnotate(t *testing.T) {
var subject = parent.Annotate(&TestAnnotation{
key: "test_key",
value: "test_value",
})
var expected = &TestNode{
name: "parent",
annotations: []*TestAnnotation{
{
key: "test_key",
value: "test_value",
},
},
}
if !reflect.DeepEqual(subject.Build("q708uc"), expected) {
t.Fatal("result should be equal to expected output")
}
}
// TestLift calls [Builder.Lift], to ensure that dynamic nodes can be added
// using the provided build context.
func TestLift(t *testing.T) {
var subject = parent.Lift(func(i string) *fst.Builder[string, *TestNode, *TestAnnotation] {
return fst.New(func(str string) *TestNode {
return &TestNode{
name: str,
}
})
})
var expected = &TestNode{
name: "parent",
children: []*TestNode{
{
name: "x8azmu",
},
},
}
if !reflect.DeepEqual(subject.Build("x8azmu"), expected) {
t.Fatal("result should be equal to expected output")
}
}