Mercurial > vim
comparison src/testdir/test_vim9_builtin.vim @ 27847:780818e65dff v8.2.4449
patch 8.2.4449: vim9: function argument of sort() not checked at compile time
Commit: https://github.com/vim/vim/commit/e843efcc3503b7af838dca3cb9dd2e17d4fc1672
Author: Bram Moolenaar <Bram@vim.org>
Date: Tue Feb 22 21:54:44 2022 +0000
patch 8.2.4449: vim9: function argument of sort() not checked at compile time
Problem: vim9: function argument of sort() not checked at compile time.
Solution: Add a compile time check.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Tue, 22 Feb 2022 23:00:03 +0100 |
parents | 5ce69c07a106 |
children | 05d222e5246a |
comparison
equal
deleted
inserted
replaced
27846:dcf84f8badcb | 27847:780818e65dff |
---|---|
3794 var l = [3, 6, 7, 1, 8, 2, 4, 5] | 3794 var l = [3, 6, 7, 1, 8, 2, 4, 5] |
3795 sort(l, Compare) | 3795 sort(l, Compare) |
3796 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l) | 3796 assert_equal([1, 2, 3, 4, 5, 6, 7, 8], l) |
3797 END | 3797 END |
3798 v9.CheckDefAndScriptSuccess(lines) | 3798 v9.CheckDefAndScriptSuccess(lines) |
3799 | |
3800 lines =<< trim END | |
3801 sort([1, 2, 3], (a: any, b: any) => 1) | |
3802 END | |
3803 v9.CheckDefAndScriptSuccess(lines) | |
3804 enddef | |
3805 | |
3806 def Test_sort_compare_func_fails() | |
3799 v9.CheckDefAndScriptFailure(['sort("a")'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E1211: List required for argument 1']) | 3807 v9.CheckDefAndScriptFailure(['sort("a")'], ['E1013: Argument 1: type mismatch, expected list<any> but got string', 'E1211: List required for argument 1']) |
3800 v9.CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict<any> but got list<number>', 'E1206: Dictionary required for argument 3']) | 3808 v9.CheckDefAndScriptFailure(['sort([1], "", [1])'], ['E1013: Argument 3: type mismatch, expected dict<any> but got list<number>', 'E1206: Dictionary required for argument 3']) |
3801 enddef | 3809 |
3802 | |
3803 def Test_sort_compare_func_fails() | |
3804 var lines =<< trim END | 3810 var lines =<< trim END |
3805 vim9script | 3811 vim9script |
3806 echo ['a', 'b', 'c']->sort((a: number, b: number) => 0) | 3812 echo ['a', 'b', 'c']->sort((a: number, b: number) => 0) |
3807 END | 3813 END |
3808 writefile(lines, 'Xbadsort') | 3814 writefile(lines, 'Xbadsort') |
3809 assert_fails('source Xbadsort', ['E1013:', 'E702:']) | 3815 assert_fails('source Xbadsort', ['E1013:', 'E702:']) |
3810 | |
3811 delete('Xbadsort') | 3816 delete('Xbadsort') |
3817 | |
3818 lines =<< trim END | |
3819 var l = [1, 2, 3] | |
3820 sort(l, (a: string, b: number) => 1) | |
3821 END | |
3822 v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(string, number): number', 'E1013: Argument 1: type mismatch, expected string but got number']) | |
3823 | |
3824 lines =<< trim END | |
3825 var l = ['a', 'b', 'c'] | |
3826 sort(l, (a: string, b: number) => 1) | |
3827 END | |
3828 v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?string, ?string): number but got func(string, number): number', 'E1013: Argument 2: type mismatch, expected number but got string']) | |
3829 | |
3830 lines =<< trim END | |
3831 sort([1, 2, 3], (a: number, b: number) => true) | |
3832 END | |
3833 v9.CheckDefAndScriptFailure(lines, ['E1013: Argument 2: type mismatch, expected func(?number, ?number): number but got func(number, number): bool', 'E1138: Using a Bool as a Number']) | |
3812 enddef | 3834 enddef |
3813 | 3835 |
3814 def Test_spellbadword() | 3836 def Test_spellbadword() |
3815 v9.CheckDefAndScriptFailure(['spellbadword(100)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1']) | 3837 v9.CheckDefAndScriptFailure(['spellbadword(100)'], ['E1013: Argument 1: type mismatch, expected string but got number', 'E1174: String required for argument 1']) |
3816 spellbadword('good')->assert_equal(['', '']) | 3838 spellbadword('good')->assert_equal(['', '']) |