diff options
-rw-r--r-- | runtime/doc/eval.txt | 18 | ||||
-rw-r--r-- | src/nvim/eval.c | 30 | ||||
-rw-r--r-- | src/nvim/eval.lua | 1 | ||||
-rw-r--r-- | src/nvim/eval_defs.h | 1 | ||||
-rw-r--r-- | src/nvim/version.c | 2 | ||||
-rw-r--r-- | test/functional/legacy/assert_spec.lua | 20 |
6 files changed, 67 insertions, 5 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index ce23bab3d4..7ce47179b8 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -1946,6 +1946,8 @@ assert_equal({exp}, {act} [, {msg}]) none assert {exp} is equal to {act} assert_exception( {error} [, {msg}]) none assert {error} is in v:exception assert_fails( {cmd} [, {error}]) none assert {cmd} fails assert_false({actual} [, {msg}]) none assert {actual} is false +assert_inrange({lower}, {upper}, {actual} [, {msg}]) + none assert {actual} is inside the range assert_match( {pat}, {text} [, {msg}]) none assert {pat} matches {text} assert_notequal( {exp}, {act} [, {msg}]) none assert {exp} is not equal {act} assert_notmatch( {pat}, {text} [, {msg}]) none assert {pat} not matches {text} @@ -2460,8 +2462,16 @@ assert_false({actual} [, {msg}]) *assert_false()* |v:errors|, like with |assert_equal()|. A value is false when it is zero or |v:false|. When "{actual}" is not a number or |v:false| the assert fails. - When {msg} is omitted an error in the form "Expected False but - got {actual}" is produced. + When {msg} is omitted an error in the form + "Expected False but got {actual}" is produced. + +assert_inrange({lower}, {upper}, {actual} [, {msg}]) *assert_inrange()* + This asserts number values. When {actual} is lower than + {lower} or higher than {upper} an error message is added to + |v:errors|. + When {msg} is omitted an error in the form + "Expected range {lower} - {upper}, but got {actual}" is + produced. *assert_match()* assert_match({pattern}, {actual} [, {msg}]) @@ -2476,8 +2486,8 @@ assert_match({pattern}, {actual} [, {msg}]) Use "^" and "$" to match with the start and end of the text. Use both to match the whole text. - When {msg} is omitted an error in the form "Pattern {pattern} - does not match {actual}" is produced. + When {msg} is omitted an error in the form + "Pattern {pattern} does not match {actual}" is produced. Example: > assert_match('^f.*o$', 'foobar') < Will result in a string to be added to |v:errors|: diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 3b8b38588b..6dc7e5606e 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -8111,6 +8111,30 @@ static void f_assert_fails(typval_T *argvars, typval_T *rettv, FunPtr fptr) set_vim_var_string(VV_ERRMSG, NULL, 0); } +void assert_inrange(typval_T *argvars) +{ + int error = (int)false; + varnumber_T lower = get_tv_number_chk(&argvars[0], &error); + varnumber_T upper = get_tv_number_chk(&argvars[1], &error); + varnumber_T actual = get_tv_number_chk(&argvars[2], &error); + + if (error) { + return; + } + if (actual < lower || actual > upper) { + garray_T ga; + prepare_assert_error(&ga); + + char msg[55]; + vim_snprintf(msg, sizeof(msg), "range %" PRId64 " - %" PRId64 ",", + (int64_t)lower, (int64_t)upper); + fill_assert_error(&ga, &argvars[3], (char_u *)msg, NULL, &argvars[2], + ASSERT_INRANGE); + assert_error(&ga); + ga_clear(&ga); + } +} + // Common for assert_true() and assert_false(). static void assert_bool(typval_T *argvars, bool is_true) { @@ -8158,6 +8182,12 @@ static void assert_match_common(typval_T *argvars, assert_type_T atype) } } +/// "assert_inrange(lower, upper[, msg])" function +static void f_assert_inrange(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + assert_inrange(argvars); +} + /// "assert_match(pattern, actual[, msg])" function static void f_assert_match(typval_T *argvars, typval_T *rettv, FunPtr fptr) { diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index fa19ff209e..e3c5981b32 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -29,6 +29,7 @@ return { assert_exception={args={1, 2}}, assert_fails={args={1, 2}}, assert_false={args={1, 2}}, + assert_inrange={args={2, 3}}, assert_match={args={2, 3}}, assert_notequal={args={2, 3}}, assert_notmatch={args={2, 3}}, diff --git a/src/nvim/eval_defs.h b/src/nvim/eval_defs.h index fb2822b851..39028fdb11 100644 --- a/src/nvim/eval_defs.h +++ b/src/nvim/eval_defs.h @@ -278,6 +278,7 @@ typedef enum ASSERT_NOTEQUAL, ASSERT_MATCH, ASSERT_NOTMATCH, + ASSERT_INRANGE, ASSERT_OTHER, } assert_type_T; diff --git a/src/nvim/version.c b/src/nvim/version.c index 87acb3f361..46009de4ea 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -345,7 +345,7 @@ static int included_patches[] = { 2098, // 2097, 2096, - // 2095, + 2095, // 2094 NA // 2093 NA // 2092 NA diff --git a/test/functional/legacy/assert_spec.lua b/test/functional/legacy/assert_spec.lua index 8a042be7f7..5c7268486a 100644 --- a/test/functional/legacy/assert_spec.lua +++ b/test/functional/legacy/assert_spec.lua @@ -235,6 +235,26 @@ describe('assert function:', function() end) end) + -- assert_inrange({lower}, {upper}, {actual}[, {msg}]) + describe('assert_inrange()', function() + it('should not change v:errors when actual is in range', function() + call('assert_inrange', 7, 7, 7) + call('assert_inrange', 5, 7, 5) + call('assert_inrange', 5, 7, 6) + call('assert_inrange', 5, 7, 7) + expected_empty() + end) + + it('should change v:errors when actual is not in range', function() + call('assert_inrange', 5, 7, 4) + call('assert_inrange', 5, 7, 8) + expected_errors({ + "Expected range 5 - 7, but got 4", + "Expected range 5 - 7, but got 8", + }) + end) + end) + -- assert_exception({cmd}, [, {error}]) describe('assert_exception()', function() it('should assert thrown exceptions properly', function() |