| 1 | use strict; |
|---|
| 2 | use warnings; |
|---|
| 3 | use Test::Base; |
|---|
| 4 | use lib qw/./; |
|---|
| 5 | use PyVM; |
|---|
| 6 | |
|---|
| 7 | use XXX -dumper; |
|---|
| 8 | |
|---|
| 9 | plan tests => 1*blocks; |
|---|
| 10 | |
|---|
| 11 | run { |
|---|
| 12 | my $block = shift; |
|---|
| 13 | |
|---|
| 14 | my $out = ''; |
|---|
| 15 | tie_output *STDOUT, $out; |
|---|
| 16 | PyVM::run_it( $block->input ); |
|---|
| 17 | untie *STDOUT; |
|---|
| 18 | is $out, $block->expected, $block->name; |
|---|
| 19 | }; |
|---|
| 20 | |
|---|
| 21 | # TODO: |
|---|
| 22 | # [1,2,3,5][:-1] etc. |
|---|
| 23 | # keyword args. |
|---|
| 24 | |
|---|
| 25 | __END__ |
|---|
| 26 | |
|---|
| 27 | === simple |
|---|
| 28 | --- input |
|---|
| 29 | print 3+2 |
|---|
| 30 | --- expected |
|---|
| 31 | 5 |
|---|
| 32 | |
|---|
| 33 | === string add |
|---|
| 34 | --- input |
|---|
| 35 | print 'Boo'+'fy' |
|---|
| 36 | --- expected |
|---|
| 37 | Boofy |
|---|
| 38 | |
|---|
| 39 | === add,mul |
|---|
| 40 | --- input |
|---|
| 41 | print (3*2)+4 |
|---|
| 42 | --- expected |
|---|
| 43 | 10 |
|---|
| 44 | |
|---|
| 45 | === binary operators |
|---|
| 46 | --- input |
|---|
| 47 | print (3+2)-4*3.14/2 |
|---|
| 48 | --- expected |
|---|
| 49 | -1.28 |
|---|
| 50 | |
|---|
| 51 | === binary power |
|---|
| 52 | --- input |
|---|
| 53 | print 3**4 |
|---|
| 54 | --- expected |
|---|
| 55 | 81 |
|---|
| 56 | |
|---|
| 57 | === binary floor divide |
|---|
| 58 | --- input |
|---|
| 59 | print 25//3 |
|---|
| 60 | --- expected |
|---|
| 61 | 8 |
|---|
| 62 | |
|---|
| 63 | === binary modulo |
|---|
| 64 | --- input |
|---|
| 65 | print 25%3 |
|---|
| 66 | --- expected |
|---|
| 67 | 1 |
|---|
| 68 | |
|---|
| 69 | === tuple |
|---|
| 70 | --- input |
|---|
| 71 | (1,2,3) |
|---|
| 72 | --- expected |
|---|
| 73 | |
|---|
| 74 | === list |
|---|
| 75 | --- input |
|---|
| 76 | [1,2,3] |
|---|
| 77 | --- expected |
|---|
| 78 | |
|---|
| 79 | === variable |
|---|
| 80 | --- input |
|---|
| 81 | x = 'kent' |
|---|
| 82 | print x |
|---|
| 83 | --- expected |
|---|
| 84 | kent |
|---|
| 85 | |
|---|
| 86 | === list one |
|---|
| 87 | --- input |
|---|
| 88 | x = [1,2,3] |
|---|
| 89 | print x[1] |
|---|
| 90 | --- expected |
|---|
| 91 | 2 |
|---|
| 92 | |
|---|
| 93 | === inplace modulo |
|---|
| 94 | --- input |
|---|
| 95 | x = 58 |
|---|
| 96 | x %= 7 |
|---|
| 97 | print x |
|---|
| 98 | --- expected |
|---|
| 99 | 2 |
|---|
| 100 | |
|---|
| 101 | === binary lshift |
|---|
| 102 | --- input |
|---|
| 103 | print 1 << 3 |
|---|
| 104 | --- expected |
|---|
| 105 | 8 |
|---|
| 106 | |
|---|
| 107 | === binary rshift |
|---|
| 108 | --- input |
|---|
| 109 | print 25352 >> 3 |
|---|
| 110 | --- expected |
|---|
| 111 | 3169 |
|---|
| 112 | |
|---|
| 113 | === binary and |
|---|
| 114 | --- input |
|---|
| 115 | print 25352 & 522 |
|---|
| 116 | --- expected |
|---|
| 117 | 520 |
|---|
| 118 | |
|---|
| 119 | === binary or |
|---|
| 120 | --- input |
|---|
| 121 | print 25352 | 522 |
|---|
| 122 | --- expected |
|---|
| 123 | 25354 |
|---|
| 124 | |
|---|
| 125 | === binary xor |
|---|
| 126 | --- input |
|---|
| 127 | print 25352 ^ 522 |
|---|
| 128 | --- expected |
|---|
| 129 | 24834 |
|---|
| 130 | |
|---|
| 131 | === unary not |
|---|
| 132 | --- input |
|---|
| 133 | print not 25453 |
|---|
| 134 | --- expected |
|---|
| 135 | 0 |
|---|
| 136 | |
|---|
| 137 | === unary_INVERT |
|---|
| 138 | --- input |
|---|
| 139 | print ~25453 |
|---|
| 140 | --- expected |
|---|
| 141 | -25454 |
|---|
| 142 | |
|---|
| 143 | === if |
|---|
| 144 | --- input |
|---|
| 145 | if 3>4: |
|---|
| 146 | print 'too bad' |
|---|
| 147 | else: |
|---|
| 148 | print 'ok' |
|---|
| 149 | --- expected |
|---|
| 150 | ok |
|---|
| 151 | |
|---|
| 152 | === if |
|---|
| 153 | --- input |
|---|
| 154 | if 3<4: |
|---|
| 155 | print 'ok' |
|---|
| 156 | else: |
|---|
| 157 | print 'too bad' |
|---|
| 158 | --- expected |
|---|
| 159 | ok |
|---|
| 160 | |
|---|
| 161 | === SLICE+0 |
|---|
| 162 | --- input |
|---|
| 163 | x = [1,2,3,4,5] |
|---|
| 164 | print x[:] |
|---|
| 165 | --- expected |
|---|
| 166 | [1, 2, 3, 4, 5] |
|---|
| 167 | |
|---|
| 168 | === SLICE+1 |
|---|
| 169 | --- input |
|---|
| 170 | x = [1,2,3,4,5] |
|---|
| 171 | print x[2:] |
|---|
| 172 | --- expected |
|---|
| 173 | [3, 4, 5] |
|---|
| 174 | |
|---|
| 175 | === SLICE+1 |
|---|
| 176 | --- input |
|---|
| 177 | x = [1] |
|---|
| 178 | print x[2:] |
|---|
| 179 | --- expected |
|---|
| 180 | [] |
|---|
| 181 | |
|---|
| 182 | === SLICE+2 |
|---|
| 183 | --- input |
|---|
| 184 | x = [1,2,3,4,5] |
|---|
| 185 | print x[:2] |
|---|
| 186 | --- expected |
|---|
| 187 | [1, 2] |
|---|
| 188 | |
|---|
| 189 | === SLICE+2 |
|---|
| 190 | --- input |
|---|
| 191 | x = [1] |
|---|
| 192 | print x[:2] |
|---|
| 193 | --- expected |
|---|
| 194 | [1] |
|---|
| 195 | |
|---|
| 196 | === SLICE+3 |
|---|
| 197 | --- input |
|---|
| 198 | x = [1, 2,3,4,5,6,7,8,9,10] |
|---|
| 199 | print x[3:5] |
|---|
| 200 | --- expected |
|---|
| 201 | [4, 5] |
|---|
| 202 | |
|---|
| 203 | === WHILE |
|---|
| 204 | --- input |
|---|
| 205 | i=0 |
|---|
| 206 | while i<3: |
|---|
| 207 | print i |
|---|
| 208 | i+=1 |
|---|
| 209 | --- expected |
|---|
| 210 | 0 |
|---|
| 211 | 1 |
|---|
| 212 | 2 |
|---|
| 213 | |
|---|
| 214 | === string modulo |
|---|
| 215 | --- input |
|---|
| 216 | print "mixi: %s" % 'Boofy' |
|---|
| 217 | --- expected |
|---|
| 218 | mixi: Boofy |
|---|
| 219 | |
|---|
| 220 | === string modulo by tuple |
|---|
| 221 | --- input |
|---|
| 222 | print "mixi: %s, %s" % ('Boofy', 'bonnu') |
|---|
| 223 | --- expected |
|---|
| 224 | mixi: Boofy, bonnu |
|---|
| 225 | |
|---|
| 226 | === FOR |
|---|
| 227 | --- SKIP |
|---|
| 228 | --- input |
|---|
| 229 | for x in ['foo', 'bar', 'baz']: |
|---|
| 230 | print x |
|---|
| 231 | --- expected |
|---|
| 232 | foo |
|---|
| 233 | bar |
|---|
| 234 | baz |
|---|
| 235 | |
|---|
| 236 | === simple function2 |
|---|
| 237 | --- input |
|---|
| 238 | def foo(x, y, z): |
|---|
| 239 | print x*5+y*3 |
|---|
| 240 | |
|---|
| 241 | foo(20, 4, 52) |
|---|
| 242 | --- expected |
|---|
| 243 | 112 |
|---|
| 244 | |
|---|
| 245 | === use function with return value |
|---|
| 246 | --- input |
|---|
| 247 | def foo(x, y, z): |
|---|
| 248 | return x*5+y*2 |
|---|
| 249 | |
|---|
| 250 | print foo(20, 4, 52) |
|---|
| 251 | --- expected |
|---|
| 252 | 108 |
|---|
| 253 | |
|---|
| 254 | === use function with keyword arguments |
|---|
| 255 | --- input |
|---|
| 256 | def func(a1, a2="default a2", a3="default a3"): |
|---|
| 257 | print "a1=%s" % a1 |
|---|
| 258 | print "a2=%s" % a2 |
|---|
| 259 | print "a3=%s" % a3 |
|---|
| 260 | |
|---|
| 261 | func(a3="custom a3", a1="custom a1") |
|---|
| 262 | --- expected |
|---|
| 263 | a1=custom a1 |
|---|
| 264 | a2=default a2 |
|---|
| 265 | a3=custom a3 |
|---|
| 266 | |
|---|
| 267 | === use function with keyword arguments |
|---|
| 268 | --- ONLY |
|---|
| 269 | --- input |
|---|
| 270 | def func(a1, a2="default a2", a3="default a3"): |
|---|
| 271 | print "a1=%s" % a1 |
|---|
| 272 | print "a2=%s" % a2 |
|---|
| 273 | print "a3=%s" % a3 |
|---|
| 274 | |
|---|
| 275 | func("custom a1", 'custom a2') |
|---|
| 276 | --- expected |
|---|
| 277 | a1=custom a1 |
|---|
| 278 | a2=custom a2 |
|---|
| 279 | a3=default a3 |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | === use function with keyword arguments |
|---|
| 283 | --- ONLY |
|---|
| 284 | --- input |
|---|
| 285 | def func(a1, a2="default a2", a3="default a3"): |
|---|
| 286 | print "a1=%s" % a1 |
|---|
| 287 | print "a2=%s" % a2 |
|---|
| 288 | print "a3=%s" % a3 |
|---|
| 289 | |
|---|
| 290 | func("custom a1") |
|---|
| 291 | --- expected |
|---|
| 292 | a1=custom a1 |
|---|
| 293 | a2=default a2 |
|---|
| 294 | a3=default a3 |
|---|
| 295 | |
|---|
| 296 | === use function with keyword arguments |
|---|
| 297 | --- SKIP |
|---|
| 298 | --- input |
|---|
| 299 | def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): |
|---|
| 300 | print "-- This parrot wouldn't", action, |
|---|
| 301 | print "if you put", voltage, "Volts through it." |
|---|
| 302 | print "-- Lovely plumage, the", type |
|---|
| 303 | print "-- It's", state, "!" |
|---|
| 304 | |
|---|
| 305 | parrot(action = 'VOOOOOM', voltage = 1000000) |
|---|
| 306 | #parrot('a thousand', state = 'pushing up the daisies') |
|---|
| 307 | #parrot('a million', 'bereft of life', 'jump') |
|---|
| 308 | #parrot(1000) |
|---|
| 309 | --- expected |
|---|
| 310 | 108 |
|---|
| 311 | |
|---|
| 312 | === simple function |
|---|
| 313 | --- SKIP |
|---|
| 314 | --- input |
|---|
| 315 | def foo(x, y): |
|---|
| 316 | x*=5 |
|---|
| 317 | print x |
|---|
| 318 | |
|---|
| 319 | x = 20 |
|---|
| 320 | foo(x) |
|---|
| 321 | print x |
|---|
| 322 | --- expected |
|---|
| 323 | 100 |
|---|
| 324 | |
|---|
| 325 | === simple function |
|---|
| 326 | --- SKIP |
|---|
| 327 | --- input |
|---|
| 328 | def foo(x): |
|---|
| 329 | print x*5 |
|---|
| 330 | |
|---|
| 331 | foo(20) |
|---|
| 332 | --- expected |
|---|
| 333 | 100 |
|---|
| 334 | |
|---|
| 335 | === �Ǥդ�����-- SKIP |
|---|
| 336 | --- input |
|---|
| 337 | def foo(x=4): |
|---|
| 338 | print x*5 |
|---|
| 339 | |
|---|
| 340 | foo(20) |
|---|
| 341 | --- expected |
|---|
| 342 | 100 |
|---|