testing

testing.txt 适用于 Vim 8.2 版本。 最近更新: 2020年2月 VIM 参考手册 by Bram Moolenaar 译者: Willis 测试 Vim 和 Vim 脚本 testing-support eval.txt 说明表达式的计算。此文件描述 Vim 脚本中编写测试的细节。这可用于 Vim 自身和插件的测试。 1. 测试 Vim testing 2. 测试函数 test-functions-details 3. 断言函数 assert-functions-details

1. 测试 Vim testing

Vim 在编译后可以进行测试,通常用 "make test" 进行。 这些测试位于目录 "src/testdir"。 在不同的时间点,加入了几种不同类型的测试: test33.in 最老的,不要再加了 test_something.in 旧风格测试 test_something.vim 新风格测试 new-style-testing 新写的测试需用新风格的测试。使用 assert_equal() 之类的函数,以便在同一处地方 维护测试命令和期待的结果。 old-style-testing 有些场合仍然需要旧风格的测试。例如在没有 +eval 特性时测试 Vim。 更多信息可见文件 src/testdir/README.txt。

2. 测试函数 test-functions-details

test_alloc_fail({id}, {countdown}, {repeat}) test_alloc_fail() 用于测试: 如果调用了 {id} 指定的内存分配,{countdown} 减一,到 零时,让内存分配失败 {repeat} 次。如果 {repeat} 小于一,失败一 次。 也可用作 method : GetAllocId()->test_alloc_fail() test_autochdir() test_autochdir() 设置标志位,在 Vim 启动过程结束之前,打开 'autochdir' 的效果。 test_feedinput({string}) test_feedinput() {string} 中的字符会如果是用户键入的那样,排队依次等待处理。使 用一个低层输入缓冲区。此函数只适用于 +unix 或 GUI 运行时。 也可用作 method : GetText()->test_feedinput() test_garbagecollect_now() test_garbagecollect_now() 类似于 garbagecollect(),但立即执行。只能通过直接调用,以避免 内部保存任何结构,而调用任何函数前, v:testing 必须置位。 test_garbagecollect_soon() test_garbagecollect_soon() 就像在 main 循环中那样,设置标志位来调用垃圾清理器。仅用于测 试。 test_getvalue({name}) test_getvalue() 取得内部变量的值。支持以下的 {name} 值: need_fileinfo 也可用作 method : GetName()->test_getvalue() test_ignore_error({expr}) test_ignore_error() 忽略包含 {expr} 的任何错误。显示正常的消息来替代。 只用于测试,因为那里 try/catch 不能用于捕捉错误 (因为跳过了下 面的代码)。 {expr} 按本义使用,不用作模式。 如果 {expr} 为字符串 "RESET",清空忽略错误的列表。 也可用作 method : GetErrorText()->test_ignore_error() test_null_blob() test_null_blob() 返回 null Blob 。仅用于测试。 test_null_channel() test_null_channel() 返回 null Channel 。仅用于测试。 {仅当编译时加入 +channel 特性才有效} test_null_dict() test_null_dict() 返回 null Dict 。仅用于测试。 test_null_job() test_null_job() 返回 null Job 。仅用于测试。 {仅当编译时加入 +job 特性才有效} test_null_list() test_null_list() 返回 null List 。仅用于测试。 test_null_partial() test_null_partial() 返回 null Partial 。仅用于测试。 test_null_string() test_null_string() 返回 null String 。仅用于测试。 test_unknown() test_unknown() 返回未知类型的值。仅用于测试。 test_void() test_void() 返回 void 类型的值。仅用于测试。 test_option_not_set({name}) test_option_not_set() 复位指示选项 {name} 已设置过的标志位。就像看起来此选项还使用缺 省值那样。这样来用: set ambiwidth=double call test_option_not_set('ambiwidth') 现在 'ambiwidth' 选项的行为就像它从未被改变过一样,即使它的值 是 "double"。 仅用于测试! 也可用作 method : GetOptionName()->test_option_not_set() test_override({name}, {val}) test_override() 覆盖 Vim 内部处理的部分行为,用于运行测试。只能用于 Vim 测试! 如果 {val} 非零,打开覆盖行为,{val} 为零时,关闭覆盖行为。 目前支持的名字是: 名字 {val} 非零的效果 redraw 屏蔽 redrawing() 函数 redraw_flag 忽略 RedrawingDisabled 标志位 char_avail 屏蔽 char_avail() 函数 starting 复位 "starting" 变量,见下 nfa_fail 使 NFA 正规表达式引擎失败以强制回归到旧引擎 no_query_mouse "dec" 终端不查询鼠标位置 no_wait_return 置位 "no_wait_return" 标志位。不被 "ALL" 复 位。 ALL 复位所有覆盖 (不使用 {val}) "starting" 用于假定初始化已经完成的测试。因为测试的运行是通过 运行一个 "starting" 变量为非零的脚本进行的。这通常有好处 (测试 更快),但有时会改变行为,使测试不能正常工作。 以下操作: call test_override('starting', 1) 保存 "starting" 的值。以下操作恢复之: call test_override('starting', 0) 也可用作 method : GetOverrideVal()-> test_override('starting') test_refcount({expr}) test_refcount() 返回 {expr} 的引用计数。如果 {expr} 是没有引用计数的类型,返回 -1。仅用于测试。 也可用作 method : GetVarname()->test_refcount() test_scrollbar({which}, {value}, {dragging}) test_scrollbar() 模拟使用滚动条 {which} 移动到位置 {value}{which} 可以是: left 当前窗口的左侧滚动条 right 当前窗口的右侧滚动条 hor 水平滚动条 对垂直滚动条而言,{value} 可取值 1 到缓冲区行数。对水平滚动条 而言,{value} 可取值 1 到最大的行长,假定 'wrap' 未置位。 {dragging} 如果非零,行为就像拖动滚动条,否则像点击滚动条。 只能用于确实存在的 {which} 滚动条,显然只能用于 GUI。 也可用作 method : GetValue()->test_scrollbar('right', 0) test_setmouse({row}, {col}) test_setmouse() 设置下个鼠标活动所用的鼠标位置。 {row}{col} 从 1 开始。 示例: call test_setmouse(4, 20) call feedkeys("\<LeftMouse>", "xt") test_settime({expr}) test_settime() 设置 Vim 内部使用的时间。目前只用于历史中的时间戳,用于 viminfo 和撤销。 值为 1 使 Vim 在警告或信息后不睡眠。 {expr} 计算结果必须为数值。值为零时恢复正常行为。 也可用作 method : GetTime()->test_settime() test_srand_seed([seed]) test_srand_seed() [seed] 给出时用来设置 srand() 使用的种子值。不给出时,删除此 测试用种子。

