tests/testthat/test-idispatch.R

testfn = function(x,...) {
  interfacer::idispatch(x,
    testfn.diamonds = i_diamonds,
    testfn.iris = i_iris
  )
}

testfn.diamonds = function(x, ...) {
  sprintf("%d rows of diamonds",nrow(x))
}

testfn.iris = function(x, ...) {
  sprintf("%d rows of iris",nrow(x))
}


test_that("dispatch works", {
  
  expect_equal(
    testfn(ggplot2::diamonds),
    "53940 rows of diamonds"
  )
  
  expect_equal(
    testfn(iris),
    "150 rows of iris"
  )
  
  expect_error(
    testfn(mtcars)
  )
  
})

Try the interfacer package in your browser

Any scripts or data that you put into this service are public.

interfacer documentation built on April 4, 2025, 6:13 a.m.