]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.0.1507: assert message is confusing with boolean result v9.0.1507
authorzeertzjq <zeertzjq@outlook.com>
Thu, 4 May 2023 17:58:22 +0000 (18:58 +0100)
committerBram Moolenaar <Bram@vim.org>
Thu, 4 May 2023 17:58:22 +0000 (18:58 +0100)
Problem:    Assert message is confusing with boolean result.  assert_inrange()
            replaces message instead of adding it.
Solution:   Don't put quotes around expected boolean value.  Append message
            for assert_inrange(). (closes #12342, closes #12341)

src/testdir/test_assert.vim
src/testing.c
src/version.c
src/vim.h

index ecf5024fdb1c99fd70370319f9482d13d10c6935..b7530bf1b98527fafb817596443e42eb5dfd3461 100644 (file)
@@ -9,11 +9,11 @@ func Test_assert_false()
   call assert_equal(0, v:false->assert_false())
 
   call assert_equal(1, assert_false(123))
-  call assert_match("Expected 'False' but got 123", v:errors[0])
+  call assert_match("Expected False but got 123", v:errors[0])
   call remove(v:errors, 0)
 
   call assert_equal(1, 123->assert_false())
-  call assert_match("Expected 'False' but got 123", v:errors[0])
+  call assert_match("Expected False but got 123", v:errors[0])
   call remove(v:errors, 0)
 endfunc
 
@@ -24,11 +24,11 @@ func Test_assert_true()
   call assert_equal(0, v:true->assert_true())
 
   call assert_equal(1, assert_true(0))
-  call assert_match("Expected 'True' but got 0", v:errors[0])
+  call assert_match("Expected True but got 0", v:errors[0])
   call remove(v:errors, 0)
 
   call assert_equal(1, 0->assert_true())
-  call assert_match("Expected 'True' but got 0", v:errors[0])
+  call assert_match("Expected True but got 0", v:errors[0])
   call remove(v:errors, 0)
 endfunc
 
@@ -416,8 +416,11 @@ func Test_assert_inrange()
   call remove(v:errors, 0)
 
   " Use a custom message
+  call assert_equal(1, assert_inrange(5, 7, 8, "Higher"))
+  call assert_match("Higher: Expected range 5 - 7, but got 8", v:errors[0])
+  call remove(v:errors, 0)
   call assert_equal(1, assert_inrange(5, 7, 8.0, "Higher"))
-  call assert_match("Higher", v:errors[0])
+  call assert_match("Higher: Expected range 5.0 - 7.0, but got 8.0", v:errors[0])
   call remove(v:errors, 0)
 
   " Invalid arguments
index 3f4387c4989d7f1691449c9bed466df87b02e1a9..0954814774e30638a3cc3873b1a4bea0337d507f 100644 (file)
@@ -223,9 +223,11 @@ fill_assert_error(
     }
     else
     {
-       ga_concat(gap, (char_u *)"'");
+       if (atype == ASSERT_FAILS)
+           ga_concat(gap, (char_u *)"'");
        ga_concat_shorten_esc(gap, exp_str);
-       ga_concat(gap, (char_u *)"'");
+       if (atype == ASSERT_FAILS)
+           ga_concat(gap, (char_u *)"'");
     }
     if (atype != ASSERT_NOTEQUAL)
     {
@@ -743,7 +745,7 @@ f_assert_fails(typval_T *argvars, typval_T *rettv)
                actual_tv.vval.v_string = actual;
            }
            fill_assert_error(&ga, &argvars[2], expected_str,
-                       &argvars[error_found_index], &actual_tv, ASSERT_OTHER);
+                       &argvars[error_found_index], &actual_tv, ASSERT_FAILS);
            ga_concat(&ga, (char_u *)": ");
            assert_append_cmd_or_arg(&ga, argvars, cmd);
            assert_error(&ga);
@@ -785,9 +787,7 @@ assert_inrange(typval_T *argvars)
 {
     garray_T   ga;
     int                error = FALSE;
-    char_u     *tofree;
-    char       msg[200];
-    char_u     numbuf[NUMBUFLEN];
+    char_u     expected_str[200];
 
     if (argvars[0].v_type == VAR_FLOAT
            || argvars[1].v_type == VAR_FLOAT
@@ -800,17 +800,10 @@ assert_inrange(typval_T *argvars)
        if (factual < flower || factual > fupper)
        {
            prepare_assert_error(&ga);
-           if (argvars[3].v_type != VAR_UNKNOWN)
-           {
-               ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
-               vim_free(tofree);
-           }
-           else
-           {
-               vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
-                                                     flower, fupper, factual);
-               ga_concat(&ga, (char_u *)msg);
-           }
+           vim_snprintf((char *)expected_str, 200, "range %g - %g,",
+                                                              flower, fupper);
+           fill_assert_error(&ga, &argvars[3], expected_str, NULL,
+                                                   &argvars[2], ASSERT_OTHER);
            assert_error(&ga);
            ga_clear(&ga);
            return 1;
@@ -827,17 +820,10 @@ assert_inrange(typval_T *argvars)
        if (actual < lower || actual > upper)
        {
            prepare_assert_error(&ga);
-           if (argvars[3].v_type != VAR_UNKNOWN)
-           {
-               ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
-               vim_free(tofree);
-           }
-           else
-           {
-               vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
-                                      (long)lower, (long)upper, (long)actual);
-               ga_concat(&ga, (char_u *)msg);
-           }
+           vim_snprintf((char *)expected_str, 200, "range %ld - %ld,",
+                                                    (long)lower, (long)upper);
+           fill_assert_error(&ga, &argvars[3], expected_str, NULL,
+                                                   &argvars[2], ASSERT_OTHER);
            assert_error(&ga);
            ga_clear(&ga);
            return 1;
index b70825408a1633f1a8e49b95fc46ef0df10f1653..5db46d8c0ea753ae4a25a396a596bcd47bc030d7 100644 (file)
@@ -695,6 +695,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1507,
 /**/
     1506,
 /**/
index 4e0f2cd12e07f4ab31f232ed260b6c4f02ee99c7..8d0f3e0b06f6bdf15fa734e65cd301a9bb694e60 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -2254,6 +2254,7 @@ typedef enum {
     ASSERT_NOTEQUAL,
     ASSERT_MATCH,
     ASSERT_NOTMATCH,
+    ASSERT_FAILS,
     ASSERT_OTHER
 } assert_type_T;