forked from qlliu/go-rule-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.go
97 lines (84 loc) · 2.21 KB
/
tree_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
package ruler
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestLogicToTree(t *testing.T) {
logic := "1 or 2"
head := logicToTree(logic)
t.Log(head)
assert.NotNil(t, head)
assert.Equal(t, "1 or 2", head.Expr)
}
func TestReplaceBiggestBracketContent3(t *testing.T) {
expr := "1 or 2 and 3 or ( 2 and 4 )"
result, _ := replaceBiggestBracketContentAtOnce(expr, make(map[string]string))
t.Log(result)
assert.Contains(t, result, "1 or 2 and 3")
}
func TestReplaceBiggestBracketContent(t *testing.T) {
expr := "( 1 or 2 ) and 3 or ( 2 and 4 )"
result, _ := replaceBiggestBracketContentAtOnce(expr, make(map[string]string))
t.Log(result)
assert.Contains(t, result, "3 or ( 2 and 4 )")
}
func TestReplaceBiggestBracketContent2(t *testing.T) {
expr := "( 1 or 2 ) and 3 or ( 2 and 4 )"
result, mapReplace := replaceBiggestBracketContent(expr)
t.Log(result)
t.Log(mapReplace)
assert.Contains(t, result, "and 3 or")
}
func TestReplaceBiggestBracketContent4(t *testing.T) {
expr := "( 1 or 2 ) and ( 3 or ( 2 and 4 ) )"
result, mapReplace := replaceBiggestBracketContent(expr)
t.Log(result)
t.Log(mapReplace)
assert.Contains(t, result, "and")
}
func TestReplaceBiggestBracketContent5(t *testing.T) {
expr := "1 or 2 and ( 3 or ( 2 and 4 ) )"
result, _ := replaceBiggestBracketContentAtOnce(expr, make(map[string]string))
t.Log(result)
assert.Contains(t, result, "1 or 2 and")
}
func TestLogicToTree2(t *testing.T) {
logic := "1 and 2 and ( 3 or not ( 2 and 4 ) )"
head := logicToTree(logic)
traverseTreeInLayer(head)
assert.NotNil(t, head)
}
func traverseTreeInLayer(head *Node) {
var buf []*Node
var i int
buf = append(buf, head)
for {
if i < len(buf) {
fmt.Printf("%+v\n", buf[i])
if buf[i].Children != nil {
buf = append(buf, buf[i].Children...)
}
i++
} else {
break
}
}
}
func traverseTreeInPostOrder(head *Node) {
children := head.Children
for _, child := range children {
traverseTreeInPostOrder(child)
}
if head.Leaf {
fmt.Printf("%+v\n", head)
return
}
fmt.Printf("%+v\n", head)
}
func TestLogicToTree3(t *testing.T) {
logic := "1 and 2 and ( 3 or not ( 2 and 4 ) )"
head := logicToTree(logic)
traverseTreeInPostOrder(head)
assert.NotNil(t, head)
}