|
286 | 286 | end
|
287 | 287 |
|
288 | 288 | @testitem "code" begin
|
289 |
| - # TODO |
| 289 | + # check for ArgumentError when inputs are mixed up |
| 290 | + @test_throws ArgumentError pyeval(Main, "1+1") |
| 291 | + @test_throws ArgumentError pyeval(Main, Main) |
| 292 | + @test_throws ArgumentError pyeval("1+1", "1+1") |
| 293 | + @test_throws ArgumentError pyexec(Main, "1+1") |
| 294 | + @test_throws ArgumentError pyexec(Main, Main) |
| 295 | + @test_throws ArgumentError pyexec("1+1", "1+1") |
| 296 | + # basic code execution |
| 297 | + m = Module(:test) |
| 298 | + g = pydict() |
| 299 | + @test pyeq(Bool, pyeval("1+1", m), 2) |
| 300 | + @test pyeq(Bool, pyeval("1+1", g), 2) |
| 301 | + @test pyeq(Bool, pyeval(pystr("1+1"), g), 2) |
| 302 | + @test pyexec("1+1", m) === nothing |
| 303 | + @test pyexec("1+1", g) === nothing |
| 304 | + @test pyexec(pystr("1+1"), g) === nothing |
| 305 | + # check the globals are what we think they are |
| 306 | + @test pyis(pyeval("globals()", g), g) |
| 307 | + mg = pyeval("globals()", m) |
| 308 | + @test pyisinstance(mg, pybuiltins.dict) |
| 309 | + # these automatically gain 1 item, __builtins__ |
| 310 | + @test length(g) == 1 |
| 311 | + @test length(mg) == 1 |
| 312 | + @test pycontains(g, "__builtins__") |
| 313 | + @test pycontains(mg, "__builtins__") |
| 314 | + # code should fail when x does not exist |
| 315 | + @test_throws PyException pyeval("x+1", g) |
| 316 | + @test_throws PyException pyeval("x+1", g) |
| 317 | + # now set x and try again |
| 318 | + g["x"] = 1 |
| 319 | + @test pyeq(Bool, pyeval("x+1", g), 2) |
| 320 | + # set x using pyexec this time |
| 321 | + pyexec("x=2", g) |
| 322 | + @test pyeq(Bool, g["x"], 2) |
| 323 | + @test pyeq(Bool, pyeval("x+1", g), 3) |
| 324 | + # now use locals |
| 325 | + # check empty locals have no effect |
| 326 | + l = pydict() |
| 327 | + @test pyeq(Bool, pyeval("x+1", g, l), 3) |
| 328 | + @test pyeq(Bool, pyeval("x+1", g, Dict()), 3) |
| 329 | + # now set x locally |
| 330 | + l["x"] = 3 |
| 331 | + @test pyeq(Bool, pyeval("x+1", g, l), 4) |
| 332 | + @test pyeq(Bool, pyeval("x+1", g, Dict()), 3) |
| 333 | + @test pyeq(Bool, pyeval("x+1", g, Dict("x" => 0)), 1) |
| 334 | + @test pyeq(Bool, pyeval("x+1", g, (x=1,)), 2) |
| 335 | + # check pyexec runs in local scope |
| 336 | + pyexec("x=4", g, l) |
| 337 | + @test pyeq(Bool, g["x"], 2) |
| 338 | + @test pyeq(Bool, l["x"], 4) |
| 339 | + # check global code runs in global scope |
| 340 | + pyexec("global y; y=x+1", g, l) |
| 341 | + @test pyeq(Bool, g["y"], 5) |
| 342 | + @test !pycontains(l, "y") |
| 343 | + # check pyeval converts types correctly |
| 344 | + @test pyeval(Int, "1+1", g) === 2 |
| 345 | + @test pyeval(Nothing, "None", g) === nothing |
290 | 346 | end
|
0 commit comments