-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview_patch_test.mbt
More file actions
197 lines (182 loc) · 5.34 KB
/
view_patch_test.mbt
File metadata and controls
197 lines (182 loc) · 5.34 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Tests for ViewPatch, Decoration, and Diagnostic JSON serialization.
///|
test "Decoration to_json produces object-based JSON" {
let dec = @protocol.Decoration(from=0, to=5, css_class="keyword")
let s = dec.to_json().stringify()
inspect(
s,
content="{\"from\":0,\"to\":5,\"css_class\":\"keyword\",\"data\":null,\"widget\":false}",
)
}
///|
test "Decoration with data and widget to_json" {
let dec = @protocol.Decoration(
from=10,
to=15,
css_class="peer-cursor",
data="Alice",
widget=true,
)
let s = dec.to_json().stringify()
assert_true(s.contains("\"data\":\"Alice\""))
assert_true(s.contains("\"widget\":true"))
assert_true(s.contains("\"css_class\":\"peer-cursor\""))
}
///|
test "Diagnostic to_json produces object-based JSON" {
let diag = @protocol.Diagnostic(
from=0,
to=3,
severity=@protocol.SevError,
message="unbound var",
)
let s = diag.to_json().stringify()
inspect(
s,
content="{\"from\":0,\"to\":3,\"severity\":\"error\",\"message\":\"unbound var\"}",
)
}
///|
test "ViewPatch::TextChange to_json" {
let patch = @protocol.ViewPatch::TextChange(from=1, to=5, insert="hello")
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"TextChange\""))
assert_true(s.contains("\"from\":1"))
assert_true(s.contains("\"to\":5"))
assert_true(s.contains("\"insert\":\"hello\""))
}
///|
test "ViewPatch::ReplaceNode to_json" {
let node = @protocol.ViewNode(
id=@core.NodeId::from_int(42),
kind_tag="Var",
label="x",
text_range=(0, 1),
)
let patch = @protocol.ViewPatch::ReplaceNode(
node_id=@core.NodeId::from_int(42),
node~,
)
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"ReplaceNode\""))
assert_true(s.contains("\"node_id\":42"))
assert_true(s.contains("\"kind_tag\":\"Var\""))
}
///|
test "ViewPatch::InsertChild to_json" {
let child = @protocol.ViewNode(
id=@core.NodeId::from_int(10),
kind_tag="Int",
label="5",
text_range=(3, 4),
)
let patch = @protocol.ViewPatch::InsertChild(
parent_id=@core.NodeId::from_int(1),
index=2,
child~,
)
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"InsertChild\""))
assert_true(s.contains("\"parent_id\":1"))
assert_true(s.contains("\"index\":2"))
}
///|
test "ViewPatch::RemoveChild to_json" {
let patch = @protocol.ViewPatch::RemoveChild(
parent_id=@core.NodeId::from_int(1),
index=0,
child_id=@core.NodeId::from_int(5),
)
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"RemoveChild\""))
assert_true(s.contains("\"parent_id\":1"))
assert_true(s.contains("\"index\":0"))
assert_true(s.contains("\"child_id\":5"))
}
///|
test "ViewPatch::UpdateNode to_json with text" {
let patch = @protocol.ViewPatch::UpdateNode(
node_id=@core.NodeId::from_int(7),
label="42",
css_class="Int",
text=Some("42"),
)
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"UpdateNode\""))
assert_true(s.contains("\"node_id\":7"))
assert_true(s.contains("\"label\":\"42\""))
assert_true(s.contains("\"text\":\"42\""))
}
///|
test "ViewPatch::UpdateNode to_json without text" {
let patch = @protocol.ViewPatch::UpdateNode(
node_id=@core.NodeId::from_int(7),
label="x",
css_class="Var",
text=None,
)
let s = patch.to_json().stringify()
assert_true(s.contains("\"text\":null"))
}
///|
test "ViewPatch::SetDecorations to_json" {
let decs = [
@protocol.Decoration(from=0, to=3, css_class="keyword"),
@protocol.Decoration(from=4, to=5, css_class="ident"),
]
let patch = @protocol.ViewPatch::SetDecorations(decorations=decs)
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"SetDecorations\""))
assert_true(s.contains("\"decorations\":["))
assert_true(s.contains("\"keyword\""))
assert_true(s.contains("\"ident\""))
}
///|
test "ViewPatch::SetDiagnostics to_json" {
let diags = [
@protocol.Diagnostic(
from=0,
to=3,
severity=@protocol.SevError,
message="bad",
),
]
let patch = @protocol.ViewPatch::SetDiagnostics(diagnostics=diags)
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"SetDiagnostics\""))
assert_true(s.contains("\"diagnostics\":["))
assert_true(s.contains("\"severity\":\"error\""))
}
///|
test "ViewPatch::SetSelection to_json" {
let patch = @protocol.ViewPatch::SetSelection(anchor=5, head=10)
let s = patch.to_json().stringify()
inspect(s, content="{\"type\":\"SetSelection\",\"anchor\":5,\"head\":10}")
}
///|
test "ViewPatch::SelectNode to_json" {
let patch = @protocol.ViewPatch::SelectNode(
node_id=@core.NodeId::from_int(99),
)
let s = patch.to_json().stringify()
inspect(s, content="{\"type\":\"SelectNode\",\"node_id\":99}")
}
///|
test "ViewPatch::FullTree to_json with node" {
let root = @protocol.ViewNode(
id=@core.NodeId::from_int(1),
kind_tag="Module",
label="module",
text_range=(0, 10),
)
let patch = @protocol.ViewPatch::FullTree(root=Some(root))
let s = patch.to_json().stringify()
assert_true(s.contains("\"type\":\"FullTree\""))
assert_true(s.contains("\"root\":{"))
}
///|
test "ViewPatch::FullTree to_json with null root" {
let patch = @protocol.ViewPatch::FullTree(root=None)
let s = patch.to_json().stringify()
inspect(s, content="{\"type\":\"FullTree\",\"root\":null}")
}