comparison src/eval.c @ 7277:6600871bb38c v7.4.944

commit https://github.com/vim/vim/commit/43345546ae63710441f066648b8485fb545b3801 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Nov 29 17:35:35 2015 +0100 patch 7.4.944 Problem: Writing tests for Vim script is hard. Solution: Add assertEqual(), assertFalse() and assertTrue() functions. Add the v:errors variable. Add the runtest script. Add a first new style test script.
author Christian Brabandt <cb@256bit.org>
date Sun, 29 Nov 2015 17:45:04 +0100
parents a01294e1c02f
children 2a4593702d5c
comparison
equal deleted inserted replaced
7276:b03d6d4fe42b 7277:6600871bb38c
366 {VV_NAME("progpath", VAR_STRING), VV_RO}, 366 {VV_NAME("progpath", VAR_STRING), VV_RO},
367 {VV_NAME("completed_item", VAR_DICT), VV_RO}, 367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
368 {VV_NAME("option_new", VAR_STRING), VV_RO}, 368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO}, 369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO}, 370 {VV_NAME("option_type", VAR_STRING), VV_RO},
371 {VV_NAME("errors", VAR_LIST), 0},
371 }; 372 };
372 373
373 /* shorthand */ 374 /* shorthand */
374 #define vv_type vv_di.di_tv.v_type 375 #define vv_type vv_di.di_tv.v_type
375 #define vv_nr vv_di.di_tv.vval.v_number 376 #define vv_nr vv_di.di_tv.vval.v_number
470 static void f_append __ARGS((typval_T *argvars, typval_T *rettv)); 471 static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
471 static void f_argc __ARGS((typval_T *argvars, typval_T *rettv)); 472 static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
472 static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv)); 473 static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
473 static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv)); 474 static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
474 static void f_argv __ARGS((typval_T *argvars, typval_T *rettv)); 475 static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
476 static void f_assertEqual __ARGS((typval_T *argvars, typval_T *rettv));
477 static void f_assertFalse __ARGS((typval_T *argvars, typval_T *rettv));
478 static void f_assertTrue __ARGS((typval_T *argvars, typval_T *rettv));
475 #ifdef FEAT_FLOAT 479 #ifdef FEAT_FLOAT
476 static void f_asin __ARGS((typval_T *argvars, typval_T *rettv)); 480 static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
477 static void f_atan __ARGS((typval_T *argvars, typval_T *rettv)); 481 static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
478 static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv)); 482 static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
479 #endif 483 #endif
8066 {"append", 2, 2, f_append}, 8070 {"append", 2, 2, f_append},
8067 {"argc", 0, 0, f_argc}, 8071 {"argc", 0, 0, f_argc},
8068 {"argidx", 0, 0, f_argidx}, 8072 {"argidx", 0, 0, f_argidx},
8069 {"arglistid", 0, 2, f_arglistid}, 8073 {"arglistid", 0, 2, f_arglistid},
8070 {"argv", 0, 1, f_argv}, 8074 {"argv", 0, 1, f_argv},
8075 {"assertEqual", 2, 3, f_assertEqual},
8076 {"assertFalse", 1, 2, f_assertFalse},
8077 {"assertTrue", 1, 2, f_assertTrue},
8071 #ifdef FEAT_FLOAT 8078 #ifdef FEAT_FLOAT
8072 {"asin", 1, 1, f_asin}, /* WJMc */ 8079 {"asin", 1, 1, f_asin}, /* WJMc */
8073 {"atan", 1, 1, f_atan}, 8080 {"atan", 1, 1, f_atan},
8074 {"atan2", 2, 2, f_atan2}, 8081 {"atan2", 2, 2, f_atan2},
8075 #endif 8082 #endif
9122 for (idx = 0; idx < ARGCOUNT; ++idx) 9129 for (idx = 0; idx < ARGCOUNT; ++idx)
9123 list_append_string(rettv->vval.v_list, 9130 list_append_string(rettv->vval.v_list,
9124 alist_name(&ARGLIST[idx]), -1); 9131 alist_name(&ARGLIST[idx]), -1);
9125 } 9132 }
9126 9133
9134 static void assertError __ARGS((garray_T *gap));
9135 static void prepareForAssertError __ARGS((garray_T*gap));
9136 static void assertBool __ARGS((typval_T *argvars, int isTrue));
9137
9138 /*
9139 * Add an assert error to v:errors.
9140 */
9141 static void
9142 assertError(gap)
9143 garray_T *gap;
9144 {
9145 struct vimvar *vp = &vimvars[VV_ERRORS];
9146
9147 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9148 /* Make sure v:errors is a list. */
9149 set_vim_var_list(VV_ERRORS, list_alloc());
9150 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9151 }
9152
9153 static void
9154 prepareForAssertError(gap)
9155 garray_T *gap;
9156 {
9157 char buf[NUMBUFLEN];
9158
9159 ga_init2(gap, 1, 100);
9160 ga_concat(gap, sourcing_name);
9161 sprintf(buf, " line %ld", (long)sourcing_lnum);
9162 ga_concat(gap, (char_u *)buf);
9163 }
9164
9165 /*
9166 * "assertEqual(expected, actual[, msg])" function
9167 */
9168 static void
9169 f_assertEqual(argvars, rettv)
9170 typval_T *argvars;
9171 typval_T *rettv UNUSED;
9172 {
9173 garray_T ga;
9174 char_u *tofree;
9175 char_u numbuf[NUMBUFLEN];
9176
9177 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9178 {
9179 prepareForAssertError(&ga);
9180 ga_concat(&ga, (char_u *)": Expected ");
9181 ga_concat(&ga, tv2string(&argvars[0], &tofree, numbuf, 0));
9182 vim_free(tofree);
9183 ga_concat(&ga, (char_u *)" but got ");
9184 ga_concat(&ga, tv2string(&argvars[1], &tofree, numbuf, 0));
9185 vim_free(tofree);
9186 assertError(&ga);
9187 ga_clear(&ga);
9188 }
9189 }
9190
9191 static void
9192 assertBool(argvars, isTrue)
9193 typval_T *argvars;
9194 int isTrue;
9195 {
9196 int error = FALSE;
9197 garray_T ga;
9198 char_u *tofree;
9199 char_u numbuf[NUMBUFLEN];
9200
9201 if (argvars[0].v_type != VAR_NUMBER
9202 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9203 || error)
9204 {
9205 prepareForAssertError(&ga);
9206 ga_concat(&ga, (char_u *)": Expected ");
9207 if (isTrue)
9208 ga_concat(&ga, (char_u *)"True ");
9209 else
9210 ga_concat(&ga, (char_u *)"False ");
9211 ga_concat(&ga, (char_u *)"but got ");
9212 ga_concat(&ga, tv2string(&argvars[0], &tofree, numbuf, 0));
9213 vim_free(tofree);
9214 assertError(&ga);
9215 ga_clear(&ga);
9216 }
9217 }
9218
9219 /*
9220 * "assertFalse(actual[, msg])" function
9221 */
9222 static void
9223 f_assertFalse(argvars, rettv)
9224 typval_T *argvars;
9225 typval_T *rettv UNUSED;
9226 {
9227 assertBool(argvars, FALSE);
9228 }
9229
9230 /*
9231 * "assertTrue(actual[, msg])" function
9232 */
9233 static void
9234 f_assertTrue(argvars, rettv)
9235 typval_T *argvars;
9236 typval_T *rettv UNUSED;
9237 {
9238 assertBool(argvars, TRUE);
9239 }
9240
9127 #ifdef FEAT_FLOAT 9241 #ifdef FEAT_FLOAT
9128 /* 9242 /*
9129 * "asin()" function 9243 * "asin()" function
9130 */ 9244 */
9131 static void 9245 static void