File: test-python-run.R

package info (click to toggle)
r-cran-reticulate 1.41.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 5,154; python: 620; sh: 13; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 980 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
context("run")

test_that("Python code can be run as strings", {

  # runs code in main module
  result <- py_run_string("x = 1")
  expect_equal(result$x, 1L)

  main <- import_main(convert = TRUE)
  expect_equal(main$x, 1L)

  # runs code in local dictionary
  result <- py_run_string("x = 42", local = TRUE)
  expect_true(result$x == 42L)
  expect_true(main$x == 1L)

})



test_that("Python files can be run", {

  file <- tempfile(fileext = ".py")
  writeLines("file = __file__", file)

  out <- py_run_file(file, local = TRUE)
  expect_s3_class(out, "python.builtin.dict")
  expect_equal(file, out$file)
  expect_false("__name__" %in% names(out))


  out <- py_run_file(file, local = FALSE)
  expect_s3_class(out, "python.builtin.dict")
  expect_identical(get("pyobj", out),
                   get("pyobj", py_get_attr(import_main(), "__dict__")))
  expect_equal(file, out$file)
  expect_false("__name__" %in% names(out))

  py_run_string("del file") # cleanup after test
})