Mercurial > vim
comparison src/eval.c @ 13796:87012d2b17b5 v8.0.1770
patch 8.0.1770: assert functions don't return anything
commit https://github.com/vim/vim/commit/65a5464985f980d2bbbf4e14d39d416dce065ec7
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Apr 28 16:56:53 2018 +0200
patch 8.0.1770: assert functions don't return anything
Problem: Assert functions don't return anything.
Solution: Return non-zero when the assertion fails.
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sat, 28 Apr 2018 17:00:06 +0200 |
parents | e76499e85744 |
children | eadecbe4e390 |
comparison
equal
deleted
inserted
replaced
13795:d3f0d82cf9ec | 13796:87012d2b17b5 |
---|---|
8813 /* Make sure v:errors is a list. */ | 8813 /* Make sure v:errors is a list. */ |
8814 set_vim_var_list(VV_ERRORS, list_alloc()); | 8814 set_vim_var_list(VV_ERRORS, list_alloc()); |
8815 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len); | 8815 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len); |
8816 } | 8816 } |
8817 | 8817 |
8818 void | 8818 int |
8819 assert_equal_common(typval_T *argvars, assert_type_T atype) | 8819 assert_equal_common(typval_T *argvars, assert_type_T atype) |
8820 { | 8820 { |
8821 garray_T ga; | 8821 garray_T ga; |
8822 | 8822 |
8823 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE) | 8823 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE) |
8826 prepare_assert_error(&ga); | 8826 prepare_assert_error(&ga); |
8827 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], | 8827 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], |
8828 atype); | 8828 atype); |
8829 assert_error(&ga); | 8829 assert_error(&ga); |
8830 ga_clear(&ga); | 8830 ga_clear(&ga); |
8831 } | 8831 return 1; |
8832 } | 8832 } |
8833 | 8833 return 0; |
8834 void | 8834 } |
8835 | |
8836 int | |
8835 assert_equalfile(typval_T *argvars) | 8837 assert_equalfile(typval_T *argvars) |
8836 { | 8838 { |
8837 char_u buf1[NUMBUFLEN]; | 8839 char_u buf1[NUMBUFLEN]; |
8838 char_u buf2[NUMBUFLEN]; | 8840 char_u buf2[NUMBUFLEN]; |
8839 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1); | 8841 char_u *fname1 = get_tv_string_buf_chk(&argvars[0], buf1); |
8841 garray_T ga; | 8843 garray_T ga; |
8842 FILE *fd1; | 8844 FILE *fd1; |
8843 FILE *fd2; | 8845 FILE *fd2; |
8844 | 8846 |
8845 if (fname1 == NULL || fname2 == NULL) | 8847 if (fname1 == NULL || fname2 == NULL) |
8846 return; | 8848 return 0; |
8847 | 8849 |
8848 IObuff[0] = NUL; | 8850 IObuff[0] = NUL; |
8849 fd1 = mch_fopen((char *)fname1, READBIN); | 8851 fd1 = mch_fopen((char *)fname1, READBIN); |
8850 if (fd1 == NULL) | 8852 if (fd1 == NULL) |
8851 { | 8853 { |
8895 { | 8897 { |
8896 prepare_assert_error(&ga); | 8898 prepare_assert_error(&ga); |
8897 ga_concat(&ga, IObuff); | 8899 ga_concat(&ga, IObuff); |
8898 assert_error(&ga); | 8900 assert_error(&ga); |
8899 ga_clear(&ga); | 8901 ga_clear(&ga); |
8900 } | 8902 return 1; |
8901 } | 8903 } |
8902 | 8904 return 0; |
8903 void | 8905 } |
8906 | |
8907 int | |
8904 assert_match_common(typval_T *argvars, assert_type_T atype) | 8908 assert_match_common(typval_T *argvars, assert_type_T atype) |
8905 { | 8909 { |
8906 garray_T ga; | 8910 garray_T ga; |
8907 char_u buf1[NUMBUFLEN]; | 8911 char_u buf1[NUMBUFLEN]; |
8908 char_u buf2[NUMBUFLEN]; | 8912 char_u buf2[NUMBUFLEN]; |
8916 prepare_assert_error(&ga); | 8920 prepare_assert_error(&ga); |
8917 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], | 8921 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1], |
8918 atype); | 8922 atype); |
8919 assert_error(&ga); | 8923 assert_error(&ga); |
8920 ga_clear(&ga); | 8924 ga_clear(&ga); |
8921 } | 8925 return 1; |
8922 } | 8926 } |
8923 | 8927 return 0; |
8924 void | 8928 } |
8929 | |
8930 int | |
8925 assert_inrange(typval_T *argvars) | 8931 assert_inrange(typval_T *argvars) |
8926 { | 8932 { |
8927 garray_T ga; | 8933 garray_T ga; |
8928 int error = FALSE; | 8934 int error = FALSE; |
8929 varnumber_T lower = get_tv_number_chk(&argvars[0], &error); | 8935 varnumber_T lower = get_tv_number_chk(&argvars[0], &error); |
8932 char_u *tofree; | 8938 char_u *tofree; |
8933 char msg[200]; | 8939 char msg[200]; |
8934 char_u numbuf[NUMBUFLEN]; | 8940 char_u numbuf[NUMBUFLEN]; |
8935 | 8941 |
8936 if (error) | 8942 if (error) |
8937 return; | 8943 return 0; |
8938 if (actual < lower || actual > upper) | 8944 if (actual < lower || actual > upper) |
8939 { | 8945 { |
8940 prepare_assert_error(&ga); | 8946 prepare_assert_error(&ga); |
8941 if (argvars[3].v_type != VAR_UNKNOWN) | 8947 if (argvars[3].v_type != VAR_UNKNOWN) |
8942 { | 8948 { |
8949 (long)lower, (long)upper, (long)actual); | 8955 (long)lower, (long)upper, (long)actual); |
8950 ga_concat(&ga, (char_u *)msg); | 8956 ga_concat(&ga, (char_u *)msg); |
8951 } | 8957 } |
8952 assert_error(&ga); | 8958 assert_error(&ga); |
8953 ga_clear(&ga); | 8959 ga_clear(&ga); |
8954 } | 8960 return 1; |
8961 } | |
8962 return 0; | |
8955 } | 8963 } |
8956 | 8964 |
8957 /* | 8965 /* |
8958 * Common for assert_true() and assert_false(). | 8966 * Common for assert_true() and assert_false(). |
8959 */ | 8967 * Return non-zero for failure. |
8960 void | 8968 */ |
8969 int | |
8961 assert_bool(typval_T *argvars, int isTrue) | 8970 assert_bool(typval_T *argvars, int isTrue) |
8962 { | 8971 { |
8963 int error = FALSE; | 8972 int error = FALSE; |
8964 garray_T ga; | 8973 garray_T ga; |
8965 | 8974 |
8966 if (argvars[0].v_type == VAR_SPECIAL | 8975 if (argvars[0].v_type == VAR_SPECIAL |
8967 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE)) | 8976 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE)) |
8968 return; | 8977 return 0; |
8969 if (argvars[0].v_type != VAR_NUMBER | 8978 if (argvars[0].v_type != VAR_NUMBER |
8970 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue | 8979 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue |
8971 || error) | 8980 || error) |
8972 { | 8981 { |
8973 prepare_assert_error(&ga); | 8982 prepare_assert_error(&ga); |
8974 fill_assert_error(&ga, &argvars[1], | 8983 fill_assert_error(&ga, &argvars[1], |
8975 (char_u *)(isTrue ? "True" : "False"), | 8984 (char_u *)(isTrue ? "True" : "False"), |
8976 NULL, &argvars[0], ASSERT_OTHER); | 8985 NULL, &argvars[0], ASSERT_OTHER); |
8977 assert_error(&ga); | 8986 assert_error(&ga); |
8978 ga_clear(&ga); | 8987 ga_clear(&ga); |
8979 } | 8988 return 1; |
8980 } | 8989 } |
8981 | 8990 return 0; |
8982 void | 8991 } |
8992 | |
8993 int | |
8983 assert_report(typval_T *argvars) | 8994 assert_report(typval_T *argvars) |
8984 { | 8995 { |
8985 garray_T ga; | 8996 garray_T ga; |
8986 | 8997 |
8987 prepare_assert_error(&ga); | 8998 prepare_assert_error(&ga); |
8988 ga_concat(&ga, get_tv_string(&argvars[0])); | 8999 ga_concat(&ga, get_tv_string(&argvars[0])); |
8989 assert_error(&ga); | 9000 assert_error(&ga); |
8990 ga_clear(&ga); | 9001 ga_clear(&ga); |
8991 } | 9002 return 1; |
8992 | 9003 } |
8993 void | 9004 |
9005 int | |
8994 assert_exception(typval_T *argvars) | 9006 assert_exception(typval_T *argvars) |
8995 { | 9007 { |
8996 garray_T ga; | 9008 garray_T ga; |
8997 char_u *error = get_tv_string_chk(&argvars[0]); | 9009 char_u *error = get_tv_string_chk(&argvars[0]); |
8998 | 9010 |
9000 { | 9012 { |
9001 prepare_assert_error(&ga); | 9013 prepare_assert_error(&ga); |
9002 ga_concat(&ga, (char_u *)"v:exception is not set"); | 9014 ga_concat(&ga, (char_u *)"v:exception is not set"); |
9003 assert_error(&ga); | 9015 assert_error(&ga); |
9004 ga_clear(&ga); | 9016 ga_clear(&ga); |
9017 return 1; | |
9005 } | 9018 } |
9006 else if (error != NULL | 9019 else if (error != NULL |
9007 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL) | 9020 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL) |
9008 { | 9021 { |
9009 prepare_assert_error(&ga); | 9022 prepare_assert_error(&ga); |
9010 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], | 9023 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0], |
9011 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER); | 9024 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER); |
9012 assert_error(&ga); | 9025 assert_error(&ga); |
9013 ga_clear(&ga); | 9026 ga_clear(&ga); |
9014 } | 9027 return 1; |
9015 } | 9028 } |
9016 | 9029 return 0; |
9017 void | 9030 } |
9031 | |
9032 int | |
9018 assert_beeps(typval_T *argvars) | 9033 assert_beeps(typval_T *argvars) |
9019 { | 9034 { |
9020 char_u *cmd = get_tv_string_chk(&argvars[0]); | 9035 char_u *cmd = get_tv_string_chk(&argvars[0]); |
9021 garray_T ga; | 9036 garray_T ga; |
9037 int ret = 0; | |
9022 | 9038 |
9023 called_vim_beep = FALSE; | 9039 called_vim_beep = FALSE; |
9024 suppress_errthrow = TRUE; | 9040 suppress_errthrow = TRUE; |
9025 emsg_silent = FALSE; | 9041 emsg_silent = FALSE; |
9026 do_cmdline_cmd(cmd); | 9042 do_cmdline_cmd(cmd); |
9029 prepare_assert_error(&ga); | 9045 prepare_assert_error(&ga); |
9030 ga_concat(&ga, (char_u *)"command did not beep: "); | 9046 ga_concat(&ga, (char_u *)"command did not beep: "); |
9031 ga_concat(&ga, cmd); | 9047 ga_concat(&ga, cmd); |
9032 assert_error(&ga); | 9048 assert_error(&ga); |
9033 ga_clear(&ga); | 9049 ga_clear(&ga); |
9050 ret = 1; | |
9034 } | 9051 } |
9035 | 9052 |
9036 suppress_errthrow = FALSE; | 9053 suppress_errthrow = FALSE; |
9037 emsg_on_display = FALSE; | 9054 emsg_on_display = FALSE; |
9038 } | 9055 return ret; |
9039 | 9056 } |
9040 void | 9057 |
9058 int | |
9041 assert_fails(typval_T *argvars) | 9059 assert_fails(typval_T *argvars) |
9042 { | 9060 { |
9043 char_u *cmd = get_tv_string_chk(&argvars[0]); | 9061 char_u *cmd = get_tv_string_chk(&argvars[0]); |
9044 garray_T ga; | 9062 garray_T ga; |
9063 int ret = 0; | |
9045 | 9064 |
9046 called_emsg = FALSE; | 9065 called_emsg = FALSE; |
9047 suppress_errthrow = TRUE; | 9066 suppress_errthrow = TRUE; |
9048 emsg_silent = TRUE; | 9067 emsg_silent = TRUE; |
9049 do_cmdline_cmd(cmd); | 9068 do_cmdline_cmd(cmd); |
9052 prepare_assert_error(&ga); | 9071 prepare_assert_error(&ga); |
9053 ga_concat(&ga, (char_u *)"command did not fail: "); | 9072 ga_concat(&ga, (char_u *)"command did not fail: "); |
9054 ga_concat(&ga, cmd); | 9073 ga_concat(&ga, cmd); |
9055 assert_error(&ga); | 9074 assert_error(&ga); |
9056 ga_clear(&ga); | 9075 ga_clear(&ga); |
9076 ret = 1; | |
9057 } | 9077 } |
9058 else if (argvars[1].v_type != VAR_UNKNOWN) | 9078 else if (argvars[1].v_type != VAR_UNKNOWN) |
9059 { | 9079 { |
9060 char_u buf[NUMBUFLEN]; | 9080 char_u buf[NUMBUFLEN]; |
9061 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf); | 9081 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf); |
9066 prepare_assert_error(&ga); | 9086 prepare_assert_error(&ga); |
9067 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], | 9087 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1], |
9068 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER); | 9088 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER); |
9069 assert_error(&ga); | 9089 assert_error(&ga); |
9070 ga_clear(&ga); | 9090 ga_clear(&ga); |
9091 ret = 1; | |
9071 } | 9092 } |
9072 } | 9093 } |
9073 | 9094 |
9074 called_emsg = FALSE; | 9095 called_emsg = FALSE; |
9075 suppress_errthrow = FALSE; | 9096 suppress_errthrow = FALSE; |
9076 emsg_silent = FALSE; | 9097 emsg_silent = FALSE; |
9077 emsg_on_display = FALSE; | 9098 emsg_on_display = FALSE; |
9078 set_vim_var_string(VV_ERRMSG, NULL, 0); | 9099 set_vim_var_string(VV_ERRMSG, NULL, 0); |
9100 return ret; | |
9079 } | 9101 } |
9080 | 9102 |
9081 /* | 9103 /* |
9082 * Append "str" to "gap", escaping unprintable characters. | 9104 * Append "str" to "gap", escaping unprintable characters. |
9083 * Changes NL to \n, CR to \r, etc. | 9105 * Changes NL to \n, CR to \r, etc. |