Mercurial > vim
annotate src/testdir/test_lambda.vim @ 32936:c517845bd10e v9.0.1776
patch 9.0.1776: No support for stable Python 3 ABI
Commit: https://github.com/vim/vim/commit/c13b3d1350b60b94fe87f0761ea31c0e7fb6ebf3
Author: Yee Cheng Chin <ychin.git@gmail.com>
Date: Sun Aug 20 21:18:38 2023 +0200
patch 9.0.1776: No support for stable Python 3 ABI
Problem: No support for stable Python 3 ABI
Solution: Support Python 3 stable ABI
Commits:
1) Support Python 3 stable ABI to allow mixed version interoperatbility
Vim currently supports embedding Python for use with plugins, and the
"dynamic" linking option allows the user to specify a locally installed
version of Python by setting `pythonthreedll`. However, one caveat is
that the Python 3 libs are not binary compatible across minor versions,
and mixing versions can potentially be dangerous (e.g. let's say Vim was
linked against the Python 3.10 SDK, but the user sets `pythonthreedll`
to a 3.11 lib). Usually, nothing bad happens, but in theory this could
lead to crashes, memory corruption, and other unpredictable behaviors.
It's also difficult for the user to tell something is wrong because Vim
has no way of reporting what Python 3 version Vim was linked with.
For Vim installed via a package manager, this usually isn't an issue
because all the dependencies would already be figured out. For prebuilt
Vim binaries like MacVim (my motivation for working on this), AppImage,
and Win32 installer this could potentially be an issue as usually a
single binary is distributed. This is more tricky when a new Python
version is released, as there's a chicken-and-egg issue with deciding
what Python version to build against and hard to keep in sync when a new
Python version just drops and we have a mix of users of different Python
versions, and a user just blindly upgrading to a new Python could lead to
bad interactions with Vim.
Python 3 does have a solution for this problem: stable ABI / limited API
(see https://docs.python.org/3/c-api/stable.html). The C SDK limits the
API to a set of functions that are promised to be stable across
versions. This pull request adds an ifdef config that allows us to turn
it on when building Vim. Vim binaries built with this option should be
safe to freely link with any Python 3 libraies without having the
constraint of having to use the same minor version.
Note: Python 2 has no such concept and this doesn't change how Python 2
integration works (not that there is going to be a new version of Python
2 that would cause compatibility issues in the future anyway).
---
Technical details:
======
The stable ABI can be accessed when we compile with the Python 3 limited
API (by defining `Py_LIMITED_API`). The Python 3 code (in `if_python3.c`
and `if_py_both.h`) would now handle this and switch to limited API
mode. Without it set, Vim will still use the full API as before so this
is an opt-in change.
The main difference is that `PyType_Object` is now an opaque struct that
we can't directly create "static types" out of, and we have to create
type objects as "heap types" instead. This is because the struct is not
stable and changes from version to version (e.g. 3.8 added a
`tp_vectorcall` field to it). I had to change all the types to be
allocated on the heap instead with just a pointer to them.
Other functions are also simply missing in limited API, or they are
introduced too late (e.g. `PyUnicode_AsUTF8AndSize` in 3.10) to it that
we need some other ways to do the same thing, so I had to abstract a few
things into macros, and sometimes re-implement functions like
`PyObject_NEW`.
One caveat is that in limited API, `OutputType` (used for replacing
`sys.stdout`) no longer inherits from `PyStdPrinter_Type` which I don't
think has any real issue other than minor differences in how they
convert to a string and missing a couple functions like `mode()` and
`fileno()`.
Also fixed an existing bug where `tp_basicsize` was set incorrectly for
`BufferObject`, `TabListObject, `WinListObject`.
Technically, there could be a small performance drop, there is a little
more indirection with accessing type objects, and some APIs like
`PyUnicode_AsUTF8AndSize` are missing, but in practice I didn't see any
difference, and any well-written Python plugin should try to avoid
excessing callbacks to the `vim` module in Python anyway.
I only tested limited API mode down to Python 3.7, which seemes to
compile and work fine. I haven't tried earlier Python versions.
2) Fix PyIter_Check on older Python vers / type##Ptr unused warning
For PyIter_Check, older versions exposed them as either macros (used in
full API), or a function (for use in limited API). A previous change
exposed PyIter_Check to the dynamic build because Python just moved it
to function-only in 3.10 anyway. Because of that, just make sure we
always grab the function in dynamic builds in earlier versions since
that's what Python eventually did anyway.
3) Move Py_LIMITED_API define to configure script
Can now use --with-python-stable-abi flag to customize what stable ABI
version to target. Can also use an env var to do so as well.
4) Show +python/dyn-stable in :version, and allow has() feature query
Not sure if the "/dyn-stable" suffix would break things, or whether we
should do it another way. Or just don't show it in version and rely on
has() feature checking.
5) Documentation first draft. Still need to implement v:python3_version
6) Fix PyIter_Check build breaks when compiling against Python 3.8
7) Add CI coverage stable ABI on Linux/Windows / make configurable on Windows
This adds configurable options for Windows make files (both MinGW and
MSVC). CI will also now exercise both traditional full API and stable
ABI for Linux and Windows in the matrix for coverage.
Also added a "dynamic" option to Linux matrix as a drive-by change to
make other scripting languages like Ruby / Perl testable under both
static and dynamic builds.
8) Fix inaccuracy in Windows docs
Python's own docs are confusing but you don't actually want to use
`python3.dll` for the dynamic linkage.
9) Add generated autoconf file
10) Add v:python3_version support
This variable indicates the version of Python3 that Vim was built
against (PY_VERSION_HEX), and will be useful to check whether the Python
library you are loading in dynamically actually fits it. When built with
stable ABI, it will be the limited ABI version instead
(`Py_LIMITED_API`), which indicates the minimum version of Python 3 the
user should have, rather than the exact match. When stable ABI is used,
we won't be exposing PY_VERSION_HEX in this var because it just doesn't
seem necessary to do so (the whole point of stable ABI is the promise
that it will work across versions), and I don't want to confuse the user
with too many variables.
Also, cleaned up some documentation, and added help tags.
11) Fix Python 3.7 compat issues
Fix a couple issues when using limited API < 3.8
- Crash on exit: In Python 3.7, if a heap-allocated type is destroyed
before all instances are, it would cause a crash later. This happens
when we destroyed `OptionsType` before calling `Py_Finalize` when
using the limited API. To make it worse, later versions changed the
semantics and now each instance has a strong reference to its own type
and the recommendation has changed to have each instance de-ref its
own type and have its type in GC traversal. To avoid dealing with
these cross-version variations, we just don't free the heap type. They
are static types in non-limited-API anyway and are designed to last
through the entirety of the app, and we also don't restart the Python
runtime and therefore do not need it to have absolutely 0 leaks.
See:
- https://docs.python.org/3/whatsnew/3.8.html#changes-in-the-c-api
- https://docs.python.org/3/whatsnew/3.9.html#changes-in-the-c-api
- PyIter_Check: This function is not provided in limited APIs older than
3.8. Previously I was trying to mock it out using manual
PyType_GetSlot() but it was brittle and also does not actually work
properly for static types (it will generate a Python error). Just
return false. It does mean using limited API < 3.8 is not recommended
as you lose the functionality to handle iterators, but from playing
with plugins I couldn't find it to be an issue.
- Fix loading of PyIter_Check so it will be done when limited API < 3.8.
Otherwise loading a 3.7 Python lib will fail even if limited API was
specified to use it.
12) Make sure to only load `PyUnicode_AsUTF8AndSize` in needed in limited API
We don't use this function unless limited API >= 3.10, but we were
loading it regardless. Usually it's ok in Unix-like systems where Python
just has a single lib that we load from, but in Windows where there is a
separate python3.dll this would not work as the symbol would not have
been exposed in this more limited DLL file. This makes it much clearer
under what condition is this function needed.
closes: #12032
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yee Cheng Chin <ychin.git@gmail.com>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 20 Aug 2023 21:30:04 +0200 |
parents | cbfbf0e17cd0 |
children |
rev | line source |
---|---|
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
1 " Test for lambda and closure |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
2 |
21765
08940efa6b4e
patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents:
19932
diff
changeset
|
3 source check.vim |
28515
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
4 import './vim9.vim' as v9 |
21765
08940efa6b4e
patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents:
19932
diff
changeset
|
5 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
6 func Test_lambda_feature() |
9690
4a7a88404076
commit https://github.com/vim/vim/commit/9532fe7fbe1b14531931e83bd9f8054efdcf7509
Christian Brabandt <cb@256bit.org>
parents:
9688
diff
changeset
|
7 call assert_equal(1, has('lambda')) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
8 endfunc |
9690
4a7a88404076
commit https://github.com/vim/vim/commit/9532fe7fbe1b14531931e83bd9f8054efdcf7509
Christian Brabandt <cb@256bit.org>
parents:
9688
diff
changeset
|
9 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
10 func Test_lambda_with_filter() |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
11 let s:x = 2 |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
12 call assert_equal([2, 3], filter([1, 2, 3], {i, v -> v >= s:x})) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
13 endfunc |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
14 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
15 func Test_lambda_with_map() |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
16 let s:x = 1 |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
17 call assert_equal([2, 3, 4], map([1, 2, 3], {i, v -> v + s:x})) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
18 endfunc |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
19 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
20 func Test_lambda_with_sort() |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
21 call assert_equal([1, 2, 3, 4, 7], sort([3,7,2,1,4], {a, b -> a - b})) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
22 endfunc |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
23 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
24 func Test_lambda_with_timer() |
21765
08940efa6b4e
patch 8.2.1432: various inconsistencies in test files
Bram Moolenaar <Bram@vim.org>
parents:
19932
diff
changeset
|
25 CheckFeature timers |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
26 |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
27 let s:n = 0 |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
28 let s:timer_id = 0 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
29 func! s:Foo() |
17480
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
30 let s:timer_id = timer_start(10, {-> execute("let s:n += 1 | echo s:n", "")}, {"repeat": -1}) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
31 endfunc |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
32 |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
33 call s:Foo() |
17480
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
34 " check timer works |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
35 for i in range(0, 10) |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
36 if s:n > 0 |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
37 break |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
38 endif |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
39 sleep 10m |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
40 endfor |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
41 |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
42 " do not collect lambda |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
43 call test_garbagecollect_now() |
17480
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
44 |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
45 " check timer still works |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
46 let m = s:n |
17480
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
47 for i in range(0, 10) |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
48 if s:n > m |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
49 break |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
50 endif |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
51 sleep 10m |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
52 endfor |
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
53 |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
54 call timer_stop(s:timer_id) |
17480
91cac682b09a
patch 8.1.1738: testing lambda with timer is slow
Bram Moolenaar <Bram@vim.org>
parents:
15456
diff
changeset
|
55 call assert_true(s:n > m) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
56 endfunc |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
57 |
28515
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
58 func Test_lambda_vim9cmd_linebreak() |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
59 CheckFeature timers |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
60 |
28520
2748a023cc37
patch 8.2.4784: lamba test with timer is flaky
Bram Moolenaar <Bram@vim.org>
parents:
28515
diff
changeset
|
61 let g:test_is_flaky = 1 |
28515
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
62 let lines =<< trim END |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
63 vim9cmd call timer_start(10, (x) => { |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
64 # comment |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
65 g:result = 'done' |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
66 }) |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
67 END |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
68 call v9.CheckScriptSuccess(lines) |
28520
2748a023cc37
patch 8.2.4784: lamba test with timer is flaky
Bram Moolenaar <Bram@vim.org>
parents:
28515
diff
changeset
|
69 " sleep longer on a retry |
2748a023cc37
patch 8.2.4784: lamba test with timer is flaky
Bram Moolenaar <Bram@vim.org>
parents:
28515
diff
changeset
|
70 exe 'sleep ' .. [20, 100, 500, 500, 500][g:run_nr] .. 'm' |
28515
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
71 call assert_equal('done', g:result) |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
72 unlet g:result |
28745
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
73 |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
74 let lines =<< trim END |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
75 g:result = [0]->map((_, v) => |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
76 1 # inline comment |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
77 + |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
78 2 |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
79 ) |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
80 assert_equal([3], g:result) |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
81 END |
67f005ab37d7
patch 8.2.4897: comment inside an expression in lambda ignores the rest
Bram Moolenaar <Bram@vim.org>
parents:
28520
diff
changeset
|
82 call v9.CheckDefAndScriptSuccess(lines) |
28515
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
83 endfunc |
c6aadb2c4cd7
patch 8.2.4782: accessing freed memory
Bram Moolenaar <Bram@vim.org>
parents:
28499
diff
changeset
|
84 |
30930
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
85 def Test_lamba_compiled_linebreak() |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
86 var lines =<< trim END |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
87 vim9script |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
88 |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
89 def Echo(what: any) |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
90 assert_equal('hello world', what) |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
91 enddef |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
92 def That() |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
93 printf("hello ") |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
94 ->((x) => x .. "world")() |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
95 ->Echo() |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
96 enddef |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
97 That() |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
98 END |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
99 v9.CheckScriptSuccess(lines) |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
100 enddef |
cbfbf0e17cd0
patch 9.0.0799: in compiled function ->() on next line not recognized
Bram Moolenaar <Bram@vim.org>
parents:
30580
diff
changeset
|
101 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
102 func Test_lambda_with_partial() |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
103 let l:Cb = function({... -> ['zero', a:1, a:2, a:3]}, ['one', 'two']) |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
104 call assert_equal(['zero', 'one', 'two', 'three'], l:Cb('three')) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
105 endfunc |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
106 |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
107 function Test_lambda_fails() |
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
108 call assert_equal(3, {a, b -> a + b}(1, 2)) |
15456
f01eb1aed348
patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents:
15406
diff
changeset
|
109 call assert_fails('echo {a, a -> a + a}(1, 2)', 'E853:') |
19826
293a22b677a8
patch 8.2.0469: Vim9: no error for missing ] after list
Bram Moolenaar <Bram@vim.org>
parents:
19724
diff
changeset
|
110 call assert_fails('echo {a, b -> a + b)}(1, 2)', 'E451:') |
19724
b3e93a05c3ca
patch 8.2.0418: code in eval.c not sufficiently covered by tests
Bram Moolenaar <Bram@vim.org>
parents:
19249
diff
changeset
|
111 echo assert_fails('echo 10->{a -> a + 2}', 'E107:') |
26636
6047c7ea7963
patch 8.2.3847: illegal memory access when using a lambda with an error
Bram Moolenaar <Bram@vim.org>
parents:
25118
diff
changeset
|
112 |
6047c7ea7963
patch 8.2.3847: illegal memory access when using a lambda with an error
Bram Moolenaar <Bram@vim.org>
parents:
25118
diff
changeset
|
113 call assert_fails('eval 0->(', "E110: Missing ')'") |
28499
a35d54d01ade
patch 8.2.4774: crash when using a number for lambda name
Bram Moolenaar <Bram@vim.org>
parents:
26636
diff
changeset
|
114 call assert_fails('eval 0->(3)()', "E1275:") |
a35d54d01ade
patch 8.2.4774: crash when using a number for lambda name
Bram Moolenaar <Bram@vim.org>
parents:
26636
diff
changeset
|
115 call assert_fails('eval 0->([3])()', "E1275:") |
a35d54d01ade
patch 8.2.4774: crash when using a number for lambda name
Bram Moolenaar <Bram@vim.org>
parents:
26636
diff
changeset
|
116 call assert_fails('eval 0->({"a": 3})()', "E1275:") |
a35d54d01ade
patch 8.2.4774: crash when using a number for lambda name
Bram Moolenaar <Bram@vim.org>
parents:
26636
diff
changeset
|
117 call assert_fails('eval 0->(xxx)()', "E121:") |
9527
e8b3db8e2d30
commit https://github.com/vim/vim/commit/069c1e7fa9f45a665064f7f2c17da84d6a48f544
Christian Brabandt <cb@256bit.org>
parents:
diff
changeset
|
118 endfunc |
9597
3ca0fd9709b1
commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents:
9527
diff
changeset
|
119 |
3ca0fd9709b1
commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents:
9527
diff
changeset
|
120 func Test_not_lamda() |
3ca0fd9709b1
commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents:
9527
diff
changeset
|
121 let x = {'>' : 'foo'} |
3ca0fd9709b1
commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents:
9527
diff
changeset
|
122 call assert_equal('foo', x['>']) |
3ca0fd9709b1
commit https://github.com/vim/vim/commit/4f0383bc3fe5af0229fb66b53fe94329af783eff
Christian Brabandt <cb@256bit.org>
parents:
9527
diff
changeset
|
123 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
124 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
125 func Test_lambda_capture_by_reference() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
126 let v = 1 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
127 let l:F = {x -> x + v} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
128 let v = 2 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
129 call assert_equal(12, l:F(10)) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
130 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
131 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
132 func Test_lambda_side_effect() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
133 func! s:update_and_return(arr) |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
134 let a:arr[1] = 5 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
135 return a:arr |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
136 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
137 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
138 func! s:foo(arr) |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
139 return {-> s:update_and_return(a:arr)} |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
140 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
141 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
142 let arr = [3,2,1] |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
143 call assert_equal([3, 5, 1], s:foo(arr)()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
144 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
145 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
146 func Test_lambda_refer_local_variable_from_other_scope() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
147 func! s:foo(X) |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
148 return a:X() " refer l:x in s:bar() |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
149 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
150 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
151 func! s:bar() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
152 let x = 123 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
153 return s:foo({-> x}) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
154 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
155 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
156 call assert_equal(123, s:bar()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
157 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
158 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
159 func Test_lambda_do_not_share_local_variable() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
160 func! s:define_funcs() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
161 let l:One = {-> split(execute("let a = 'abc' | echo a"))[0]} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
162 let l:Two = {-> exists("a") ? a : "no"} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
163 return [l:One, l:Two] |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
164 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
165 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
166 let l:F = s:define_funcs() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
167 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
168 call assert_equal('no', l:F[1]()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
169 call assert_equal('abc', l:F[0]()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
170 call assert_equal('no', l:F[1]()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
171 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
172 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
173 func Test_lambda_closure_counter() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
174 func! s:foo() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
175 let x = 0 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
176 return {-> [execute("let x += 1"), x][-1]} |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
177 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
178 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
179 let l:F = s:foo() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
180 call test_garbagecollect_now() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
181 call assert_equal(1, l:F()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
182 call assert_equal(2, l:F()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
183 call assert_equal(3, l:F()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
184 call assert_equal(4, l:F()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
185 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
186 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
187 func Test_lambda_with_a_var() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
188 func! s:foo() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
189 let x = 2 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
190 return {... -> a:000 + [x]} |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
191 endfunc |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
192 func! s:bar() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
193 return s:foo()(1) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
194 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
195 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
196 call assert_equal([1, 2], s:bar()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
197 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
198 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
199 func Test_lambda_call_lambda_from_lambda() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
200 func! s:foo(x) |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
201 let l:F1 = {-> {-> a:x}} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
202 return {-> l:F1()} |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
203 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
204 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
205 let l:F = s:foo(1) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
206 call assert_equal(1, l:F()()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
207 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
208 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
209 func Test_lambda_delfunc() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
210 func! s:gen() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
211 let pl = l: |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
212 let l:Foo = {-> get(pl, "Foo", get(pl, "Bar", {-> 0}))} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
213 let l:Bar = l:Foo |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
214 delfunction l:Foo |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
215 return l:Bar |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
216 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
217 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
218 let l:F = s:gen() |
9723
80ac9cf77c9b
commit https://github.com/vim/vim/commit/437bafe4c8a83ed71ee006eda7f54b65a90f0d4c
Christian Brabandt <cb@256bit.org>
parents:
9721
diff
changeset
|
219 call assert_fails(':call l:F()', 'E933:') |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
220 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
221 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
222 func Test_lambda_scope() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
223 func! s:NewCounter() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
224 let c = 0 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
225 return {-> [execute('let c += 1'), c][-1]} |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
226 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
227 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
228 func! s:NewCounter2() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
229 return {-> [execute('let c += 100'), c][-1]} |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
230 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
231 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
232 let l:C = s:NewCounter() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
233 let l:D = s:NewCounter2() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
234 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
235 call assert_equal(1, l:C()) |
15456
f01eb1aed348
patch 8.1.0736: code for Blob not sufficiently tested
Bram Moolenaar <Bram@vim.org>
parents:
15406
diff
changeset
|
236 call assert_fails(':call l:D()', 'E121:') |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
237 call assert_equal(2, l:C()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
238 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
239 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
240 func Test_lambda_share_scope() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
241 func! s:New() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
242 let c = 0 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
243 let l:Inc0 = {-> [execute('let c += 1'), c][-1]} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
244 let l:Dec0 = {-> [execute('let c -= 1'), c][-1]} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
245 return [l:Inc0, l:Dec0] |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
246 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
247 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
248 let [l:Inc, l:Dec] = s:New() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
249 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
250 call assert_equal(1, l:Inc()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
251 call assert_equal(2, l:Inc()) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
252 call assert_equal(1, l:Dec()) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
253 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
254 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
255 func Test_lambda_circular_reference() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
256 func! s:Foo() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
257 let d = {} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
258 let d.f = {-> d} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
259 return d.f |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
260 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
261 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
262 call s:Foo() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
263 call test_garbagecollect_now() |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
264 let i = 0 | while i < 10000 | call s:Foo() | let i+= 1 | endwhile |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
265 call test_garbagecollect_now() |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
266 endfunc |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
267 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
268 func Test_lambda_combination() |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
269 call assert_equal(2, {x -> {x -> x}}(1)(2)) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
270 call assert_equal(10, {y -> {x -> x(y)(10)}({y -> y})}({z -> z})) |
30325
58fb880f3607
patch 9.0.0498: various small issues
Bram Moolenaar <Bram@vim.org>
parents:
28745
diff
changeset
|
271 call assert_equal(5.0, {x -> {y -> x / y}}(10)(2.0)) |
9686
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
272 call assert_equal(6, {x -> {y -> {z -> x + y + z}}}(1)(2)(3)) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
273 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
274 call assert_equal(6, {x -> {f -> f(x)}}(3)({x -> x * 2})) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
275 call assert_equal(6, {f -> {x -> f(x)}}({x -> x * 2})(3)) |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
276 |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
277 " Z combinator |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
278 let Z = {f -> {x -> f({y -> x(x)(y)})}({x -> f({y -> x(x)(y)})})} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
279 let Fact = {f -> {x -> x == 0 ? 1 : x * f(x - 1)}} |
8c2553beff0f
commit https://github.com/vim/vim/commit/1e96d9bf98f9ab84d5af7f98d6a961d91b17364f
Christian Brabandt <cb@256bit.org>
parents:
9597
diff
changeset
|
280 call assert_equal(120, Z(Fact)(5)) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
281 endfunc |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
282 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
283 func Test_closure_counter() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
284 func! s:foo() |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
285 let x = 0 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
286 func! s:bar() closure |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
287 let x += 1 |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
288 return x |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
289 endfunc |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
290 return function('s:bar') |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
291 endfunc |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
292 |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
293 let l:F = s:foo() |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
294 call test_garbagecollect_now() |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
295 call assert_equal(1, l:F()) |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
296 call assert_equal(2, l:F()) |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
297 call assert_equal(3, l:F()) |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
298 call assert_equal(4, l:F()) |
23108
34a74f5f0fb4
patch 8.2.2100: insufficient testing for function range and dict
Bram Moolenaar <Bram@vim.org>
parents:
22087
diff
changeset
|
299 |
34a74f5f0fb4
patch 8.2.2100: insufficient testing for function range and dict
Bram Moolenaar <Bram@vim.org>
parents:
22087
diff
changeset
|
300 call assert_match("^\n function <SNR>\\d\\+_bar() closure" |
34a74f5f0fb4
patch 8.2.2100: insufficient testing for function range and dict
Bram Moolenaar <Bram@vim.org>
parents:
22087
diff
changeset
|
301 \ .. "\n1 let x += 1" |
34a74f5f0fb4
patch 8.2.2100: insufficient testing for function range and dict
Bram Moolenaar <Bram@vim.org>
parents:
22087
diff
changeset
|
302 \ .. "\n2 return x" |
34a74f5f0fb4
patch 8.2.2100: insufficient testing for function range and dict
Bram Moolenaar <Bram@vim.org>
parents:
22087
diff
changeset
|
303 \ .. "\n endfunction$", execute('func s:bar')) |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
304 endfunc |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
305 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
306 func Test_closure_unlet() |
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
307 func! s:foo() |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
308 let x = 1 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
309 func! s:bar() closure |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
310 unlet x |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
311 endfunc |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
312 call s:bar() |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
313 return l: |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
314 endfunc |
9688
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
315 |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
316 call assert_false(has_key(s:foo(), 'x')) |
2ea935bdd1a1
commit https://github.com/vim/vim/commit/10ce39a0d52272a3dfff2feb8c631529f29e6740
Christian Brabandt <cb@256bit.org>
parents:
9686
diff
changeset
|
317 call test_garbagecollect_now() |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
318 endfunc |
9721
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
319 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
320 func LambdaFoo() |
9721
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
321 let x = 0 |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
322 func! LambdaBar() closure |
9721
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
323 let x += 1 |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
324 return x |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
325 endfunc |
9721
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
326 return function('LambdaBar') |
15406
63b02fcf1361
patch 8.1.0711: test files still use function!
Bram Moolenaar <Bram@vim.org>
parents:
14002
diff
changeset
|
327 endfunc |
9721
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
328 |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
329 func Test_closure_refcount() |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
330 let g:Count = LambdaFoo() |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
331 call test_garbagecollect_now() |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
332 call assert_equal(1, g:Count()) |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
333 let g:Count2 = LambdaFoo() |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
334 call test_garbagecollect_now() |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
335 call assert_equal(1, g:Count2()) |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
336 call assert_equal(2, g:Count()) |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
337 call assert_equal(3, g:Count2()) |
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
338 |
9727
8436bb5134f5
commit https://github.com/vim/vim/commit/0588d4f9d2741f35a271400a37fddbdd72d84219
Christian Brabandt <cb@256bit.org>
parents:
9723
diff
changeset
|
339 delfunc LambdaFoo |
8436bb5134f5
commit https://github.com/vim/vim/commit/0588d4f9d2741f35a271400a37fddbdd72d84219
Christian Brabandt <cb@256bit.org>
parents:
9723
diff
changeset
|
340 delfunc LambdaBar |
9721
79862f44c647
commit https://github.com/vim/vim/commit/580164481924ed8611eb79f0247a0eb1ca0b3b9a
Christian Brabandt <cb@256bit.org>
parents:
9690
diff
changeset
|
341 endfunc |
9735
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
342 |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
343 func Test_named_function_closure() |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
344 func! Afoo() |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
345 let x = 14 |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
346 func! s:Abar() closure |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
347 return x |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
348 endfunc |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
349 call assert_equal(14, s:Abar()) |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
350 endfunc |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
351 call Afoo() |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
352 call assert_equal(14, s:Abar()) |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
353 call test_garbagecollect_now() |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
354 call assert_equal(14, s:Abar()) |
8037eb704e93
commit https://github.com/vim/vim/commit/bc7ce675b2d1c9fb58c067eff3edd59abc30aba4
Christian Brabandt <cb@256bit.org>
parents:
9727
diff
changeset
|
355 endfunc |
14002
ade79e3e3b88
patch 8.1.0019: error when defining a Lambda with index of a function result
Christian Brabandt <cb@256bit.org>
parents:
9735
diff
changeset
|
356 |
ade79e3e3b88
patch 8.1.0019: error when defining a Lambda with index of a function result
Christian Brabandt <cb@256bit.org>
parents:
9735
diff
changeset
|
357 func Test_lambda_with_index() |
ade79e3e3b88
patch 8.1.0019: error when defining a Lambda with index of a function result
Christian Brabandt <cb@256bit.org>
parents:
9735
diff
changeset
|
358 let List = {x -> [x]} |
ade79e3e3b88
patch 8.1.0019: error when defining a Lambda with index of a function result
Christian Brabandt <cb@256bit.org>
parents:
9735
diff
changeset
|
359 let Extract = {-> function(List, ['foobar'])()[0]} |
ade79e3e3b88
patch 8.1.0019: error when defining a Lambda with index of a function result
Christian Brabandt <cb@256bit.org>
parents:
9735
diff
changeset
|
360 call assert_equal('foobar', Extract()) |
ade79e3e3b88
patch 8.1.0019: error when defining a Lambda with index of a function result
Christian Brabandt <cb@256bit.org>
parents:
9735
diff
changeset
|
361 endfunc |
18851
3cf9529b3a4a
patch 8.1.2412: crash when evaluating expression with error
Bram Moolenaar <Bram@vim.org>
parents:
17480
diff
changeset
|
362 |
3cf9529b3a4a
patch 8.1.2412: crash when evaluating expression with error
Bram Moolenaar <Bram@vim.org>
parents:
17480
diff
changeset
|
363 func Test_lambda_error() |
3cf9529b3a4a
patch 8.1.2412: crash when evaluating expression with error
Bram Moolenaar <Bram@vim.org>
parents:
17480
diff
changeset
|
364 " This was causing a crash |
22087
ff21e2962490
patch 8.2.1593: tests do not check the error number properly
Bram Moolenaar <Bram@vim.org>
parents:
21765
diff
changeset
|
365 call assert_fails('ec{@{->{d->()()', 'E15:') |
18851
3cf9529b3a4a
patch 8.1.2412: crash when evaluating expression with error
Bram Moolenaar <Bram@vim.org>
parents:
17480
diff
changeset
|
366 endfunc |
19724
b3e93a05c3ca
patch 8.2.0418: code in eval.c not sufficiently covered by tests
Bram Moolenaar <Bram@vim.org>
parents:
19249
diff
changeset
|
367 |
19932
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
368 func Test_closure_error() |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
369 let l =<< trim END |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
370 func F1() closure |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
371 return 1 |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
372 endfunc |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
373 END |
30580
f08ed0738f7a
patch 9.0.0625: too many delete() calls in tests
Bram Moolenaar <Bram@vim.org>
parents:
30325
diff
changeset
|
374 call writefile(l, 'Xscript', 'D') |
19932
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
375 let caught_932 = 0 |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
376 try |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
377 source Xscript |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
378 catch /E932:/ |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
379 let caught_932 = 1 |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
380 endtry |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
381 call assert_equal(1, caught_932) |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
382 endfunc |
2c4d9ca33769
patch 8.2.0522: several errors are not tested for
Bram Moolenaar <Bram@vim.org>
parents:
19826
diff
changeset
|
383 |
19724
b3e93a05c3ca
patch 8.2.0418: code in eval.c not sufficiently covered by tests
Bram Moolenaar <Bram@vim.org>
parents:
19249
diff
changeset
|
384 " vim: shiftwidth=2 sts=2 expandtab |