comparison src/testdir/test_vim9_class.vim @ 31604:9b13b3a63bc0 v9.0.1134

patch 9.0.1134: comparing objects uses identity instead of equality Commit: https://github.com/vim/vim/commit/bcf31ec36b4b056bf06d21036640c6f0235e9c2b Author: Bram Moolenaar <Bram@vim.org> Date: Mon Jan 2 20:32:24 2023 +0000 patch 9.0.1134: comparing objects uses identity instead of equality Problem: Comparing objects uses identity instead of equality. Solution: Compare the object values.
author Bram Moolenaar <Bram@vim.org>
date Mon, 02 Jan 2023 21:45:02 +0100
parents aee868b9229a
children f3c7e573b7be
comparison
equal deleted inserted replaced
31603:2b8fe38a9989 31604:9b13b3a63bc0
365 var c = MyCar.new("def") 365 var c = MyCar.new("def")
366 END 366 END
367 v9.CheckScriptFailure(lines, 'E1041:') 367 v9.CheckScriptFailure(lines, 'E1041:')
368 enddef 368 enddef
369 369
370 def Test_class_object_compare()
371 var class_lines =<< trim END
372 vim9script
373 class Item
374 this.nr = 0
375 this.name = 'xx'
376 endclass
377 END
378
379 # used at the script level and in a compiled function
380 var test_lines =<< trim END
381 var i1 = Item.new()
382 assert_equal(i1, i1)
383 assert_true(i1 is i1)
384 var i2 = Item.new()
385 assert_equal(i1, i2)
386 assert_false(i1 is i2)
387 var i3 = Item.new(0, 'xx')
388 assert_equal(i1, i3)
389
390 var io1 = Item.new(1, 'xx')
391 assert_notequal(i1, io1)
392 var io2 = Item.new(0, 'yy')
393 assert_notequal(i1, io2)
394 END
395
396 v9.CheckScriptSuccess(class_lines + test_lines)
397 # TODO: this does not work yet
398 #v9.CheckScriptSuccess(
399 # class_lines + ['def Test()'] + test_lines + ['enddef', 'Test()'])
400
401 for op in ['>', '>=', '<', '<=', '=~', '!~']
402 var op_lines = [
403 'var i1 = Item.new()',
404 'var i2 = Item.new()',
405 'echo i1 ' .. op .. ' i2',
406 ]
407 v9.CheckScriptFailure(class_lines + op_lines, 'E1153: Invalid operation for object')
408 # TODO: this does not work yet
409 #v9.CheckScriptFailure(class_lines
410 # + ['def Test()'] + op_lines + ['enddef', 'Test()'], 'E99:')
411 endfor
412 enddef
413
370 def Test_class_member() 414 def Test_class_member()
371 # check access rules 415 # check access rules
372 var lines =<< trim END 416 var lines =<< trim END
373 vim9script 417 vim9script
374 class TextPos 418 class TextPos