/
server_test.go
101 lines (86 loc) · 1.95 KB
/
server_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
package cooperate_test
import (
"testing"
"github.com/tylerchr/cooperate"
"github.com/tylerchr/cooperate/text"
)
func TestServer(t *testing.T) {
s := &cooperate.Server{
Document: text.NewTextDocument(""),
History: &cooperate.MemoryHistory{},
}
ops := []struct {
Root int
Operation cooperate.Operation
}{
{
Root: 0,
Operation: cooperate.Operation([]cooperate.Action{
text.InsertAction("foo"),
}),
},
{
Root: 1,
Operation: cooperate.Operation([]cooperate.Action{
text.RetainAction(3),
text.InsertAction(" "),
}),
},
{
Root: 2,
Operation: cooperate.Operation([]cooperate.Action{
text.RetainAction(4),
text.InsertAction("bar"),
}),
},
}
for _, op := range ops {
if err := s.Apply(op.Root, op.Operation); err != nil {
t.Fatalf("apply error: %s", err)
}
}
if doc := s.Document.(*text.TextDocument); doc.String() != "foo bar" {
t.Errorf("unexpected document: %s\n", doc.String())
}
}
func TestServer_OutOfOrder(t *testing.T) {
s := &cooperate.Server{
Document: text.NewTextDocument(""),
History: &cooperate.MemoryHistory{},
ExpandReducer: &text.TextHandler{},
ComposeTransformer: &text.TextHandler{},
}
ops := []struct {
Root int
Operation cooperate.Operation
}{
{
Root: 0,
Operation: cooperate.Operation([]cooperate.Action{
text.InsertAction("red"),
}),
},
{
Root: 1,
Operation: cooperate.Operation([]cooperate.Action{
text.RetainAction(3),
text.InsertAction("blue"),
}),
},
{
Root: 0,
Operation: cooperate.Operation([]cooperate.Action{
text.InsertAction("green"),
}),
},
}
for _, op := range ops {
if err := s.Apply(op.Root, op.Operation); err != nil {
t.Fatalf("apply error: %s", err)
}
}
if doc := s.Document.(*text.TextDocument); doc.String() != "greenredblue" {
t.Errorf("unexpected document: %s\n", doc.String())
}
t.Logf("Server {seqno=%d}\n", s.History.SequenceNumber())
}