blob: 40b359afab0c44b474339600dbf7a9ccf6e03384 [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 +02004CheckFeature python
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:py2pattern, 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:py2pattern, split(getline('.'))[0])
31endfunc
32
33
34func Test_pyxeval()
35 pyx import sys
Bram Moolenaar3f4f3d82019-09-04 20:05:59 +020036 call assert_match(s:py2pattern, split('sys.version'->pyxeval())[0])
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +010037endfunc
38
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 py << 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
82
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +010083func Test_pyxfile()
84 " No special comments nor shebangs
85 redir => var
86 pyxfile pyxfile/pyx.py
87 redir END
88 call assert_match(s:py2pattern, split(var)[0])
89
90 " Python 2 special comment
91 redir => var
92 pyxfile pyxfile/py2_magic.py
93 redir END
94 call assert_match(s:py2pattern, split(var)[0])
95
96 " Python 2 shebang
97 redir => var
98 pyxfile pyxfile/py2_shebang.py
99 redir END
100 call assert_match(s:py2pattern, split(var)[0])
101
102 if has('python3')
103 " Python 3 special comment
104 redir => var
105 pyxfile pyxfile/py3_magic.py
106 redir END
107 call assert_match(s:py3pattern, split(var)[0])
108
109 " Python 3 shebang
110 redir => var
111 pyxfile pyxfile/py3_shebang.py
112 redir END
113 call assert_match(s:py3pattern, 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_pyx2_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
zeertzjq449c2e52025-02-03 18:56:16 +0100142 pyx << trimm
143result+='F'
144trimm
145 call assert_equal('ABCDEF', pyxeval('result'))
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200146endfunc
147
148" vim: shiftwidth=2 sts=2 expandtab