diff options
-rw-r--r-- | runtime/doc/eval.txt | 12 | ||||
-rw-r--r-- | src/nvim/eval.c | 56 | ||||
-rw-r--r-- | src/nvim/eval.lua | 1 | ||||
-rw-r--r-- | src/nvim/testdir/test_assert.vim | 35 |
4 files changed, 102 insertions, 2 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 512cfc4e58..fb39617c17 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -2007,6 +2007,8 @@ argv([-1, {winid}]) List the argument list assert_beeps({cmd}) none assert {cmd} causes a beep assert_equal({exp}, {act} [, {msg}]) none assert {exp} is equal to {act} +assert_equalfile({fname-one}, {fname-two}) + none assert file contents is equal assert_exception({error} [, {msg}]) none assert {error} is in v:exception assert_fails({cmd} [, {error}]) none assert {cmd} fails @@ -2597,6 +2599,13 @@ assert_equal({expected}, {actual}, [, {msg}]) < Will result in a string to be added to |v:errors|: test.vim line 12: Expected 'foo' but got 'bar' ~ + *assert_equalfile()* +assert_equalfile({fname-one}, {fname-two}) + When the files {fname-one} and {fname-two} do not contain + exactly the same text an error message is added to |v:errors|. + When {fname-one} or {fname-two} does not exist the error will + mention that. + assert_exception({error} [, {msg}]) *assert_exception()* When v:exception does not contain the string {error} an error message is added to |v:errors|. @@ -4479,8 +4488,7 @@ getftype({fname}) *getftype()* systems that support it. On some systems only "dir" and "file" are returned. - *getjumplist()* -getjumplist([{winnr} [, {tabnr}]]) +getjumplist([{winnr} [, {tabnr}]]) *getjumplist()* Returns the |jumplist| for the specified window. Without arguments use the current window. diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 1f753608d2..1a2bdcf88f 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -6961,6 +6961,56 @@ static void assert_equal_common(typval_T *argvars, assert_type_T atype) } } +static void assert_equalfile(typval_T *argvars) + FUNC_ATTR_NONNULL_ALL +{ + char buf1[NUMBUFLEN]; + char buf2[NUMBUFLEN]; + const char *const fname1 = tv_get_string_buf_chk(&argvars[0], buf1); + const char *const fname2 = tv_get_string_buf_chk(&argvars[1], buf2); + garray_T ga; + + if (fname1 == NULL || fname2 == NULL) { + return; + } + + IObuff[0] = NUL; + FILE *const fd1 = os_fopen(fname1, READBIN); + if (fd1 == NULL) { + snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1); + } else { + FILE *const fd2 = os_fopen(fname2, READBIN); + if (fd2 == NULL) { + fclose(fd1); + snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2); + } else { + for (int64_t count = 0; ; count++) { + const int c1 = fgetc(fd1); + const int c2 = fgetc(fd2); + if (c1 == EOF) { + if (c2 != EOF) { + STRCPY(IObuff, "first file is shorter"); + } + break; + } else if (c2 == EOF) { + STRCPY(IObuff, "second file is shorter"); + break; + } else if (c1 != c2) { + snprintf((char *)IObuff, IOSIZE, + "difference at byte %" PRId64, count); + break; + } + } + } + } + if (IObuff[0] != NUL) { + prepare_assert_error(&ga); + ga_concat(&ga, IObuff); + assert_error(&ga); + ga_clear(&ga); + } +} + static void f_assert_beeps(typval_T *argvars, typval_T *rettv, FunPtr fptr) { const char *const cmd = tv_get_string_chk(&argvars[0]); @@ -6988,6 +7038,12 @@ static void f_assert_equal(typval_T *argvars, typval_T *rettv, FunPtr fptr) assert_equal_common(argvars, ASSERT_EQUAL); } +// "assert_equalfile(fname-one, fname-two)" function +static void f_assert_equalfile(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + assert_equalfile(argvars); +} + // "assert_notequal(expected, actual[, msg])" function static void f_assert_notequal(typval_T *argvars, typval_T *rettv, FunPtr fptr) { diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index ab5ff57c2f..8efbcc71f1 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -28,6 +28,7 @@ return { asin={args=1, func="float_op_wrapper", data="&asin"}, -- WJMc assert_beeps={args={1, 2}}, assert_equal={args={2, 3}}, + assert_equalfile={args=2}, assert_exception={args={1, 2}}, assert_fails={args={1, 2}}, assert_false={args={1, 2}}, diff --git a/src/nvim/testdir/test_assert.vim b/src/nvim/testdir/test_assert.vim index a4c8ce7e43..cbb65ffc01 100644 --- a/src/nvim/testdir/test_assert.vim +++ b/src/nvim/testdir/test_assert.vim @@ -1,5 +1,40 @@ " Test that the methods used for testing work. +func Test_assert_equalfile() + call assert_equalfile('abcabc', 'xyzxyz') + call assert_match("E485: Can't read file abcabc", v:errors[0]) + call remove(v:errors, 0) + + let goodtext = ["one", "two", "three"] + call writefile(goodtext, 'Xone') + call assert_equalfile('Xone', 'xyzxyz') + call assert_match("E485: Can't read file xyzxyz", v:errors[0]) + call remove(v:errors, 0) + + call writefile(goodtext, 'Xtwo') + call assert_equalfile('Xone', 'Xtwo') + + call writefile([goodtext[0]], 'Xone') + call assert_equalfile('Xone', 'Xtwo') + call assert_match("first file is shorter", v:errors[0]) + call remove(v:errors, 0) + + call writefile(goodtext, 'Xone') + call writefile([goodtext[0]], 'Xtwo') + call assert_equalfile('Xone', 'Xtwo') + call assert_match("second file is shorter", v:errors[0]) + call remove(v:errors, 0) + + call writefile(['1234X89'], 'Xone') + call writefile(['1234Y89'], 'Xtwo') + call assert_equalfile('Xone', 'Xtwo') + call assert_match("difference at byte 4", v:errors[0]) + call remove(v:errors, 0) + + call delete('Xone') + call delete('Xtwo') +endfunc + func Test_assert_fails_in_try_block() try call assert_equal(0, assert_fails('throw "error"')) |