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