diff options
author | LemonBoy <thatlemon@gmail.com> | 2023-08-23 21:08:11 +0200 |
---|---|---|
committer | Christian Brabandt <cb@256bit.org> | 2023-08-23 21:08:11 +0200 |
commit | afe0466fb1695fa8b9782eea8a8e9f9540d4cb85 (patch) | |
tree | f71446a214e45f7c0cdcb186fe40d25d1a1da39d /src/testdir | |
parent | 1193951bebcff50d88403ce17dec5d3be14f131d (diff) |
patch 9.0.1786: Vim9: need instanceof() functionv9.0.1786
Problem: Vim9: need instanceof() function
Solution: Implement instanceof() builtin
Implemented in the same form as Python's isinstance because it allows
for checking multiple class types at the same time.
closes: #12867
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: LemonBoy <thatlemon@gmail.com>
Diffstat (limited to 'src/testdir')
-rw-r--r-- | src/testdir/test_vim9_builtin.vim | 18 | ||||
-rw-r--r-- | src/testdir/test_vim9_class.vim | 33 |
2 files changed, 51 insertions, 0 deletions
diff --git a/src/testdir/test_vim9_builtin.vim b/src/testdir/test_vim9_builtin.vim index d64366d03c..da8bc4299f 100644 --- a/src/testdir/test_vim9_builtin.vim +++ b/src/testdir/test_vim9_builtin.vim @@ -2301,6 +2301,24 @@ def Test_insert() v9.CheckDefAndScriptFailure(['insert([2, 3], 1, "x")'], ['E1013: Argument 3: type mismatch, expected number but got string', 'E1210: Number required for argument 3']) enddef +def Test_instanceof() + var lines =<< trim END + vim9script + class Foo + endclass + instanceof('hello', Foo) + END + v9.CheckScriptFailure(lines, 'E616: Object required for argument 1') + + lines =<< trim END + vim9script + class Foo + endclass + instanceof(Foo.new(), 123) + END + v9.CheckScriptFailure(lines, 'E693: List or Class required for argument 2') +enddef + def Test_invert() v9.CheckDefAndScriptFailure(['invert("x")'], ['E1013: Argument 1: type mismatch, expected number but got string', 'E1210: Number required for argument 1']) enddef diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim index 7e33c6d448..a650cc2509 100644 --- a/src/testdir/test_vim9_class.vim +++ b/src/testdir/test_vim9_class.vim @@ -2367,6 +2367,39 @@ def Test_call_method_in_extended_class() v9.CheckScriptSuccess(lines) enddef +def Test_instanceof() + var lines =<< trim END + vim9script + + class Base1 + endclass + + class Base2 extends Base1 + endclass + + interface Intf1 + endinterface + + class Mix1 implements Intf1 + endclass + + class Base3 extends Mix1 + endclass + + var b1 = Base1.new() + var b2 = Base2.new() + var b3 = Base3.new() + + assert_true(instanceof(b1, Base1)) + assert_true(instanceof(b2, Base1)) + assert_false(instanceof(b1, Base2)) + assert_true(instanceof(b3, Mix1)) + assert_false(instanceof(b3, [])) + assert_true(instanceof(b3, [Base1, Base2, Intf1])) + END + v9.CheckScriptSuccess(lines) +enddef + " Test for calling a method in the parent class that is extended partially. " This used to fail with the 'E118: Too many arguments for function: Text' error " message (Github issue #12524). |