@@ -1153,3 +1153,58 @@ func Test_func_exists_on_reload()
11531153 call delete (' Xfuncexists' )
11541154 delfunc ExistingFunction
11551155endfunc
1156+
1157+ " Test confirm({msg} [, {choices} [, {default} [, {type}]]])
1158+ func Test_confirm ()
1159+ if ! has (' unix' ) || has (' gui_running' )
1160+ return
1161+ endif
1162+
1163+ call feedkeys (' o' , ' L' )
1164+ let a = confirm (' Press O to proceed' )
1165+ call assert_equal (1 , a )
1166+
1167+ call feedkeys (' y' , ' L' )
1168+ let a = confirm (' Are you sure?' , " &Yes\n &No" )
1169+ call assert_equal (1 , a )
1170+
1171+ call feedkeys (' n' , ' L' )
1172+ let a = confirm (' Are you sure?' , " &Yes\n &No" )
1173+ call assert_equal (2 , a )
1174+
1175+ " confirm() should return 0 when pressing CTRL-C.
1176+ call feedkeys (" \<C-c> " , ' L' )
1177+ let a = confirm (' Are you sure?' , " &Yes\n &No" )
1178+ call assert_equal (0 , a )
1179+
1180+ " <Esc> requires another character to avoid it being seen as the start of an
1181+ " escape sequence. Zero should be harmless.
1182+ call feedkeys (" \<Esc> 0" , ' L' )
1183+ let a = confirm (' Are you sure?' , " &Yes\n &No" )
1184+ call assert_equal (0 , a )
1185+
1186+ " Default choice is returned when pressing <CR>.
1187+ call feedkeys (" \<CR> " , ' L' )
1188+ let a = confirm (' Are you sure?' , " &Yes\n &No" )
1189+ call assert_equal (1 , a )
1190+
1191+ call feedkeys (" \<CR> " , ' L' )
1192+ let a = confirm (' Are you sure?' , " &Yes\n &No" , 2 )
1193+ call assert_equal (2 , a )
1194+
1195+ call feedkeys (" \<CR> " , ' L' )
1196+ let a = confirm (' Are you sure?' , " &Yes\n &No" , 0 )
1197+ call assert_equal (0 , a )
1198+
1199+ " Test with the {type} 4th argument
1200+ for type in [' Error' , ' Question' , ' Info' , ' Warning' , ' Generic' ]
1201+ call feedkeys (' y' , ' L' )
1202+ let a = confirm (' Are you sure?' , " &Yes\n &No\n " , 1 , type )
1203+ call assert_equal (1 , a )
1204+ endfor
1205+
1206+ call assert_fails (' call confirm([])' , ' E730:' )
1207+ call assert_fails (' call confirm("Are you sure?", [])' , ' E730:' )
1208+ call assert_fails (' call confirm("Are you sure?", "&Yes\n&No\n", [])' , ' E745:' )
1209+ call assert_fails (' call confirm("Are you sure?", "&Yes\n&No\n", 0, [])' , ' E730:' )
1210+ endfunc
0 commit comments