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