blob: b34fdcbd7e72317dcd7dc45f8dc5c5b4a84ed4be [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 +02004source check.vim
5CheckFeature python3
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:py3pattern, 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:py3pattern, split(getline('.'))[0])
32endfunc
33
34
35func Test_pyxeval()
36 pyx import sys
37 call assert_match(s:py3pattern, split(pyxeval('sys.version'))[0])
38endfunc
39
Ben Jacksonea19e782024-11-06 21:50:05 +010040" Test for pyxeval with locals
41func Test_python_pyeval_locals()
42 let str = 'a string'
43 let num = 0xbadb33f
44 let d = {'a': 1, 'b': 2, 'c': str}
45 let l = [ str, num, d ]
46
47 let locals = #{
48 \ s: str,
49 \ n: num,
50 \ d: d,
51 \ l: l,
52 \ }
53
54 " check basics
55 call assert_equal('a string', pyxeval('s', locals))
56 call assert_equal(0xbadb33f, pyxeval('n', locals))
57 call assert_equal(d, pyxeval('d', locals))
58 call assert_equal(l, pyxeval('l', locals))
59
60 py3 << trim EOF
61 def __UpdateDict(d, upd):
62 d.update(upd)
63 return d
64
65 def __ExtendList(l, *args):
66 l.extend(*args)
67 return l
68 EOF
69
70 " check assign to dict member works like bindeval
71 call assert_equal(3, pyxeval('__UpdateDict( d, {"c": 3} )["c"]', locals))
72 call assert_equal(3, d['c'])
73
74 " check append lo list
75 call assert_equal(4, pyxeval('len(__ExtendList(l, ["new item"]))', locals))
76 call assert_equal("new item", l[-1])
77
78 " check calling a function
79 let StrLen = function('strlen')
80 call assert_equal(3, pyxeval('f("abc")', {'f': StrLen}))
81endfunc
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +010082
83func Test_pyxfile()
84 " No special comments nor shebangs
85 redir => var
86 pyxfile pyxfile/pyx.py
87 redir END
88 call assert_match(s:py3pattern, split(var)[0])
89
90 " Python 3 special comment
91 redir => var
92 pyxfile pyxfile/py3_magic.py
93 redir END
94 call assert_match(s:py3pattern, split(var)[0])
95
96 " Python 3 shebang
97 redir => var
98 pyxfile pyxfile/py3_shebang.py
99 redir END
100 call assert_match(s:py3pattern, split(var)[0])
101
102 if has('python')
103 " Python 2 special comment
104 redir => var
105 pyxfile pyxfile/py2_magic.py
106 redir END
107 call assert_match(s:py2pattern, split(var)[0])
108
109 " Python 2 shebang
110 redir => var
111 pyxfile pyxfile/py2_shebang.py
112 redir END
113 call assert_match(s:py2pattern, split(var)[0])
114 endif
115endfunc
Bram Moolenaar7f3a2842019-05-18 15:02:25 +0200116
117func Test_Catch_Exception_Message()
118 try
119 pyx raise RuntimeError( 'TEST' )
120 catch /.*/
121 call assert_match( '^Vim(.*):RuntimeError: TEST$', v:exception )
122 endtry
123endfunc
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200124
125" Test for various heredoc syntaxes
126func Test_pyx3_heredoc()
127 pyx << END
128result='A'
129END
130 pyx <<
131result+='B'
132.
133 pyx << trim END
134 result+='C'
135 END
136 pyx << trim
137 result+='D'
138 .
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200139 pyx << trim eof
140 result+='E'
141 eof
142 call assert_equal('ABCDE', pyxeval('result'))
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200143endfunc
144
145" vim: shiftwidth=2 sts=2 expandtab