Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 1 | " Test for pyx* commands and functions with Python 3. |
| 2 | |
| 3 | set pyx=3 |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 4 | CheckFeature python3 |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 5 | |
| 6 | let s:py2pattern = '^2\.[0-7]\.\d\+' |
| 7 | let s:py3pattern = '^3\.\d\+\.\d\+' |
| 8 | |
| 9 | |
| 10 | func Test_has_pythonx() |
| 11 | call assert_true(has('pythonx')) |
| 12 | endfunc |
| 13 | |
| 14 | |
| 15 | func Test_pyx() |
| 16 | redir => var |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 17 | pyx << trim EOF |
| 18 | import sys |
| 19 | print(sys.version) |
| 20 | EOF |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 21 | redir END |
| 22 | call assert_match(s:py3pattern, split(var)[0]) |
| 23 | endfunc |
| 24 | |
| 25 | |
| 26 | func 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]) |
| 31 | endfunc |
| 32 | |
| 33 | |
| 34 | func Test_pyxeval() |
| 35 | pyx import sys |
| 36 | call assert_match(s:py3pattern, split(pyxeval('sys.version'))[0]) |
| 37 | endfunc |
| 38 | |
Ben Jackson | ea19e78 | 2024-11-06 21:50:05 +0100 | [diff] [blame] | 39 | " Test for pyxeval with locals |
| 40 | func 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})) |
| 80 | endfunc |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 81 | |
| 82 | func 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 |
| 114 | endfunc |
Bram Moolenaar | 7f3a284 | 2019-05-18 15:02:25 +0200 | [diff] [blame] | 115 | |
| 116 | func Test_Catch_Exception_Message() |
| 117 | try |
| 118 | pyx raise RuntimeError( 'TEST' ) |
| 119 | catch /.*/ |
| 120 | call assert_match( '^Vim(.*):RuntimeError: TEST$', v:exception ) |
| 121 | endtry |
| 122 | endfunc |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 123 | |
| 124 | " Test for various heredoc syntaxes |
| 125 | func Test_pyx3_heredoc() |
| 126 | pyx << END |
| 127 | result='A' |
| 128 | END |
| 129 | pyx << |
| 130 | result+='B' |
| 131 | . |
| 132 | pyx << trim END |
| 133 | result+='C' |
| 134 | END |
| 135 | pyx << trim |
| 136 | result+='D' |
| 137 | . |
Bram Moolenaar | 6ab0953 | 2020-05-01 14:10:13 +0200 | [diff] [blame] | 138 | pyx << trim eof |
| 139 | result+='E' |
| 140 | eof |
zeertzjq | 449c2e5 | 2025-02-03 18:56:16 +0100 | [diff] [blame] | 141 | pyx << trimm |
| 142 | result+='F' |
| 143 | trimm |
| 144 | call assert_equal('ABCDEF', pyxeval('result')) |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 145 | endfunc |
| 146 | |
| 147 | " vim: shiftwidth=2 sts=2 expandtab |