summaryrefslogtreecommitdiff
path: root/src/testdir/test_netbeans.vim
blob: c4851ae1addb3e95ee8c7fb4392a4a17838b42ac (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
" Test the netbeans interface.

source check.vim
CheckFeature netbeans_intg

source shared.vim

let s:python = PythonProg()
if s:python == ''
  throw 'Skipped: python program missing'
endif

" Run "testfunc" after starting the server and stop the server afterwards.
func s:run_server(testfunc, ...)
  call RunServer('test_netbeans.py', a:testfunc, a:000)
endfunc

" Wait for an exception (error) to be thrown. This is used to check whether a
" message from the netbeans server causes an error. It takes some time for Vim
" to process a netbeans message. So a sleep is used below to account for this.
func WaitForError(errcode)
  let save_exception = ''
  for i in range(200)
    try
      sleep 5m
    catch
      let save_exception = v:exception
      break
    endtry
  endfor
  call assert_match(a:errcode, save_exception)
endfunc

" Read the "Xnetbeans" file and filter out geometry messages.
func ReadXnetbeans()
  let l = readfile("Xnetbeans")
  " Xnetbeans may include '0:geometry=' messages on GUI environment if window
  " position, size, or z order are changed.  Remove these messages because
  " will causes troubles on check.
  return filter(l, 'v:val !~ "^0:geometry="')
endfunc

func Nb_basic(port)
  call delete("Xnetbeans")
  call writefile([], "Xnetbeans")

  " Last line number in the Xnetbeans file. Used to verify the result of the
  " communication with the netbeans server
  let g:last = 0

  " Establish the connection with the netbeans server
  exe 'nbstart :localhost:' .. a:port .. ':bunny'
  call assert_true(has("netbeans_enabled"))
  call WaitFor('len(ReadXnetbeans()) > (g:last + 2)')
  let l = ReadXnetbeans()
  call assert_equal(['AUTH bunny',
        \ '0:version=0 "2.5"',
        \ '0:startupDone=0'], l[-3:])
  let g:last += 3

  " Trying to connect again to netbeans server should fail
  call assert_fails("exe 'nbstart :localhost:' . a:port . ':bunny'", 'E511:')

  " Open the command buffer to communicate with the server
  split Xcmdbuf
  let cmdbufnr = bufnr()
  call WaitFor('len(ReadXnetbeans()) > (g:last + 2)')
  let l = ReadXnetbeans()
  call assert_equal('0:fileOpened=0 "Xcmdbuf" T F',
        \ substitute(l[-3], '".*/', '"', ''))
  call assert_equal('send: 1:putBufferNumber!15 "Xcmdbuf"',
        \ substitute(l[-2], '".*/', '"', ''))
  call assert_equal('1:startDocumentListen!16', l[-1])
  let g:last += 3

  " Keep the command buffer loaded for communication
  hide

  sleep 1m

  " getCursor test
  call writefile(['foo bar', 'foo bar', 'foo bar'], 'Xfile1')
  split Xfile1
  call cursor(3, 4)
  sleep 10m
  call appendbufline(cmdbufnr, '$', 'getCursor_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 5)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:getCursor/30', '30 -1 3 3 19'], l[-2:])
  let g:last += 5

  " Test for E627
  call appendbufline(cmdbufnr, '$', 'E627_Test')
  call WaitForError('E627:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0 setReadOnly!31', l[-1])
  let g:last += 3

  " Test for E628
  call appendbufline(cmdbufnr, '$', 'E628_Test')
  call WaitForError('E628:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setReadOnly 32', l[-1])
  let g:last += 3

  " Test for E632
  call appendbufline(cmdbufnr, '$', 'E632_Test')
  call WaitForError('E632:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:getLength/33', '33 0'], l[-2:])
  let g:last += 4

  " Test for E633
  call appendbufline(cmdbufnr, '$', 'E633_Test')
  call WaitForError('E633:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:getText/34', '34 '], l[-2:])
  let g:last += 4

  " Test for E634
  call appendbufline(cmdbufnr, '$', 'E634_Test')
  call WaitForError('E634:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:remove/35 1 1', '35'], l[-2:])
  let g:last += 4

  " Test for E635
  call appendbufline(cmdbufnr, '$', 'E635_Test')
  call WaitForError('E635:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:insert/36 0 "line1\n"', '36'], l[-2:])
  let g:last += 4

  " Test for E636
  call appendbufline(cmdbufnr, '$', 'E636_Test')
  call WaitForError('E636:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:create!37', l[-1])
  let g:last += 3

  " Test for E637
  call appendbufline(cmdbufnr, '$', 'E637_Test')
  call WaitForError('E637:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:startDocumentListen!38', l[-1])
  let g:last += 3

  " Test for E638
  call appendbufline(cmdbufnr, '$', 'E638_Test')
  call WaitForError('E638:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:stopDocumentListen!39', l[-1])
  let g:last += 3

  " Test for E639
  call appendbufline(cmdbufnr, '$', 'E639_Test')
  call WaitForError('E639:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setTitle!40 "Title"', l[-1])
  let g:last += 3

  " Test for E640
  call appendbufline(cmdbufnr, '$', 'E640_Test')
  call WaitForError('E640:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:initDone!41', l[-1])
  let g:last += 3

  " Test for E641
  call appendbufline(cmdbufnr, '$', 'E641_Test')
  call WaitForError('E641:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:putBufferNumber!42 "XSomeBuf"', l[-1])
  let g:last += 3

  " Test for E642
  call appendbufline(cmdbufnr, '$', 'E642_Test')
  call WaitForError('E642:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 9:putBufferNumber!43 "XInvalidBuf"', l[-1])
  let g:last += 3

  " Test for E643
  call appendbufline(cmdbufnr, '$', 'E643_Test')
  call WaitForError('E643:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setFullName!44 "XSomeBuf"', l[-1])
  let g:last += 3

  enew!

  " Test for E644
  call appendbufline(cmdbufnr, '$', 'E644_Test')
  call WaitForError('E644:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:editFile!45 "Xfile3"', l[-1])
  let g:last += 3

  " Test for E645 (shown only when verbose > 0)
  call appendbufline(cmdbufnr, '$', 'E645_Test')
  set verbose=1
  call WaitForError('E645:')
  set verbose&
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setVisible!46 T', l[-1])
  let g:last += 3

  " Test for E646 (shown only when verbose > 0)
  call appendbufline(cmdbufnr, '$', 'E646_Test')
  set verbose=1
  call WaitForError('E646:')
  set verbose&
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setModified!47 T', l[-1])
  let g:last += 3

  " Test for E647
  call appendbufline(cmdbufnr, '$', 'E647_Test')
  call WaitForError('E647:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setDot!48 1/1', l[-1])
  let g:last += 3

  " Test for E648
  call appendbufline(cmdbufnr, '$', 'E648_Test')
  call WaitForError('E648:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:close!49', l[-1])
  let g:last += 3

  " Test for E650
  call appendbufline(cmdbufnr, '$', 'E650_Test')
  call WaitForError('E650:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:defineAnnoType!50 1 "abc" "a" "a" 1 1', l[-1])
  let g:last += 3

  " Test for E651
  call appendbufline(cmdbufnr, '$', 'E651_Test')
  call WaitForError('E651:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:addAnno!51 1 1 1 1', l[-1])
  let g:last += 3

  " Test for E652
  call appendbufline(cmdbufnr, '$', 'E652_Test')
  call WaitForError('E652:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:getAnno/52 8', '52 0'], l[-2:])
  let g:last += 4

  " editFile test
  call writefile(['foo bar1', 'foo bar2', 'foo bar3'], 'Xfile3')
  call appendbufline(cmdbufnr, '$', 'editFile_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:editFile!53 "Xfile3"', l[-2])
  call assert_match('0:fileOpened=0 ".*/Xfile3" T F', l[-1])
  call assert_equal('Xfile3', bufname())
  let g:last += 4

  " getLength test
  call appendbufline(cmdbufnr, '$', 'getLength_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 2:getLength/54', '54 27'], l[-2:])
  let g:last += 4

  " getModified test
  call appendbufline(cmdbufnr, '$', 'getModified_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 2:getModified/55', '55 0'], l[-2:])
  let g:last += 4

  " getText test
  call appendbufline(cmdbufnr, '$', 'getText_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 2:getText/56',
        \ '56 "foo bar1\nfoo bar2\nfoo bar3\n"'], l[-2:])
  let g:last += 4

  " setDot test with lnum/col
  call cursor(1, 1)
  call appendbufline(cmdbufnr, '$', 'setDot_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:setDot!57 3/6', l[-1])
  call assert_equal([0, 3, 7, 0], getpos('.'))
  let g:last += 3

  " setDot test with an offset
  call cursor(1, 1)
  call appendbufline(cmdbufnr, '$', 'setDot2_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:setDot!57 9', l[-1])
  call assert_equal([0, 2, 1, 0], getpos('.'))
  let g:last += 3

  " startDocumentListen test
  call appendbufline(cmdbufnr, '$', 'startDocumentListen_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:startDocumentListen!58', l[-1])
  let g:last += 3

  " make some changes to the buffer and check whether the netbeans server
  " received the notifications
  call append(2, 'blue sky')
  1d
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_match('2:insert=\d\+ 18 "blue sky"', l[-3])
  call assert_match('2:insert=\d\+ 26 "\\n"', l[-2])
  call assert_match('2:remove=\d\+ 0 9', l[-1])
  let g:last += 3

  " Change case using the ~ command with 'whichwrap' containing '~'
  set whichwrap+=~
  normal 2G$~
  set whichwrap&
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 2)')
  let l = ReadXnetbeans()
  call assert_match('2:remove=\d\+ 16 1', l[-4])
  call assert_match('2:insert=\d\+ 16 "Y"', l[-3])
  call assert_match('2:remove=\d\+ 18 0', l[-2])
  call assert_match('2:insert=\d\+ 18 ""', l[-1])
  let g:last += 4

  " Test for replacing spaces with a tab character using 'softtabstop' and
  " 'noexpandtab'
  setlocal softtabstop=4
  setlocal noexpandtab
  exe "normal I\<Tab>\<Tab>"
  setlocal expandtab&
  setlocal softtabstop&
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 18)')
  let l = ReadXnetbeans()
  call assert_match('2:insert=\d\+ 18 "        foo bar3"', l[-3])
  call assert_match('2:remove=\d\+ 26 8', l[-2])
  call assert_match('2:insert=\d\+ 26 "\t"', l[-1])
  let g:last += 18

  " stopDocumentListen test
  call appendbufline(cmdbufnr, '$', 'stopDocumentListen_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:stopDocumentListen!59', l[-1])
  let g:last += 3

  " Wait for vim to process the previous netbeans message
  sleep 1m

  " modify the buffer and make sure that the netbeans server is not notified
  call append(2, 'clear sky')
  1d

  " defineAnnoType test
  call appendbufline(cmdbufnr, '$', 'define_anno_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:defineAnnoType!60 1 "s1" "x" "=>" blue none', l[-1])
  sleep 1m
  call assert_equal({'name': '1', 'texthl': 'NB_s1', 'text': '=>'},
        \ sign_getdefined()[0])
  let g:last += 3

  " defineAnnoType with a long color name
  call appendbufline(cmdbufnr, '$', 'E532_Test')
  call WaitForError('E532:')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:defineAnnoType!61 1 "s1" "x" "=>" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa none', l[-1])
  let g:last += 3

  " addAnno test
  call appendbufline(cmdbufnr, '$', 'add_anno_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:addAnno!62 1 1 2/1 0', l[-1])
  sleep 1m
  call assert_equal([{'lnum': 2, 'id': 1, 'name': '1', 'priority': 10,
        \ 'group': ''}], sign_getplaced()[0].signs)
  let g:last += 3

  " getAnno test
  call appendbufline(cmdbufnr, '$', 'get_anno_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 2:getAnno/63 1', '63 2'], l[-2:])
  let g:last += 4

  " removeAnno test
  call appendbufline(cmdbufnr, '$', 'remove_anno_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 2:removeAnno!64 1', l[-1])
  sleep 1m
  call assert_equal([], sign_getplaced())
  let g:last += 3

  " getModified test to get the number of modified buffers
  call appendbufline(cmdbufnr, '$', 'getModifiedAll_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:getModified/65', '65 2'], l[-2:])
  let g:last += 4

  let bufcount = len(getbufinfo())

  " create test to create a new buffer
  call appendbufline(cmdbufnr, '$', 'create_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:create!66', l[-1])
  " Wait for vim to process the previous netbeans message
  sleep 10m
  call assert_equal(bufcount + 1, len(getbufinfo()))
  let g:last += 3

  " setTitle test
  call appendbufline(cmdbufnr, '$', 'setTitle_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setTitle!67 "Xfile4"', l[-1])
  let g:last += 3

  " setFullName test
  call appendbufline(cmdbufnr, '$', 'setFullName_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 5)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setFullName!68 "Xfile4"', l[-3])
  call assert_match('0:fileOpened=0 ".*/Xfile4" T F', l[-1])
  call assert_equal('Xfile4', bufname())
  let g:last += 5

  " initDone test
  call appendbufline(cmdbufnr, '$', 'initDone_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:initDone!69', l[-1])
  let g:last += 3

  " setVisible test
  hide enew
  call appendbufline(cmdbufnr, '$', 'setVisible_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setVisible!70 T', l[-1])
  let g:last += 3

  " setModtime test
  call appendbufline(cmdbufnr, '$', 'setModtime_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setModtime!71 6', l[-1])
  let g:last += 3

  " insert test
  call appendbufline(cmdbufnr, '$', 'insert_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 3:insert/72 0 "line1\nline2\n"', '72'], l[-2:])
  call assert_equal(['line1', 'line2'], getline(1, '$'))
  let g:last += 4

  " remove test
  call appendbufline(cmdbufnr, '$', 'remove_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 3:remove/73 3 4', '73'], l[-2:])
  call assert_equal(['linine2'], getline(1, '$'))
  let g:last += 4

  " remove with invalid offset
  call appendbufline(cmdbufnr, '$', 'remove_invalid_offset_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 3:remove/74 900 4', '74 !bad position'], l[-2:])
  let g:last += 4

  " remove with invalid count
  call appendbufline(cmdbufnr, '$', 'remove_invalid_count_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 3:remove/75 1 800', '75 !bad count'], l[-2:])
  let g:last += 4

  " guard test
  %d
  call setline(1, ['foo bar', 'foo bar', 'foo bar'])
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 8)')
  let g:last += 8

  call appendbufline(cmdbufnr, '$', 'guard_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:guard!76 8 7', l[-1])
  sleep 1m
  " second line is guarded. Try modifying the line
  call assert_fails('normal 2GIbaz', 'E463:')
  call assert_fails('normal 2GAbaz', 'E463:')
  call assert_fails('normal dd', 'E463:')
  call assert_equal([{'name': '1', 'texthl': 'NB_s1', 'text': '=>'},
        \ {'name': '10000', 'linehl': 'NBGuarded'}],
        \ sign_getdefined())
  let s = sign_getplaced()[0].signs[0]
  call assert_equal(2, s.lnum)
  call assert_equal('10000', s.name)
  let g:last += 3

  " setModified test
  call appendbufline(cmdbufnr, '$', 'setModified_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setModified!77 T', l[-1])
  sleep 1m
  call assert_equal(1, &modified)
  let g:last += 3

  " clear setModified test
  call appendbufline(cmdbufnr, '$', 'setModifiedClear_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setModified!77 F', l[-1])
  sleep 1m
  call assert_equal(0, &modified)
  let g:last += 3

  " insertDone test
  let v:statusmsg = ''
  call appendbufline(cmdbufnr, '$', 'insertDone_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:insertDone!78 T F', l[-1])
  sleep 1m
  call assert_match('.*/Xfile4" 3L, 0B', v:statusmsg)
  let g:last += 3

  " saveDone test
  let v:statusmsg = ''
  call appendbufline(cmdbufnr, '$', 'saveDone_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:saveDone!79', l[-1])
  sleep 1m
  call assert_match('.*/Xfile4" 3L, 0B', v:statusmsg)
  let g:last += 3

  " unimplemented command test
  call appendbufline(cmdbufnr, '$', 'invalidcmd_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:invalidcmd!80', l[-1])
  let g:last += 3

  " unimplemented function test
  call appendbufline(cmdbufnr, '$', 'invalidfunc_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 3:invalidfunc/81', '81'], l[-2:])
  let g:last += 4

  " Test for removeAnno cmd failure
  call appendbufline(cmdbufnr, '$', 'removeAnno_fail_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:removeAnno/82 1', '82'], l[-2:])
  let g:last += 4

  " Test for guard cmd failure
  call appendbufline(cmdbufnr, '$', 'guard_fail_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:guard/83 1 1', '83'], l[-2:])
  let g:last += 4

  " Test for save cmd failure
  call appendbufline(cmdbufnr, '$', 'save_fail_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:save/84', '84'], l[-2:])
  let g:last += 4

  " Test for netbeansBuffer cmd failure
  call appendbufline(cmdbufnr, '$', 'netbeansBuffer_fail_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal(['send: 0:netbeansBuffer/85 T', '85'], l[-2:])
  let g:last += 4

  " nbkey test
  call cursor(3, 3)
  nbkey "\<C-F2>"
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal(['3:newDotAndMark=85 18 18',
        \ '3:keyCommand=85 ""\<C-F2>""',
        \ '3:keyAtPos=85 ""\<C-F2>"" 18 3/2'], l[-3:])
  let g:last += 3

  " setExitDelay test
  call appendbufline(cmdbufnr, '$', 'setExitDelay_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:setExitDelay!86 2', l[-1])
  let g:last += 3

  " setReadonly test
  call appendbufline(cmdbufnr, '$', 'setReadOnly_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setReadOnly!87 T', l[-1])
  sleep 1m
  call assert_equal(1, &readonly)
  let g:last += 3

  " clear setReadonly test
  call appendbufline(cmdbufnr, '$', 'setReadOnlyClear_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:setReadOnly!88 F', l[-1])
  sleep 1m
  call assert_equal(0, &readonly)
  let g:last += 3

  " save test
  call setbufvar(bufnr('Xfile4'), '&modified', 1)
  call appendbufline(cmdbufnr, '$', 'save_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:save!89', l[-1])
  sleep 1m
  call assert_true(filereadable('Xfile4'))
  let g:last += 3

  " close test. Don't use buffer 10 after this
  call appendbufline(cmdbufnr, '$', 'close_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 4)')
  let l = ReadXnetbeans()
  call assert_equal('send: 3:close!90', l[-2])
  call assert_equal('3:killed=90', l[-1])
  call assert_equal(1, winnr('$'))
  let g:last += 4

  " specialKeys test
  call appendbufline(cmdbufnr, '$', 'specialKeys_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:specialKeys!91 "F12 F13 C-F13"', l[-1])
  sleep 1m
  call assert_equal(':nbkey F12<CR>', maparg('<F12>', 'n'))
  call assert_equal(':nbkey F13<CR>', maparg('<F13>', 'n'))
  call assert_equal(':nbkey C-F13<CR>', maparg('<C-F13>', 'n'))
  let g:last += 3

  " Open a buffer not monitored by netbeans
  enew | only!
  nbkey "\<C-F3>"
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 1)')
  let l = ReadXnetbeans()
  call assert_equal('0:fileOpened=0 "" T F', l[-1])
  let g:last += 1

  " Test for writing a netbeans buffer
  call appendbufline(cmdbufnr, '$', 'nbbufwrite_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 5)')
  call assert_fails('write', 'E656:')
  call setline(1, ['one', 'two'])
  call assert_fails('1write!', 'E657:')
  write
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 10)')
  let g:last += 10

  if has('mouse')
    " Test for mouse button release
    let save_mouse = &mouse
    set mouse=a
    call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
    let &mouse = save_mouse
    call WaitFor('len(ReadXnetbeans()) >= (g:last + 2)')
    let l = ReadXnetbeans()
    call assert_equal('4:newDotAndMark=93 0 0', l[-2])
    call assert_equal('4:buttonRelease=93 0 1 -1', l[-1])
    let g:last += 2
  endif

  " Test for startAtomic
  call appendbufline(cmdbufnr, '$', 'startAtomic_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:startAtomic!94', l[-1])
  let g:last += 3

  " Test for endAtomic
  call appendbufline(cmdbufnr, '$', 'endAtomic_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_equal('send: 0:endAtomic!95', l[-1])
  let g:last += 3

  " Test for invoking a netbeans key binding
  let special_keys = [
        \ ["\<F1>", 'F1'], ["\<S-F1>", 'S-F1'],
        \ ["\<F2>", 'F2'], ["\<S-F2>", 'S-F2'],
        \ ["\<F3>", 'F3'], ["\<S-F3>", 'S-F3'],
        \ ["\<F4>", 'F4'], ["\<S-F4>", 'S-F4'],
        \ ["\<F5>", 'F5'], ["\<S-F5>", 'S-F5'],
        \ ["\<F6>", 'F6'], ["\<S-F6>", 'S-F6'],
        \ ["\<F7>", 'F7'], ["\<S-F7>", 'S-F7'],
        \ ["\<F8>", 'F8'], ["\<S-F8>", 'S-F8'],
        \ ["\<F9>", 'F9'], ["\<S-F9>", 'S-F9'],
        \ ["\<F11>", 'F11'], ["\<S-F11>", 'S-F11'],
        \ ["\<F12>", 'F12'], ["\<S-F12>", 'S-F12'], ['!', '!']
        \ ]
  for [key, name] in special_keys
    call feedkeys("\<F21>" .. key, 'xt')
    call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
    let l = ReadXnetbeans()
    call assert_match('4:keyCommand=\d\+ "' .. name .. '"', l[-2])
    call assert_match('4:keyAtPos=\d\+ "' .. name .. '" 0 1/0', l[-1])
    let g:last += 3
  endfor
  call feedkeys("\<F21>\<C-S-M-F9>", 'xt')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 3)')
  let l = ReadXnetbeans()
  call assert_match('4:keyCommand=\d\+ "CSM-F9"', l[-2])
  call assert_match('4:keyAtPos=\d\+ "CSM-F9" 0 1/0', l[-1])
  let g:last += 3

  if has('signs') && has('mouse')
    sign define S1 linehl=Search text==>
    sign define S2 linehl=ErrorMsg text=!!
    sign place 10 line=1 name=S1
    sign place 20 line=1 name=S2

    let save_mouse = &mouse
    set mouse=a
    call assert_equal('S2', sign_getplaced()[0].signs[0].name)
    call test_setmouse(1, 1)
    call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
    call assert_equal('S1', sign_getplaced()[0].signs[0].name)
    call test_setmouse(1, 1)
    call feedkeys("\<LeftMouse>\<LeftRelease>", 'xt')
    call assert_equal('S2', sign_getplaced()[0].signs[0].name)
    let &mouse = save_mouse

    sign unplace 10
    sign unplace 20
    sign undefine S1
    sign undefine S2
  endif

  " define a large number of annotations
  call appendbufline(cmdbufnr, '$', 'AnnoScale_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 26)')
  let l = ReadXnetbeans()
  call assert_equal('2:defineAnnoType!60 25 "s25" "x" "=>" blue none', l[-1])
  sleep 1m
  call assert_true(len(sign_getdefined()) >= 25)
  let g:last += 26

  " detach
  call appendbufline(cmdbufnr, '$', 'detach_Test')
  call WaitFor('len(ReadXnetbeans()) >= (g:last + 8)')
  call WaitForAssert({-> assert_equal('0:disconnect=97', ReadXnetbeans()[-1])})

  " the connection was closed
  call assert_false(has("netbeans_enabled"))

  " Remove all the signs
  call sign_unplace('*')
  call sign_undefine()

  call delete("Xnetbeans")
  call delete('Xfile1')
  call delete('Xfile3')
  call delete('Xfile4')
