tests/testthat/test-step.R

describe("make_steps_impl", {
  it("should allow adding steps", {
    steps <- make_steps_impl(x = function() {})
    expected <- list(x = function() {})
    expect_equal(steps, expected)

    steps <- make_steps_impl(
      x = function() {},
      y = function() {},
    )
    expected <- list2(
      x = function() {},
      y = function() {},
    )
    expect_equal(steps, expected)
  })

  it("should error if the same name is used twice", {
    expect_error({
      make_steps_impl(
        x = function() {},
        x = function() {},
      )
    })

    expect_error({
      make_steps_impl(
        x = function() {},
        x = function(x) {},
      )
    })
  })

  it("should error if values are not functions", {
    expect_error({
      make_steps_impl(x = "abc")
    }, regexp = "Steps must be functions")

    expect_error({
      make_steps_impl(x = 1L)
    }, regexp = "Steps must be functions")

    expect_error({
      make_steps_impl(x = 1.0)
    }, regexp = "Steps must be functions")

    expect_error({
      make_steps_impl(x = TRUE)
    }, regexp = "Steps must be functions")

    expect_error({
      make_steps_impl(x = NULL)
    }, regexp = "Steps must be functions")
  })

  it("should error if values are missing", {
    expect_error({
      make_steps_impl(x = )
    })

    expect_error({
      make_steps_impl(x = missing_arg())
    })
  })

  it("should error if formals have default args", {
    error_message <- "Steps should not have default arguments"
    expect_error({
      make_steps_impl(x = function(x = 10) {})
    }, regexp = error_message)

    expect_error({
      make_steps_impl(x = function(x, y = 10) {})
    }, regexp = error_message)

    expect_error({
      make_steps_impl(x = function(x, y = 10) {})
    }, regexp = error_message)
  })
})

describe("update_steps_impl", {
  it("should return old is new is empty", {
    old <- make_steps_impl(x = function() {})
    new <- list()
    steps <- update_steps_impl(old, new)
    expected <- old
    expect_equal(steps, expected)
  })

  it("should be able to redefine an existing steps", {
    old <- make_steps_impl(
      x = function() {}
    )
    new <- list(
      x = function(x) {}
    )
    steps <- update_steps_impl(old, new)
    expected <- make_steps_impl(x = function(x) {})
    expect_equal(steps, expected)

    old <- make_steps_impl(
      x = function() {}
    )
    new <- list(
      x = function(x) x
    )
    steps <- update_steps_impl(old, new)
    expected <- make_steps_impl(x = function(x) x)
    expect_equal(steps, expected)

    old <- make_steps_impl(
      x = function() {}
    )
    new <- list(
      x = identity
    )
    steps <- update_steps_impl(old, new)
    expected <- make_steps_impl(x = identity)
    expect_equal(steps, expected)
  })

  it("should be able to add a new function", {
    old <- make_steps_impl()
    new <- list(
      x = function() {}
    )
    steps <- update_steps_impl(old, new)
    expected <- make_steps_impl(x = function() {})
    expect_equal(steps, expected)

    old <- make_steps_impl(
      x = function() {}
    )
    new <- list(
      y = identity
    )
    steps <- update_steps_impl(old, new)
    expected <- make_steps_impl(x = function() {}, y = identity)
    expect_equal(steps, expected)
  })
})

describe("remove_steps_impl", {
  it("should be able to remove an existing step", {
    old <- make_steps_impl(x = function() {})
    steps <- remove_steps_impl(old, x)
    expected <- make_steps_impl()
    expect_equal(steps, expected)
  })
})
shunsambongi/planner documentation built on Aug. 19, 2022, 9:57 a.m.