tests/testthat/test_mockable_function_wrapper.R

library(mockable)

testthat::context("Mockable function wrapper class")

testthat::test_that(
  "Initialisation with a function assigns the function",
  {
    f <- function()1
    mfw <- mockable:::MockableFunctionWrapper$new(f)
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$fn_, "function")
  }
)

testthat::test_that(
  "Initialisation without a function throws an error",
  {
    testthat::expect_error(mockable:::MockableFunctionWrapper$new())
    testthat::expect_error(mockable:::MockableFunctionWrapper$new(1))
  }
)

testthat::test_that(
  "Mocking underlying with a function assigns the function",
  {
    f <- function(a) a + 1
    mfw <- mockable:::MockableFunctionWrapper$new(f)
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$fn_, 'function')
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$orig_fn_, 'function')
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$fn_, f)
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$orig_fn_, f)
    f1 <- function(a) a + 2
    mfw$mock(f1)
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$fn_, 'function')
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$orig_fn_, 'function')
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$fn_, f1)
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$orig_fn_, f)
  }
)

testthat::test_that(
  "Mocking without a fn throws a warning and doesn't change underlying",
  {
    f <- function()1
    mfw <- mockable:::MockableFunctionWrapper$new(f)
    testthat::expect_warning(
      mfw$mock(),
      "Mock function is not a closure, mockable not mocked"
    )
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$fn_, 'function')
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$fn_, f)
    testthat::expect_warning(
      mfw$mock(1),
      "Mock function 1 is not a closure, mockable not mocked"
    )
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$fn_, 'function')
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$fn_, f)
  }
)

testthat::test_that(
  "Mocking with fn with wrong args throws a warning and doesn't change underlying",
  {
    f <- function()1
    mfw <- mockable:::MockableFunctionWrapper$new(f)
    g <- function(a) a + 1
    testthat::expect_warning(
      mfw$mock(g),
      stringr::str_c(
        'Mock function g does not have compatible argument list as original ',
        'function, mockable not mocked'
      )
    )
    testthat::expect_is((mfw$.__enclos_env__)[['private']]$fn_, 'function')
    testthat::expect_equal((mfw$.__enclos_env__)[['private']]$fn_, f)
    rm(mfw)
    mfw <- mockable:::MockableFunctionWrapper$new(g)
    h <- function(b) b + 1
    testthat::expect_warning(
      mfw$mock(h),
      stringr::str_c(
        'Mock function h does not have compatible argument list as original ',
        'function, mockable not mocked'
      )
    )


  }
)
EntirelyDS/mutablefnr documentation built on May 6, 2019, 3:48 p.m.