-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
166 lines (142 loc) · 4.53 KB
/
client_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package cooperate_test
import (
"reflect"
"testing"
"github.com/tylerchr/cooperate"
"github.com/tylerchr/cooperate/text"
)
func TestClient_Buffering(t *testing.T) {
cases := []struct {
ExistingDocument string
Operations []cooperate.Operation
ExpectedDocument string
ExpectedInFlight cooperate.Operation
ExpectedBuffer cooperate.Operation
}{
// first operation applied is immediately placed in-flight;
// future operations are held in the buffer
{
ExistingDocument: "",
Operations: []cooperate.Operation{
cooperate.Operation([]cooperate.Action{
text.InsertAction("lorem"),
}),
cooperate.Operation([]cooperate.Action{
text.RetainAction(5),
text.InsertAction(" ipsum"),
}),
},
ExpectedDocument: "lorem ipsum",
ExpectedInFlight: cooperate.Operation([]cooperate.Action{
text.InsertAction("lorem"),
}),
ExpectedBuffer: cooperate.Operation([]cooperate.Action{
text.RetainAction(5),
text.InsertAction(" ipsum"),
}),
},
// buffer composition
{
ExistingDocument: "",
Operations: []cooperate.Operation{
cooperate.Operation([]cooperate.Action{
text.InsertAction("lorem"),
}),
cooperate.Operation([]cooperate.Action{
text.RetainAction(5),
text.InsertAction(" ipsum"),
}),
cooperate.Operation([]cooperate.Action{
text.RetainAction(11),
text.InsertAction(" dolor"),
}),
},
ExpectedDocument: "lorem ipsum dolor",
ExpectedInFlight: cooperate.Operation([]cooperate.Action{
text.InsertAction("lorem"),
}),
ExpectedBuffer: cooperate.Operation([]cooperate.Action{
text.RetainAction(5),
text.InsertAction(" ipsum dolor"),
}),
},
}
for i, c := range cases {
client := &cooperate.Client{
Document: text.NewTextDocument(c.ExistingDocument),
ExpandReducer: text.TextHandler{},
ComposeTransformer: text.TextHandler{},
}
for _, op := range c.Operations {
client.ApplyLocal(op)
}
if client.Document.(*text.TextDocument).String() != c.ExpectedDocument {
t.Errorf("[case %d] unexpected document: expected '%s' but got '%s'", i, c.ExpectedDocument, client.Document.(*text.TextDocument).String())
}
if !reflect.DeepEqual(client.InFlight, c.ExpectedInFlight) {
t.Errorf("[case %d] unexpected in-flight operation: expected '%s' but got '%s'", i, c.ExpectedInFlight, client.InFlight)
}
if !reflect.DeepEqual(client.Buffer, c.ExpectedBuffer) {
t.Errorf("[case %d] unexpected buffered operation: expected '%s' but got '%s'", i, c.ExpectedBuffer, client.Buffer)
}
}
}
func TestClient_ApplyReceived(t *testing.T) {
cases := []struct {
ExistingDocument string
ExistingInFlight cooperate.Operation
ExistingBuffer cooperate.Operation
Operations []cooperate.Operation
ExpectedDocument string
ExpectedInFlight cooperate.Operation
ExpectedBuffer cooperate.Operation
}{
// first operation applied is immediately placed in-flight;
// future operations are held in the buffer
{
ExistingDocument: "redblue",
ExistingInFlight: cooperate.Operation([]cooperate.Action{
text.InsertAction("red"),
}),
ExistingBuffer: cooperate.Operation([]cooperate.Action{
text.RetainAction(3),
text.InsertAction("blue"),
}),
Operations: []cooperate.Operation{
cooperate.Operation([]cooperate.Action{
text.InsertAction("green"),
}),
},
ExpectedDocument: "greenredblue",
ExpectedInFlight: cooperate.Operation([]cooperate.Action{
text.RetainAction(5),
text.InsertAction("red"),
}),
ExpectedBuffer: cooperate.Operation([]cooperate.Action{
text.RetainAction(8),
text.InsertAction("blue"),
}),
},
}
for i, c := range cases {
client := &cooperate.Client{
Document: text.NewTextDocument(c.ExistingDocument),
InFlight: c.ExistingInFlight,
Buffer: c.ExistingBuffer,
ExpandReducer: text.TextHandler{},
ComposeTransformer: text.TextHandler{},
}
for _, op := range c.Operations {
client.ApplyReceived(op)
}
if client.Document.(*text.TextDocument).String() != c.ExpectedDocument {
t.Errorf("[case %d] unexpected document: expected '%s' but got '%s'", i, c.ExpectedDocument, client.Document.(*text.TextDocument).String())
}
if !reflect.DeepEqual(client.InFlight, c.ExpectedInFlight) {
t.Errorf("[case %d] unexpected in-flight operation: expected '%s' but got '%s'", i, c.ExpectedInFlight, client.InFlight)
}
if !reflect.DeepEqual(client.Buffer, c.ExpectedBuffer) {
t.Errorf("[case %d] unexpected buffered operation: expected '%s' but got '%s'", i, c.ExpectedBuffer, client.Buffer)
}
}
}