Debian Codegraph @

tests/testthat/test_user_conversion.R

raw
1 test_that("we can convert between units with a user-defined function", {
2 expect_error(as_units("apple"))
3 expect_error(as_units("orange"))
4 install_unit("orange")
5 oranges <- 3 * as_units("orange")
6 expect_error(apples + oranges) # obviously
7
8 install_unit("apple", "orange / 2") # one orange is worth two apples
9 apples <- 2 * as_units("apple")
10 expect_equal(apples + oranges, (2 + 2*3) * as_units("apple"))
11 expect_equal(oranges + apples, (3 + 2/2) * as_units("orange"))
12 expect_equal(oranges + apples, set_units(apples + oranges, units(oranges), mode = "standard"))
13 expect_equal(apples + oranges, set_units(apples + oranges, units(apples), mode = "standard"))
14
15 # now just checking that we get different results with a different fruit
16 expect_error(as_units("banana"))
17 expect_error(apples + bananas) # obviously
18 expect_error(bananas + apples) # obviously
19
20 install_unit("banana", "3 apple") # one apple is worth three bananas
21 bananas <- 6 * as_units("banana")
22 expect_equal(bananas + 3 * apples, (6 + 3 * 2 / 3) * as_units("banana"))
23
24 # check dimensionless
25 install_unit("person", "1")
26 persons <- set_units(3, person)
27 expect_true(persons == set_units(3, 1))
28
29 # restore
30 remove_unit(c("orange", "apple", "banana", "person"))
31 })
32
33 test_that("we can simplify via user-defined units", {
34 install_unit("orange")
35 install_unit("apple", "orange / 2") # one orange is worth two apples
36 apples <- 4 * as_units("apple")
37 oranges <- 2 * as_units("orange")
38 expect_equal(apples / oranges, set_units(1))
39 expect_equal(oranges / apples, set_units(1))
40
41 # restore
42 remove_unit(c("orange", "apple"))
43 })
44
45 test_that("removing units works", {
46 expect_silent(remove_unit("foo"))
47 expect_silent(install_unit("foo"))
48 expect_error(install_unit("foo"))
49 expect_silent(remove_unit("foo"))
50 })