-
Notifications
You must be signed in to change notification settings - Fork 7
/
frame_test.go
129 lines (119 loc) · 2.81 KB
/
frame_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ron
import (
"testing"
)
func TestFrame_Split(t *testing.T) {
frame := ParseFrameString("*lww#id1!:val=1*#id2:0!:val=2")
h1 := "*lww#id1!:val=1"
h2 := "*lww#id2!:val=2"
frame.Next()
frame.Next()
if frame.Term() != TERM_HEADER {
t.Fail()
return
}
id1, id2 := frame.Split2()
if id1.String() != h1 {
t.Fail()
t.Logf("\nneed: %s\nhave: %s\n", h1, id1)
}
if id2.String() != h2 {
t.Fail()
t.Logf("\nneed: %s\nhave: %s\n", h2, id2)
}
if id2.Type() != NewName("lww") {
t.Fail()
}
}
func TestBatchFrames(t *testing.T) {
frame1 := "*lww#A@1!:a=1:b=2:c=3"
frame2 := "*lww#A@2!:d=4"
var batch Batch
batch = append(batch, ParseFrameString(frame1))
batch = append(batch, ParseFrameString(frame2))
frame12 := batch.Join()
batchStr := "*lww#A@1!:a=1:b=2:c=3*#@2:0!:d=4"
if frame12.String() != batchStr {
t.Logf("\n%s != \n%s\n", frame12.String(), batchStr)
t.Fail()
return
}
b2 := frame12.Split()
if len(b2) != 2 {
t.Fail()
t.Log("length", len(b2))
return
}
if b2[0].String() != frame1 {
t.Fail()
t.Logf("%s != %s\n", b2[0].String(), frame1)
}
if b2[1].String() != frame2 {
t.Fail()
t.Logf("%s != %s\n", b2[0].String(), frame1)
}
}
func TestFrame_SplitMultiframe(t *testing.T) {
multiStr := "*lww#test!:a=1*#best:0!:b=2:c=3*#:d=4;"
splits := []string{
"*lww#test!:a=1",
"*lww#best!:b=2:c=3",
"*lww#best:d=4",
}
multi := ParseFrameString(multiStr)
monos := multi.Split()
for i := 0; i < len(monos); i++ {
if monos[i].String() != splits[i] {
t.Fail()
t.Logf("split fail at %d:\n'%s'\nshould be\n'%s'\n", i, monos[i].String(), splits[i])
}
}
}
func TestBatch_Equal(t *testing.T) {
b1 := ParseStringBatch([]string{"*one", "*two"})
b2 := ParseStringBatch([]string{"*one*two"})
eq := b1.Equal(b2)
if !eq {
t.Fail()
}
b2 = append(b2, ParseFrameString("*three"))
eq = b1.Equal(b2)
if eq {
t.Fail()
}
}
func TestFrame_Copy(t *testing.T) {
a := ParseFrameString("*~'comment' *lww#obj!")
b := a
if b.Type() != COMMENT_UUID {
t.Log("improper copy")
t.Fail()
}
b.Next()
if a.Type() != COMMENT_UUID {
t.Log("the copy is still linked")
t.Fail()
}
}
func TestFrame_Split2(t *testing.T) {
frame := ParseFrameString("*rga#test@4!@1'A'@2'B'*#@4:rm!:3,")
split := frame.Split()
eq := split.Equal(Batch{frame})
if !eq {
t.Fail()
t.Logf("split fail, \n%s\nbecame\n%s", frame.String(), split.String())
}
}
func TestFrame_Empties(t *testing.T) {
head := ParseFrameString("*lww #obj @)4+UserAlice ?\n* # @ !\n")
next := ParseFrameString("*lww #obj @)4+UserAlice ?\n* # @ !\n")
next.Next()
if head.Type() != next.Type() || head.Object() != next.Object() || head.Event() != next.Event() || head.Ref() != next.Ref() {
t.Fail()
t.Logf("failed default")
}
if next.Term() != TERM_HEADER {
t.Fail()
t.Logf("incorrect term: %v != %v", next.Term(), TERM_HEADER)
}
}