@@ -120,3 +120,95 @@ func TestRegister(t *testing.T) {
120
120
}
121
121
}
122
122
}
123
+
124
+ func TestSubscribe (t * testing.T ) {
125
+ p , cleanup := newChildProcess (t )
126
+ t .Cleanup (func () {
127
+ cleanup ()
128
+ })
129
+
130
+ const event1 = "event1"
131
+ eventFn1 := func (t * testing.T , v * nvim.Nvim ) error {
132
+ return v .RegisterHandler (event1 , func (event ... interface {}) {
133
+ if event [0 ] != int64 (1 ) {
134
+ t .Fatalf ("expected event[0] is 1 but got %d" , event [0 ])
135
+ }
136
+ if event [1 ] != int64 (2 ) {
137
+ t .Fatalf ("expected event[1] is 2 but got %d" , event [1 ])
138
+ }
139
+ if event [2 ] != int64 (3 ) {
140
+ t .Fatalf ("expected event[2] is 3 but got %d" , event [2 ])
141
+ }
142
+ })
143
+ }
144
+ const event2 = "event2"
145
+ eventFn2 := func (t * testing.T , v * nvim.Nvim ) error {
146
+ return v .RegisterHandler (event1 , func (event ... interface {}) {
147
+ if event [0 ] != int64 (4 ) {
148
+ t .Fatalf ("expected event[0] is 4 but got %d" , event [0 ])
149
+ }
150
+ if event [1 ] != int64 (5 ) {
151
+ t .Fatalf ("expected event[1] is 5 but got %d" , event [1 ])
152
+ }
153
+ if event [2 ] != int64 (6 ) {
154
+ t .Fatalf ("expected event[2] is 6 but got %d" , event [2 ])
155
+ }
156
+ })
157
+ }
158
+ p .Handle (event1 , func () error { return eventFn1 (t , p .Nvim ) })
159
+ p .Handle (event2 , func () error { return eventFn2 (t , p .Nvim ) })
160
+
161
+ if err := p .RegisterForTests (); err != nil {
162
+ t .Fatal (err )
163
+ }
164
+
165
+ if err := p .Nvim .Subscribe (event1 ); err != nil {
166
+ t .Fatal (err )
167
+ }
168
+ b := p .Nvim .NewBatch ()
169
+ b .Subscribe (event2 )
170
+ if err := b .Execute (); err != nil {
171
+ t .Fatal (err )
172
+ }
173
+
174
+ // warm-up
175
+ var result int
176
+ if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q)` , event1 ), & result ); err != nil {
177
+ t .Fatal (err )
178
+ }
179
+ if result != 1 {
180
+ t .Fatalf ("expect 1 but got %d" , result )
181
+ }
182
+
183
+ var result2 int
184
+ if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 1, 2, 3)` , event1 ), & result2 ); err != nil {
185
+ t .Fatal (err )
186
+ }
187
+ if result2 != 1 {
188
+ t .Fatalf ("expect 1 but got %d" , result2 )
189
+ }
190
+
191
+ var result3 int
192
+ if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 4, 5, 6)` , event2 ), & result3 ); err != nil {
193
+ t .Fatal (err )
194
+ }
195
+ if result3 != 1 {
196
+ t .Fatalf ("expect 1 but got %d" , result3 )
197
+ }
198
+
199
+ if err := p .Nvim .Unsubscribe (event1 ); err != nil {
200
+ t .Fatal (err )
201
+ }
202
+ b .Unsubscribe (event2 )
203
+ if err := b .Execute (); err != nil {
204
+ t .Fatal (err )
205
+ }
206
+
207
+ if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 7, 8, 9)` , event1 ), nil ); err != nil {
208
+ t .Fatal (err )
209
+ }
210
+
211
+ if err := p .Nvim .Eval (fmt .Sprintf (`rpcnotify(0, %q, 10, 11, 12)` , event2 ), nil ); err != nil {
212
+ t .Fatal (err )
213
+ }
214
+ }
0 commit comments