endfunc

func Test_nb_basic()
  call ch_log('Test_nb_basic')
  call s:run_server('Nb_basic')
endfunc

func Nb_file_auth(port)
  call delete("Xnetbeans")
  call writefile([], "Xnetbeans")

  call assert_fails('nbstart =notexist', 'E660:')
  call writefile(['host=localhost', 'port=' . a:port, 'auth=bunny'], 'Xnbauth')
  if has('unix')
    call setfperm('Xnbauth', "rw-r--r--")
    call assert_fails('nbstart =Xnbauth', 'E668:')
  endif
  call setfperm('Xnbauth', "rw-------")
  exe 'nbstart =Xnbauth'
  call assert_true(has("netbeans_enabled"))

  call WaitFor('len(ReadXnetbeans()) > 2')
  nbclose
  let lines = ReadXnetbeans()
  call assert_equal('AUTH bunny', lines[0])
  call assert_equal('0:version=0 "2.5"', lines[1])
  call assert_equal('0:startupDone=0', lines[2])

  call delete("Xnbauth")
  call delete("Xnetbeans")
endfunc

func Test_nb_file_auth()
  call ch_log('Test_nb_file_auth')
  call s:run_server('Nb_file_auth')
endfunc

" Test for quiting Vim with an open netbeans connection
func Nb_quit_with_conn(port)
  call delete("Xnetbeans")
  call writefile([], "Xnetbeans")
  let after =<< trim END
    source shared.vim

    func ReadXnetbeans()
      let l = readfile("Xnetbeans")
      return filter(l, 'v:val !~ "^0:geometry="')
    endfunc

    " Establish the connection with the netbeans server
    exe 'nbstart :localhost:' .. g:port .. ':star'
    call assert_true(has("netbeans_enabled"))
    call WaitFor('len(ReadXnetbeans()) >= 3')
    let l = ReadXnetbeans()
    call assert_equal(['AUTH star',
      \ '0:version=0 "2.5"',
      \ '0:startupDone=0'], l[-3:])

    " Open the command buffer to communicate with the server
    split Xcmdbuf
    call WaitFor('len(ReadXnetbeans()) >= 6')
    let l = ReadXnetbeans()
    call assert_equal('0:fileOpened=0 "Xcmdbuf" T F',
          \ substitute(l[-3], '".*/', '"', ''))
    call assert_equal('send: 1:putBufferNumber!15 "Xcmdbuf"',
          \ substitute(l[-2], '".*/', '"', ''))
    call assert_equal('1:startDocumentListen!16', l[-1])
    sleep 1m

    quit!
    quit!
  END
  if RunVim(['let g:port = ' .. a:port], after, '')
    call WaitFor('len(ReadXnetbeans()) >= 9')
    let l = ReadXnetbeans()
    call assert_equal('1:unmodified=16', l[-3])
    call assert_equal('1:killed=16', l[-2])
    call assert_equal('0:disconnect=16', l[-1])
  endif
  call delete('Xnetbeans')
endfunc

func Test_nb_quit_with_conn()
  " Exiting Vim with a netbeans connection doesn't work properly on
  " MS-Windows.
  CheckUnix
  call s:run_server('Nb_quit_with_conn')
endfunc

" vim: shiftwidth=2 sts=2 expandtab