File: test-python-classes.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 (41 lines) | stat: -rw-r--r-- 977 bytes parent folder | download
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
41
context("classes")

test_that("Python class variables are accessible via $", {
  skip_if_no_python()
  expect_equal(test$PythonClass$FOO, 1)
})

test_that("Python class members can be called via $", {
  skip_if_no_python()
  expect_equal(test$PythonClass$class_method(), 1)
})


test_that("py_has_method()", {
  # in python2, unbound methods are still methods
  # in python3, only bound, user-defined, functions are methods
  skip_if(py_version() < "3")
  Fraction <- import("fractions")$Fraction

  expect_false(py_has_method(Fraction, "conjugate"))
  expect_true(py_has_method(Fraction(), "conjugate"))
})


if(getRversion() >= "4.3.0")
test_that("nameOfClass()", {

  numpy <- import("numpy")
  x <- r_to_py(array(1:3))
  expect_true(inherits(x, numpy$ndarray))

  io <- import("io")
  builtins <- import_builtins()

  with(builtins$open(tempfile(), "wb") %as% f, {
    expect_true(inherits(f, io$BufferedWriter))
    expect_false(f$closed)
  })
  expect_true(f$closed)

})