3. 断言函数 assert-functions-details

assert_beeps({cmd}) assert_beeps() 执行 {cmd},如果 响铃或可视响铃,加入错误信息到 v:errors 。 另见 assert_fails()assert-return 。 也可用作 method : GetCmd()->assert_beeps() assert_equal() assert_equal({expected}, {actual} [, {msg}]) {expected} 不等于 {actual} 时,加入错误信息到 v:errors ,并反 回 1。否则返回零 assert-return 。 没有自动转换,字符串 "4" 不同于数值 4。数值 4 又不同于浮点数 4.0。不适用 'ignorecase',大小写永远敏感。 省略 {msg} 则生成形如 "Expected {expected} but got {actual}" 的错误。 示例: assert_equal('foo', 'bar') 会在 v:errors 加入字符串: test.vim line 12: Expected 'foo' but got 'bar' 也可用作 method : mylist->assert_equal([1, 2, 3]) assert_equalfile() assert_equalfile({fname-one}, {fname-two}) 文件 {fname-one}{fname-two} 不包含相同文本时,加入错误信息 到 v:errors 。 另见 assert-return 。 文件 {fname-one}{fname-two} 不存在时,错误信息说明这一点。 最常用于 terminal-diff 。 也可用作 method : GetLog()->assert_equalfile('expected.log') assert_exception({error} [, {msg}]) assert_exception() v:exception 不包含 {error} 时,加入错误信息到 v:errors 。另见 assert-return 。 可用于断言命令抛出例外。使用错误号后加冒号,就不用担心翻译的问 题: try commandthatfails call assert_false(1, 'command should have failed') catch call assert_exception('E492:') endtry assert_fails({cmd} [, {error} [, {msg}]]) assert_fails() 执行 {cmd},如果 出错,加入错误信息到 v:errors 。另见 assert-return 。 给出 {error} 时,必须匹配 v:errmsg注意 响铃不作为错误处理,而有些出错的命令只会响铃。为此,可用 assert_beeps() 断言。 也可用作 method : GetCmd()->assert_fails('E99:') assert_false({actual} [, {msg}]) assert_false() {actual} 不为假时,加入错误信息到 v:errors ,其余类同 assert_equal() 。 另见 assert-return 。 零值为假值。{actual} 不是数值时,断言失败。 省略 {msg} 则生成形如 "Expected False but got {actual}" 的错 误。 也可用作 method : GetResult()->assert_false() assert_inrange({lower}, {upper}, {actual} [, {msg}]) assert_inrange() 对数值和 Float 值进行断言。{actual} 小于 {lower} 会大于 {upper} 时,加入错误信息到 v:errors 。另见 assert-return 。 省略 {msg} 则生成形如 "Expected range {lower} - {upper}, but got {actual}" 的错误。 assert_match() assert_match({pattern}, {actual} [, {msg}]) {pattern} 不匹配 {actual} 时,加入错误信息到 v:errors 。另见 assert-return{pattern} 的用法类同于 =~ : 匹配总是假定 'magic' 置位而 'cpoptions' 为空,而忽略 'magic''cpoptions' 的实际值。 {actual} 作字符串使用,适用自动转换。 "^" 和 "$" 可用来匹配文本的开始和结尾。两者皆用则可匹配整个文 本。 省略 {msg} 则生成形如 "Pattern {pattern} does not match {actual}" 的错误。 示例: assert_match('^f.*o$', 'foobar') 会在 v:errors 加入字符串: test.vim line 12: Pattern '^f.*o$' does not match 'foobar' 也可用作 method : getFile()->assert_match('foo.*') assert_notequal() assert_notequal({expected}, {actual} [, {msg}]) 和 assert_equal() 相反: {expected} 等于 {actual} 时,加入错 误信息到 v:errors 。 另见 assert-return 。 也可用作 method : mylist->assert_notequal([1, 2, 3]) assert_notmatch() assert_notmatch({pattern}, {actual} [, {msg}]) 和 assert_match() 相反: {pattern} 匹配 {actual} 时,加入错 误信息到 v:errors 。 另见 assert-return 。 也可用作 method : getFile()->assert_notmatch('bar.*') assert_report({msg}) assert_report() 直接报告测试失败,用 {msg}。总返回一。 也可用作 method : GetMessage()->assert_report() assert_true({actual} [, {msg}]) assert_true() {actual} 不为真时,加入错误信息到 v:errors ,余类同 assert_equal() 。 另见 assert-return 。 非零值为真值。{actual} 不是数值时,断言失败。 省略 {msg} 则生成形如 "Expected True but got {actual}" 的错 误。 也可用作 method : GetResult()->assert_true() vim:tw=78:ts=8:noet:ft=help:norl: