comparison src/testing.c @ 20834:9a624c1672a3 v8.2.0969

patch 8.2.0969: assert_equal() output for dicts is hard to figure out Commit: https://github.com/vim/vim/commit/4a021dfbeee88ac09d07e257912485314ecdcabe Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jun 13 15:13:38 2020 +0200 patch 8.2.0969: assert_equal() output for dicts is hard to figure out Problem: Assert_equal() output for dicts is hard to figure out. Solution: Only show the different items.
author Bram Moolenaar <Bram@vim.org>
date Sat, 13 Jun 2020 15:15:04 +0200
parents b9a6a129b94e
children 2616c5a337e0
comparison
equal deleted inserted replaced
20833:ca13ee9c6f8a 20834:9a624c1672a3
129 static void 129 static void
130 fill_assert_error( 130 fill_assert_error(
131 garray_T *gap, 131 garray_T *gap,
132 typval_T *opt_msg_tv, 132 typval_T *opt_msg_tv,
133 char_u *exp_str, 133 char_u *exp_str,
134 typval_T *exp_tv, 134 typval_T *exp_tv_arg,
135 typval_T *got_tv, 135 typval_T *got_tv_arg,
136 assert_type_T atype) 136 assert_type_T atype)
137 { 137 {
138 char_u numbuf[NUMBUFLEN]; 138 char_u numbuf[NUMBUFLEN];
139 char_u *tofree; 139 char_u *tofree;
140 typval_T *exp_tv = exp_tv_arg;
141 typval_T *got_tv = got_tv_arg;
142 int did_copy = FALSE;
143 int omitted = 0;
140 144
141 if (opt_msg_tv->v_type != VAR_UNKNOWN) 145 if (opt_msg_tv->v_type != VAR_UNKNOWN)
142 { 146 {
143 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0)); 147 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
144 vim_free(tofree); 148 vim_free(tofree);
151 ga_concat(gap, (char_u *)"Expected not equal to "); 155 ga_concat(gap, (char_u *)"Expected not equal to ");
152 else 156 else
153 ga_concat(gap, (char_u *)"Expected "); 157 ga_concat(gap, (char_u *)"Expected ");
154 if (exp_str == NULL) 158 if (exp_str == NULL)
155 { 159 {
160 // When comparing dictionaries, drop the items that are equal, so that
161 // it's a lot easier to see what differs.
162 if (atype != ASSERT_NOTEQUAL
163 && exp_tv->v_type == VAR_DICT && got_tv->v_type == VAR_DICT
164 && exp_tv->vval.v_dict != NULL && got_tv->vval.v_dict != NULL)
165 {
166 dict_T *exp_d = exp_tv->vval.v_dict;
167 dict_T *got_d = got_tv->vval.v_dict;
168 hashitem_T *hi;
169 dictitem_T *item2;
170 int todo;
171
172 did_copy = TRUE;
173 exp_tv->vval.v_dict = dict_alloc();
174 got_tv->vval.v_dict = dict_alloc();
175 if (exp_tv->vval.v_dict == NULL || got_tv->vval.v_dict == NULL)
176 return;
177
178 todo = (int)exp_d->dv_hashtab.ht_used;
179 for (hi = exp_d->dv_hashtab.ht_array; todo > 0; ++hi)
180 {
181 if (!HASHITEM_EMPTY(hi))
182 {
183 item2 = dict_find(got_d, hi->hi_key, -1);
184 if (item2 == NULL || !tv_equal(&HI2DI(hi)->di_tv,
185 &item2->di_tv, FALSE, FALSE))
186 {
187 // item of exp_d not present in got_d or values differ.
188 dict_add_tv(exp_tv->vval.v_dict,
189 (char *)hi->hi_key, &HI2DI(hi)->di_tv);
190 if (item2 != NULL)
191 dict_add_tv(got_tv->vval.v_dict,
192 (char *)hi->hi_key, &item2->di_tv);
193 }
194 else
195 ++omitted;
196 --todo;
197 }
198 }
199
200 // Add items only present in got_d.
201 todo = (int)got_d->dv_hashtab.ht_used;
202 for (hi = got_d->dv_hashtab.ht_array; todo > 0; ++hi)
203 {
204 if (!HASHITEM_EMPTY(hi))
205 {
206 item2 = dict_find(exp_d, hi->hi_key, -1);
207 if (item2 == NULL)
208 // item of got_d not present in exp_d
209 dict_add_tv(got_tv->vval.v_dict,
210 (char *)hi->hi_key, &HI2DI(hi)->di_tv);
211 --todo;
212 }
213 }
214 }
215
156 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0)); 216 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
157 vim_free(tofree); 217 vim_free(tofree);
158 } 218 }
159 else 219 else
160 ga_concat_shorten_esc(gap, exp_str); 220 ga_concat_shorten_esc(gap, exp_str);
166 ga_concat(gap, (char_u *)" does match "); 226 ga_concat(gap, (char_u *)" does match ");
167 else 227 else
168 ga_concat(gap, (char_u *)" but got "); 228 ga_concat(gap, (char_u *)" but got ");
169 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0)); 229 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
170 vim_free(tofree); 230 vim_free(tofree);
231
232 if (omitted != 0)
233 {
234 char buf[100];
235
236 vim_snprintf(buf, 100, " - %d equal item%s omitted",
237 omitted, omitted == 1 ? "" : "s");
238 ga_concat(gap, (char_u *)buf);
239 }
240 }
241
242 if (did_copy)
243 {
244 clear_tv(exp_tv);
245 clear_tv(got_tv);
171 } 246 }
172 } 247 }
173 248
174 static int 249 static int
175 assert_equal_common(typval_T *argvars, assert_type_T atype) 250 assert_equal_common(typval_T *argvars, assert_type_T atype)