Skip to content

Commit

Permalink
add tests for how parsing should work
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Feb 14, 2025
1 parent 27c4106 commit 3e2bbe3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/testthat/test_unit_creation.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,35 @@ test_that("set_units default enforces NSE", {
# is it bad if this works?
# expect_error(set_units(1:3, "m/s"))
})

expect_symbolic <- function(u, n, d)
expect_equal(units(as_units(u)), units:::.symbolic_units(n, d))

test_that("exotic units work", {
# some_string should work if the following works:
# units:::R_ut_parse(some_string)
# to see what's actually parsed:
# units:::R_ut_format(units:::R_ut_parse(some_string))

expect_symbolic("2.2 m s", c("2.2", "m", "s"), character(0))
expect_symbolic("2.2*m*s", c("2.2", "m", "s"), character(0))
expect_symbolic("2.2.m.s", c("2.2", "m", "s"), character(0))

expect_symbolic("m2/s", c("m", "m"), "s")
expect_symbolic("m^2/s", c("m", "m"), "s")
expect_symbolic("m 2/s", c("m", "2"), "s")
expect_symbolic("m-2/s", character(0), c("m", "m", "s"))
expect_symbolic("m^-2/s", character(0), c("m", "m", "s"))
expect_symbolic("m -2/s", c("m", "-2"), "s")
expect_symbolic("m/s2", "m", c("s", "s"))
expect_symbolic("m/s^2", "m", c("s", "s"))
expect_symbolic("m/s 2", c("m", "2"), "s")
expect_symbolic("m/s-2", c("m", "s", "s"), character(0))
expect_symbolic("m/s^-2", c("m", "s", "s"), character(0))
expect_symbolic("m/s -2", c("m", "-2"), "s")

expect_symbolic("ml/min/1.73m^2", c("ml", "m", "m"), c("min", "1.73"))
expect_symbolic("ml/min/(1.73m^2)", "ml", c("min", "1.73", "m", "m"))
expect_symbolic("ml/min/1.73/m^2", "ml", c("min", "1.73", "m", "m"))
expect_symbolic("ml/min/1.73m-2", "ml", c("min", "1.73", "m", "m"))
})

0 comments on commit 3e2bbe3

Please sign in to comment.