@@ -85,6 +85,7 @@ func TestAPI(t *testing.T) {
8585 v , cleanup := newChildProcess (t )
8686 defer cleanup ()
8787
88+ t .Run ("BufAttach" , testBufAttach (v ))
8889 t .Run ("SimpleHandler" , testSimpleHandler (v ))
8990 t .Run ("Buffer" , testBuffer (v ))
9091 t .Run ("Window" , testWindow (v ))
@@ -98,7 +99,6 @@ func TestAPI(t *testing.T) {
9899 t .Run ("Mode" , testMode (v ))
99100 t .Run ("ExecLua" , testExecLua (v ))
100101 t .Run ("Highlight" , testHighlight (v ))
101- t .Run ("BufAttach" , testBufAttach (v ))
102102 t .Run ("VirtualText" , testVirtualText (v ))
103103 t .Run ("FloatingWindow" , testFloatingWindow (v ))
104104 t .Run ("Context" , testContext (v ))
@@ -108,6 +108,109 @@ func TestAPI(t *testing.T) {
108108 t .Run ("OptionsInfo" , testOptionsInfo (v ))
109109}
110110
111+ func testBufAttach (v * Nvim ) func (* testing.T ) {
112+ return func (t * testing.T ) {
113+ clearBuffer (t , v , 0 ) // clear curret buffer text
114+
115+ type ChangedtickEvent struct {
116+ Buffer Buffer
117+ Changetick int64
118+ }
119+ type BufLinesEvent struct {
120+ Buffer Buffer
121+ Changetick int64
122+ FirstLine int64
123+ LastLine int64
124+ LineData string
125+ IsMultipart bool
126+ }
127+
128+ changedtickChan := make (chan * ChangedtickEvent )
129+ v .RegisterHandler ("nvim_buf_changedtick_event" , func (changedtickEvent ... interface {}) {
130+ ev := & ChangedtickEvent {
131+ Buffer : changedtickEvent [0 ].(Buffer ),
132+ Changetick : changedtickEvent [1 ].(int64 ),
133+ }
134+ changedtickChan <- ev
135+ })
136+
137+ bufLinesChan := make (chan * BufLinesEvent )
138+ v .RegisterHandler ("nvim_buf_lines_event" , func (bufLinesEvent ... interface {}) {
139+ ev := & BufLinesEvent {
140+ Buffer : bufLinesEvent [0 ].(Buffer ),
141+ Changetick : bufLinesEvent [1 ].(int64 ),
142+ FirstLine : bufLinesEvent [2 ].(int64 ),
143+ LastLine : bufLinesEvent [3 ].(int64 ),
144+ LineData : fmt .Sprint (bufLinesEvent [4 ]),
145+ IsMultipart : bufLinesEvent [5 ].(bool ),
146+ }
147+ bufLinesChan <- ev
148+ })
149+
150+ ok , err := v .AttachBuffer (0 , false , make (map [string ]interface {})) // first 0 arg refers to the current buffer
151+ if err != nil {
152+ t .Fatal (err )
153+ }
154+ if ! ok {
155+ t .Fatal (errors .New ("could not attach buffer" ))
156+ }
157+
158+ changedtickExpected := & ChangedtickEvent {
159+ Buffer : 1 ,
160+ Changetick : 3 ,
161+ }
162+ bufLinesEventExpected := & BufLinesEvent {
163+ Buffer : 1 ,
164+ Changetick : 4 ,
165+ FirstLine : 0 ,
166+ LastLine : 1 ,
167+ LineData : "[test]" ,
168+ IsMultipart : false ,
169+ }
170+
171+ var numEvent int64 // add and load should be atomically
172+ errc := make (chan error )
173+ done := make (chan struct {})
174+ go func () {
175+ for {
176+ select {
177+ default :
178+ if atomic .LoadInt64 (& numEvent ) == 2 { // end buf_attach test when handle 2 event
179+ done <- struct {}{}
180+ return
181+ }
182+ case changedtick := <- changedtickChan :
183+ if ! reflect .DeepEqual (changedtick , changedtickExpected ) {
184+ errc <- fmt .Errorf ("changedtick = %+v, want %+v" , changedtick , changedtickExpected )
185+ }
186+ atomic .AddInt64 (& numEvent , 1 )
187+ case bufLines := <- bufLinesChan :
188+ if expected := bufLinesEventExpected ; ! reflect .DeepEqual (bufLines , expected ) {
189+ errc <- fmt .Errorf ("bufLines = %+v, want %+v" , bufLines , expected )
190+ }
191+ atomic .AddInt64 (& numEvent , 1 )
192+ }
193+ }
194+ }()
195+
196+ go func () {
197+ <- done
198+ close (errc )
199+ }()
200+
201+ test := []byte ("test" )
202+ if err := v .SetBufferLines (0 , 0 , - 1 , true , bytes .Fields (test )); err != nil { // first 0 arg refers to the current buffer
203+ t .Fatal (err )
204+ }
205+
206+ for err := range errc {
207+ if err != nil {
208+ t .Fatal (err )
209+ }
210+ }
211+ }
212+ }
213+
111214func testSimpleHandler (v * Nvim ) func (* testing.T ) {
112215 return func (t * testing.T ) {
113216 cid := v .ChannelID ()
@@ -1211,109 +1314,6 @@ func testHighlight(v *Nvim) func(*testing.T) {
12111314 }
12121315}
12131316
1214- func testBufAttach (v * Nvim ) func (* testing.T ) {
1215- return func (t * testing.T ) {
1216- clearBuffer (t , v , 0 ) // clear curret buffer text
1217-
1218- type ChangedtickEvent struct {
1219- Buffer Buffer
1220- Changetick int64
1221- }
1222- type BufLinesEvent struct {
1223- Buffer Buffer
1224- Changetick int64
1225- FirstLine int64
1226- LastLine int64
1227- LineData string
1228- IsMultipart bool
1229- }
1230-
1231- changedtickChan := make (chan * ChangedtickEvent )
1232- v .RegisterHandler ("nvim_buf_changedtick_event" , func (changedtickEvent ... interface {}) {
1233- ev := & ChangedtickEvent {
1234- Buffer : changedtickEvent [0 ].(Buffer ),
1235- Changetick : changedtickEvent [1 ].(int64 ),
1236- }
1237- changedtickChan <- ev
1238- })
1239-
1240- bufLinesChan := make (chan * BufLinesEvent )
1241- v .RegisterHandler ("nvim_buf_lines_event" , func (bufLinesEvent ... interface {}) {
1242- ev := & BufLinesEvent {
1243- Buffer : bufLinesEvent [0 ].(Buffer ),
1244- Changetick : bufLinesEvent [1 ].(int64 ),
1245- FirstLine : bufLinesEvent [2 ].(int64 ),
1246- LastLine : bufLinesEvent [3 ].(int64 ),
1247- LineData : fmt .Sprint (bufLinesEvent [4 ]),
1248- IsMultipart : bufLinesEvent [5 ].(bool ),
1249- }
1250- bufLinesChan <- ev
1251- })
1252-
1253- ok , err := v .AttachBuffer (0 , false , make (map [string ]interface {})) // first 0 arg refers to the current buffer
1254- if err != nil {
1255- t .Fatal (err )
1256- }
1257- if ! ok {
1258- t .Fatal (errors .New ("could not attach buffer" ))
1259- }
1260-
1261- changedtickExpected := & ChangedtickEvent {
1262- Buffer : 1 ,
1263- Changetick : 5 ,
1264- }
1265- bufLinesEventExpected := & BufLinesEvent {
1266- Buffer : 1 ,
1267- Changetick : 6 ,
1268- FirstLine : 0 ,
1269- LastLine : 1 ,
1270- LineData : "[test]" ,
1271- IsMultipart : false ,
1272- }
1273-
1274- var numEvent int64 // add and load should be atomically
1275- errc := make (chan error )
1276- done := make (chan struct {})
1277- go func () {
1278- for {
1279- select {
1280- default :
1281- if atomic .LoadInt64 (& numEvent ) == 2 { // end buf_attach test when handle 2 event
1282- done <- struct {}{}
1283- return
1284- }
1285- case changedtick := <- changedtickChan :
1286- if ! reflect .DeepEqual (changedtick , changedtickExpected ) {
1287- errc <- fmt .Errorf ("changedtick = %+v, want %+v" , changedtick , changedtickExpected )
1288- }
1289- atomic .AddInt64 (& numEvent , 1 )
1290- case bufLines := <- bufLinesChan :
1291- if expected := bufLinesEventExpected ; ! reflect .DeepEqual (bufLines , expected ) {
1292- errc <- fmt .Errorf ("bufLines = %+v, want %+v" , bufLines , expected )
1293- }
1294- atomic .AddInt64 (& numEvent , 1 )
1295- }
1296- }
1297- }()
1298-
1299- go func () {
1300- <- done
1301- close (errc )
1302- }()
1303-
1304- test := []byte ("test" )
1305- if err := v .SetBufferLines (0 , 0 , - 1 , true , bytes .Fields (test )); err != nil { // first 0 arg refers to the current buffer
1306- t .Fatal (err )
1307- }
1308-
1309- for err := range errc {
1310- if err != nil {
1311- t .Fatal (err )
1312- }
1313- }
1314- }
1315- }
1316-
13171317func testVirtualText (v * Nvim ) func (* testing.T ) {
13181318 return func (t * testing.T ) {
13191319 clearBuffer (t , v , Buffer (0 )) // clear curret buffer text
0 commit comments