@@ -8022,4 +8022,256 @@ def Test_class_member_funcref()
80228022 v9.CheckSourceSuccess (lines )
80238023enddef
80248024
8025+ " Test for using object methods as popup callback functions
8026+ def Test_objmethod_popup_callback ()
8027+ # Use the popup from the script level
8028+ var lines = << trim END
8029+ vim9script
8030+
8031+ class A
8032+ this.selection : number = -1
8033+ this.filterkeys: list <string> = []
8034+
8035+ def PopupFilter (id: number , key : string ): bool
8036+ add (this.filterkeys, key )
8037+ return popup_filter_yesno (id, key )
8038+ enddef
8039+
8040+ def PopupCb (id: number , result: number )
8041+ this.selection = result ? 100 : 200
8042+ enddef
8043+ endclass
8044+
8045+ var a = A.new ()
8046+ feedkeys (' ' , ' xt' )
8047+ var winid = popup_create (' Y/N?' ,
8048+ {filter : a .PopupFilter, callback: a .PopupCb})
8049+ feedkeys (' y' , ' xt' )
8050+ popup_close (winid)
8051+ assert_equal (100 , a .selection )
8052+ assert_equal ([' y' ], a .filterkeys)
8053+ feedkeys (' ' , ' xt' )
8054+ winid = popup_create (' Y/N?' ,
8055+ {filter : a .PopupFilter, callback: a .PopupCb})
8056+ feedkeys (' n' , ' xt' )
8057+ popup_close (winid)
8058+ assert_equal (200 , a .selection )
8059+ assert_equal ([' y' , ' n' ], a .filterkeys)
8060+ END
8061+ v9.CheckSourceSuccess (lines )
8062+
8063+ # Use the popup from a def function
8064+ lines = << trim END
8065+ vim9script
8066+
8067+ class A
8068+ this.selection : number = -1
8069+ this.filterkeys: list <string> = []
8070+
8071+ def PopupFilter (id: number , key : string ): bool
8072+ add (this.filterkeys, key )
8073+ return popup_filter_yesno (id, key )
8074+ enddef
8075+
8076+ def PopupCb (id: number , result: number )
8077+ this.selection = result ? 100 : 200
8078+ enddef
8079+ endclass
8080+
8081+ def Foo ()
8082+ var a = A.new ()
8083+ feedkeys (' ' , ' xt' )
8084+ var winid = popup_create (' Y/N?' ,
8085+ {filter : a .PopupFilter, callback: a .PopupCb})
8086+ feedkeys (' y' , ' xt' )
8087+ popup_close (winid)
8088+ assert_equal (100 , a .selection )
8089+ assert_equal ([' y' ], a .filterkeys)
8090+ feedkeys (' ' , ' xt' )
8091+ winid = popup_create (' Y/N?' ,
8092+ {filter : a .PopupFilter, callback: a .PopupCb})
8093+ feedkeys (' n' , ' xt' )
8094+ popup_close (winid)
8095+ assert_equal (200 , a .selection )
8096+ assert_equal ([' y' , ' n' ], a .filterkeys)
8097+ enddef
8098+ Foo ()
8099+ END
8100+ v9.CheckSourceSuccess (lines )
8101+ enddef
8102+
8103+ " Test for using class methods as popup callback functions
8104+ def Test_classmethod_popup_callback ()
8105+ # Use the popup from the script level
8106+ var lines = << trim END
8107+ vim9script
8108+
8109+ class A
8110+ static selection : number = -1
8111+ static filterkeys: list <string> = []
8112+
8113+ static def PopupFilter (id: number , key : string ): bool
8114+ add (filterkeys, key )
8115+ return popup_filter_yesno (id, key )
8116+ enddef
8117+
8118+ static def PopupCb (id: number , result: number )
8119+ selection = result ? 100 : 200
8120+ enddef
8121+ endclass
8122+
8123+ feedkeys (' ' , ' xt' )
8124+ var winid = popup_create (' Y/N?' ,
8125+ {filter : A.PopupFilter, callback: A.PopupCb})
8126+ feedkeys (' y' , ' xt' )
8127+ popup_close (winid)
8128+ assert_equal (100 , A.selection )
8129+ assert_equal ([' y' ], A.filterkeys)
8130+ feedkeys (' ' , ' xt' )
8131+ winid = popup_create (' Y/N?' ,
8132+ {filter : A.PopupFilter, callback: A.PopupCb})
8133+ feedkeys (' n' , ' xt' )
8134+ popup_close (winid)
8135+ assert_equal (200 , A.selection )
8136+ assert_equal ([' y' , ' n' ], A.filterkeys)
8137+ END
8138+ v9.CheckSourceSuccess (lines )
8139+
8140+ # Use the popup from a def function
8141+ lines = << trim END
8142+ vim9script
8143+
8144+ class A
8145+ static selection : number = -1
8146+ static filterkeys: list <string> = []
8147+
8148+ static def PopupFilter (id: number , key : string ): bool
8149+ add (filterkeys, key )
8150+ return popup_filter_yesno (id, key )
8151+ enddef
8152+
8153+ static def PopupCb (id: number , result: number )
8154+ selection = result ? 100 : 200
8155+ enddef
8156+ endclass
8157+
8158+ def Foo ()
8159+ feedkeys (' ' , ' xt' )
8160+ var winid = popup_create (' Y/N?' ,
8161+ {filter : A.PopupFilter, callback: A.PopupCb})
8162+ feedkeys (' y' , ' xt' )
8163+ popup_close (winid)
8164+ assert_equal (100 , A.selection )
8165+ assert_equal ([' y' ], A.filterkeys)
8166+ feedkeys (' ' , ' xt' )
8167+ winid = popup_create (' Y/N?' ,
8168+ {filter : A.PopupFilter, callback: A.PopupCb})
8169+ feedkeys (' n' , ' xt' )
8170+ popup_close (winid)
8171+ assert_equal (200 , A.selection )
8172+ assert_equal ([' y' , ' n' ], A.filterkeys)
8173+ enddef
8174+ Foo ()
8175+ END
8176+ v9.CheckSourceSuccess (lines )
8177+ enddef
8178+
8179+ " Test for using an object method as a timer callback function
8180+ def Test_objmethod_timer_callback ()
8181+ # Use the timer callback from script level
8182+ var lines = << trim END
8183+ vim9script
8184+
8185+ class A
8186+ this.timerTick: number = -1
8187+ def TimerCb (timerID: number )
8188+ this.timerTick = 6
8189+ enddef
8190+ endclass
8191+
8192+ var a = A.new ()
8193+ timer_start (0 , a .TimerCb)
8194+ var maxWait = 5
8195+ while maxWait > 0 && a .timerTick == -1
8196+ :sleep 10 m
8197+ maxWait -= 1
8198+ endwhile
8199+ assert_equal (6 , a .timerTick)
8200+ END
8201+ v9.CheckSourceSuccess (lines )
8202+
8203+ # Use the timer callback from a def function
8204+ lines = << trim END
8205+ vim9script
8206+
8207+ class A
8208+ this.timerTick: number = -1
8209+ def TimerCb (timerID: number )
8210+ this.timerTick = 6
8211+ enddef
8212+ endclass
8213+
8214+ def Foo ()
8215+ var a = A.new ()
8216+ timer_start (0 , a .TimerCb)
8217+ var maxWait = 5
8218+ while maxWait > 0 && a .timerTick == -1
8219+ :sleep 10 m
8220+ maxWait -= 1
8221+ endwhile
8222+ assert_equal (6 , a .timerTick)
8223+ enddef
8224+ Foo ()
8225+ END
8226+ v9.CheckSourceSuccess (lines )
8227+ enddef
8228+
8229+ " Test for using a class method as a timer callback function
8230+ def Test_classmethod_timer_callback ()
8231+ # Use the timer callback from script level
8232+ var lines = << trim END
8233+ vim9script
8234+
8235+ class A
8236+ static timerTick: number = -1
8237+ static def TimerCb (timerID: number )
8238+ timerTick = 6
8239+ enddef
8240+ endclass
8241+
8242+ timer_start (0 , A.TimerCb)
8243+ var maxWait = 5
8244+ while maxWait > 0 && A.timerTick == -1
8245+ :sleep 10 m
8246+ maxWait -= 1
8247+ endwhile
8248+ assert_equal (6 , A.timerTick)
8249+ END
8250+ v9.CheckSourceSuccess (lines )
8251+
8252+ # Use the timer callback from a def function
8253+ lines = << trim END
8254+ vim9script
8255+
8256+ class A
8257+ static timerTick: number = -1
8258+ static def TimerCb (timerID: number )
8259+ timerTick = 6
8260+ enddef
8261+ endclass
8262+
8263+ def Foo ()
8264+ timer_start (0 , A.TimerCb)
8265+ var maxWait = 5
8266+ while maxWait > 0 && A.timerTick == -1
8267+ :sleep 10 m
8268+ maxWait -= 1
8269+ endwhile
8270+ assert_equal (6 , A.timerTick)
8271+ enddef
8272+ Foo ()
8273+ END
8274+ v9.CheckSourceSuccess (lines )
8275+ enddef
8276+
80258277" vim: ts = 8 sw = 2 sts = 2 expandtab tw = 80 fdm = marker
0 commit comments