blob: 781bb417881091fca91289ba3c5a75359287530d [file] [log] [blame]
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01001" Test for pyx* commands and functions with Python 2.
2
3set pyx=2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02004source check.vim
5CheckFeature python
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01006
7let s:py2pattern = '^2\.[0-7]\.\d\+'
8let s:py3pattern = '^3\.\d\+\.\d\+'
9
10
11func Test_has_pythonx()
12 call assert_true(has('pythonx'))
13endfunc
14
15
16func Test_pyx()
17 redir => var
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +020018 pyx << trim EOF
19 import sys
20 print(sys.version)
21 EOF
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +010022 redir END
23 call assert_match(s:py2pattern, split(var)[0])
24endfunc
25
26
27func Test_pyxdo()
28 pyx import sys
29 enew
30 pyxdo return sys.version.split("\n")[0]
31 call assert_match(s:py2pattern, split(getline('.'))[0])
32endfunc
33
34
35func Test_pyxeval()
36 pyx import sys
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020037 call assert_match(s:py2pattern, split('sys.version'->pyxeval())[0])
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +010038endfunc
39
40
Ben Jacksonea19e782024-11-06 21:50:05 +010041" Test for pyxeval with locals
42func Test_python_pyeval_locals()
43 let str = 'a string'
44 let num = 0xbadb33f
45 let d = {'a': 1, 'b': 2, 'c': str}
46 let l = [ str, num, d ]
47
48 let locals = #{
49 \ s: str,
50 \ n: num,
51 \ d: d,
52 \ l: l,
53 \ }
54
55 " check basics
56 call assert_equal('a string', pyxeval('s', locals))
57 call assert_equal(0xbadb33f, pyxeval('n', locals))
58 call assert_equal(d, pyxeval('d', locals))
59 call assert_equal(l, pyxeval('l', locals))
60
61 py << trim EOF
62 def __UpdateDict(d, upd):
63 d.update(upd)
64 return d
65
66 def __ExtendList(l, *args):
67 l.extend(*args)
68 return l
69 EOF
70
71 " check assign to dict member works like bindeval
72 call assert_equal(3, pyxeval('__UpdateDict( d, {"c": 3} )["c"]', locals))
73 call assert_equal(3, d['c'])
74
75 " check append lo list
76 call assert_equal(4, pyxeval('len(__ExtendList(l, ["new item"]))', locals))
77 call assert_equal("new item", l[-1])
78
79 " check calling a function
80 let StrLen = function('strlen')
81 call assert_equal(3, pyxeval('f("abc")', {'f': StrLen}))
82endfunc
83
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +010084func Test_pyxfile()
85 " No special comments nor shebangs
86 redir => var
87 pyxfile pyxfile/pyx.py
88 redir END
89 call assert_match(s:py2pattern, split(var)[0])
90
91 " Python 2 special comment
92 redir => var
93 pyxfile pyxfile/py2_magic.py
94 redir END
95 call assert_match(s:py2pattern, split(var)[0])
96
97 " Python 2 shebang
98 redir => var
99 pyxfile pyxfile/py2_shebang.py
100 redir END
101 call assert_match(s:py2pattern, split(var)[0])
102
103 if has('python3')
104 " Python 3 special comment
105 redir => var
106 pyxfile pyxfile/py3_magic.py
107 redir END
108 call assert_match(s:py3pattern, split(var)[0])
109
110 " Python 3 shebang
111 redir => var
112 pyxfile pyxfile/py3_shebang.py
113 redir END
114 call assert_match(s:py3pattern, split(var)[0])
115 endif
116endfunc
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200117
118func Test_Catch_Exception_Message()
119 try
120 pyx raise RuntimeError( 'TEST' )
121 catch /.*/
122 call assert_match( '^Vim(.*):RuntimeError: TEST$', v:exception )
123 endtry
124endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200125
126" Test for various heredoc syntaxes
127func Test_pyx2_heredoc()
128 pyx << END
129result='A'
130END
131 pyx <<
132result+='B'
133.
134 pyx << trim END
135 result+='C'
136 END
137 pyx << trim
138 result+='D'
139 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200140 pyx << trim eof
141 result+='E'
142 eof
143 call assert_equal('ABCDE', pyxeval('result'))
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200144endfunc
145
146" vim: shiftwidth=2 sts=2 expandtab