Skip to content

Commit

Permalink
Use testthat 3 (#274)
Browse files Browse the repository at this point in the history
* Use testthat 3

* Update generated files

* Remove context() calls

* expect_is()

* Avoid warning

* Expect warning

* expect_equivalent()
  • Loading branch information
krlmlr authored Mar 16, 2021
1 parent 70bcb76 commit dcbe2dd
Show file tree
Hide file tree
Showing 14 changed files with 12 additions and 37 deletions.
2 changes: 1 addition & 1 deletion R/make_units.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ as_units.call <- function(x, check_is_valid = TRUE, ...) {
identical(x, 1) || identical(x, 1L))
return(.as.units(1, unitless))

if (is.vector(x) && any(is.na(x)))
if (is.vector(x) && !is.expression(x) && any(is.na(x)))
stop("a missing value for units is not allowed")

stopifnot(is.language(x))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_arith.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Arithmetic")

test_that("we can compare vectors with equal units", {
x <- 1:4 * as_units("m")
y <- 1:4 * as_units("m")
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test_conversion.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Unit conversion")

test_that("we can convert numbers to unit-less units", {
x <- as_units(1:4)
expect_equal(length(x), 4)
Expand Down Expand Up @@ -201,7 +199,7 @@ test_that("errors are correctly coerced to a data frame", {
test_that("units are correctly coerced to a list", {
x <- 1:10 * as_units("m")
y <- as.list(x)
expect_is(y, "list")
expect_type(y, "list")
expect_true(all(sapply(seq_along(y), function(i) all.equal(y[[i]], x[i]))))
})

Expand All @@ -212,7 +210,7 @@ test_that("NA as units generate warnings", {

test_that("ud_are_convertible return the expected value", {
x <- 1:10 * as_units("m")
expect_is(ud_are_convertible("m", "km"), "logical")
expect_type(ud_are_convertible("m", "km"), "logical")
expect_true(ud_are_convertible("m", "km"))
expect_true(ud_are_convertible(units(x), "km"))
expect_false(ud_are_convertible("s", "kg"))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_helpers.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Helpers")

test_that("keep_units restores units", {
x <- set_units(1:5, m)

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_math.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Math functions")

test_that("we can call math functions on units", {
x <- 1:4 - 2.1
ux <- x * as_units("m")
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Misc. utility functions")

test_that("We can replace parts if they have an equivalent unit", {
x <- 1:4 * as_units("km")
y <- c(3000, 4000) * as_units("m")
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test_mixed.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Mixed Unit tests")

test_that("mixed units work", {
(m = c(set_units(1:3, km), set_units(4:6, g), allow_mixed = TRUE))

Expand Down Expand Up @@ -29,16 +27,18 @@ test_that("mixed units work", {
expect_s3_class(m[1:3] / mixed_units(set_units(1, mm)), "mixed_units")
expect_s3_class(m[1:3] + mixed_units(set_units(1, mm)), "mixed_units")
expect_s3_class(m[1:3] - mixed_units(set_units(1, mm)), "mixed_units")
expect_is(m[1:3] == mixed_units(set_units(1, mm)), "logical")
expect_is(m[1:3] != mixed_units(set_units(1, mm)), "logical")
expect_type(m[1:3] == mixed_units(set_units(1, mm)), "logical")
expect_type(m[1:3] != mixed_units(set_units(1, mm)), "logical")
expect_error(m[1:3] ^ mixed_units(set_units(1, mm)))

# this breaks -- seems to be an s3 limitation:
expect_error(m[1:3] * set_units(1, mm))
# FIXME: Ops.mixed_units and Ops.units must be the same method
# to avoid the warning and the error.
# We can discriminate by switchpatching.
expect_error(expect_warning(m[1:3] * set_units(1, mm)))

expect_s3_class(units(m), "mixed_symbolic_units")
expect_is(format(m), "character")
expect_is(as.character(units(m)), "character")
expect_type(format(m), "character")
expect_type(as.character(units(m)), "character")
print(m)
expect_equal(drop_units(m), sapply(m, as.numeric))

Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test_summaries.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Unit summaries")

test_that("we can compute summary functions on units", {
x <- 1:4
ux <- x * as_units("m")
Expand Down Expand Up @@ -35,7 +33,7 @@ test_that("we can compute means and medians and quantiles", {

expect_equal(as.numeric(mean(ux)), mean(x))
expect_equal(as.numeric(median(ux)), median(x))
expect_equivalent(as.numeric(quantile(ux)), quantile(x))
expect_equal(as.numeric(quantile(ux)), quantile(x), ignore_attr = TRUE)
expect_equal(as.numeric(weighted.mean(ux, w)), weighted.mean(x, w))
expect_equal(as.numeric(weighted.mean(ux, uw)), weighted.mean(x, w))
})
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_symbolic_units.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Symbolic unit manipulation")

test_that("we can make symbolic units", {
m <- units:::.symbolic_units("m")
expect_true(inherits(m, "symbolic_units"))
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_tidyverse.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("tidyverse")

test_that("pillar methods are available for units objects", {
skip_if_not_installed("pillar")

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_time.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("Date/time")

test_that("we can convert from udunits time/date to R", {
#skip("set_units doesn't accept strings")
# rewrite this to
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_udunits.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("udunits2")

test_that("udunits error messages", {
expect_error(set_units(1:3, "qqq"), "qqq")
})
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test_unit_creation.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
context("unit(s) creation")



test_that("parse_units() backticks strings correctly", {

Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test_user_conversion.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
context("User-defined unit conversion")

test_that("we can convert between units with a user-defined function", {
expect_error(as_units("apple"))
expect_error(as_units("orange"))
Expand Down

0 comments on commit dcbe2dd

Please sign in to comment.