Skip to content

Commit

Permalink
amend unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chlebowa committed Dec 19, 2024
1 parent 45a6f7d commit b63e9a1
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 14 deletions.
3 changes: 0 additions & 3 deletions tests/testthat/test-all_choices.R

This file was deleted.

16 changes: 14 additions & 2 deletions tests/testthat/test-choices_selected.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,22 @@ testthat::test_that("choices_selected throws error when selected is not found in
)
})

testthat::test_that("all_choices is the same as selecting all choices", {
testthat::test_that("delayed_choices selects the desired choices", {
testthat::expect_equal(
choices_selected(choices = letters, selected = letters),
choices_selected(choices = letters, selected = all_choices())
choices_selected(choices = letters, selected = delayed_choices())
)
testthat::expect_equal(
choices_selected(choices = letters, selected = letters),
choices_selected(choices = letters, selected = delayed_choices("all"))
)
testthat::expect_equal(
choices_selected(choices = letters, selected = letters[1]),
choices_selected(choices = letters, selected = delayed_choices("first"))
)
testthat::expect_equal(
choices_selected(choices = letters, selected = letters[length(letters)]),
choices_selected(choices = letters, selected = delayed_choices("last"))
)
})

Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test-delayed_choices.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
testthat::test_that("delayed_choices constructor runs without error", {
testthat::expect_no_error(delayed_choices())
testthat::expect_no_error(delayed_choices("all"))
testthat::expect_no_error(delayed_choices("first"))
testthat::expect_no_error(delayed_choices("last"))
testthat::expect_error(
delayed_choices("wrong"),
"should be one of "
)
})

testthat::test_that("delayed_choices constructor returns funciton", {
testthat::expect(is.function(delayed_choices()), "expecting function")
testthat::expect(is.function(delayed_choices("all")), "expecting function")
testthat::expect(is.function(delayed_choices("first")), "expecting function")
testthat::expect(is.function(delayed_choices("last")), "expecting function")
})

testthat::test_that("delayed_choices functions return appropriate subsets of atomic input data", {
all_choices <- delayed_choices()
testthat::expect_identical(all_choices(letters), letters)
all_choices <- delayed_choices("all")
testthat::expect_identical(all_choices(letters), letters)
first_choice <- delayed_choices("first")
testthat::expect_identical(first_choice(letters), letters[1L])
last_choice <- delayed_choices("last")
testthat::expect_identical(last_choice(letters), letters[length(letters)])
})

testthat::test_that("delayed_choices funcitons return passed NULL", {
all_choices <- delayed_choices()
testthat::expect_null(all_choices(NULL))
all_choices <- delayed_choices("all")
testthat::expect_null(all_choices(NULL))
first_choice <- delayed_choices("first")
testthat::expect_null(first_choice(NULL))
last_choice <- delayed_choices("last")
testthat::expect_null(last_choice(NULL))
})

testthat::test_that("delayed_choices funcitons return passed empty vector", {
all_choices <- delayed_choices()
testthat::expect_identical(all_choices(character(0L)), character(0L))
all_choices <- delayed_choices("all")
testthat::expect_identical(all_choices(character(0L)), character(0L))
first_choice <- delayed_choices("first")
testthat::expect_identical(first_choice(character(0L)), character(0L))
last_choice <- delayed_choices("last")
testthat::expect_identical(last_choice(character(0L)), character(0L))
})
19 changes: 15 additions & 4 deletions tests/testthat/test-filter_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,25 @@ testthat::test_that("delayed version of filter_spec", {
)
})

testthat::test_that("all_choices passed to selected identical to all choices", {
testthat::test_that("delayed_choices passed to selected selects desired choices", {
testthat::expect_equal(
filter_spec(vars = "test", choices = c(1, 2), selected = c(1, 2)),
filter_spec(vars = "test", choices = c(1, 2), selected = all_choices())
filter_spec(vars = "test", choices = 1:3, selected = 1:3),
filter_spec(vars = "test", choices = 1:3, selected = delayed_choices())
)
testthat::expect_equal(
filter_spec(vars = "test", choices = 1:3, selected = 1:3),
filter_spec(vars = "test", choices = 1:3, selected = delayed_choices("all"))
)
testthat::expect_equal(
filter_spec(vars = "test", choices = 1:3, selected = 1L),
filter_spec(vars = "test", choices = 1:3, selected = delayed_choices("first"))
)
testthat::expect_equal(
filter_spec(vars = "test", choices = 1:3, selected = 3L),
filter_spec(vars = "test", choices = 1:3, selected = delayed_choices("last"))
)
})


# With resolve_delayed
testthat::test_that("delayed filter_spec - resolve_delayed", {
expected_spec <- filter_spec(
Expand Down
22 changes: 17 additions & 5 deletions tests/testthat/test-select_spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,28 @@ testthat::test_that("delayed version of select_spec", {
testthat::expect_equal(res_obj, exp_obj)
})

testthat::test_that("all_choices passed to selected is the same as passing all choices", {
testthat::test_that("delayed_choices passed to selected selects desired choices", {
testthat::expect_equal(
select_spec(choices = letters, selected = letters),
select_spec(choices = letters, selected = all_choices())
select_spec(choices = letters, selected = delayed_choices())
)
testthat::expect_equal(
select_spec(choices = letters, selected = letters),
select_spec(choices = letters, selected = delayed_choices("all"))
)
testthat::expect_equal(
select_spec(choices = letters, selected = letters[1]),
select_spec(choices = letters, selected = delayed_choices("first"))
)
testthat::expect_equal(
select_spec(choices = letters, selected = letters[1]),
select_spec(choices = letters, selected = delayed_choices("first"))
)
})

testthat::test_that("multiple is set to TRUE if all_choices is passed to selected", {
testthat::expect_true(select_spec(choices = variable_choices("test"), selected = all_choices())$multiple)
testthat::expect_true(select_spec(choices = variable_choices(iris), selected = all_choices())$multiple)
testthat::test_that("multiple is set to TRUE if delayed_choices(\"all\") is passed to selected", {
testthat::expect_true(select_spec(choices = variable_choices("test"), selected = delayed_choices())$multiple)
testthat::expect_true(select_spec(choices = variable_choices(iris), selected = delayed_choices())$multiple)
})

testthat::test_that("default values", {
Expand Down

0 comments on commit b63e9a1

Please sign in to comment.