Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for percent #137

Open
wants to merge 4 commits into
base: add_percent_func
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions tests/testthat/test-percent_james.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
test_that("as_percent works correctly", {
expect_s3_class(as_percent(0.5), "percent")
expect_type(as_percent(0.5), "double")

expect_equal(as.character(as_percent(50)), "5,000%")
expect_equal(as.character(as_percent(5)), "500%")
expect_equal(as.character(as_percent(0.5)), "50%")
expect_equal(as.character(as_percent(0.05)), "5%")
expect_equal(as.character(as_percent(0.005)), "0.5%")
expect_equal(as.character(as_percent(0.0005)), "0.05%")
expect_equal(as.character(as_percent(0.00005)), "0.01%")
Moohan marked this conversation as resolved.
Show resolved Hide resolved

expect_equal(format(as_percent(50)), "5,000%")
expect_equal(format(as_percent(5)), "500%")
expect_equal(format(as_percent(0.5)), "50%")
expect_equal(format(as_percent(0.05)), "5%")
expect_equal(format(as_percent(0.005)), "0.5%")
expect_equal(format(as_percent(0.0005)), "0.05%")
expect_equal(format(as_percent(0.00005)), "0.01%")
})

test_that("as_percent handles non-numeric input", {
expect_error(as_percent("not a number"), regexp = "must be a <numeric> vector, not a <character> vector")
expect_error(as_percent(TRUE), regexp = "must be a <numeric> vector, not a <logical> vector")
expect_error(as_percent(list(1, 2, 3)), regexp = "must be a <numeric> vector, not a <list> vector")
})

test_that("round_half_up handles non-numeric input", {
expect_error(round_half_up("not a number"), regexp = "must be a <numeric> vector, not a <character> vector")
expect_error(round_half_up(TRUE), regexp = "must be a <numeric> vector, not a <logical> vector")
expect_error(round_half_up(list(1, 2, 3)), regexp = "must be a <numeric> vector, not a <list> vector")
})


test_that("signif_half_up handles non-numeric input", {
expect_error(signif_half_up("not a number"), regexp = "must be a <numeric> vector, not a <character> vector")
expect_error(signif_half_up(TRUE), regexp = "must be a <numeric> vector, not a <logical> vector")
expect_error(signif_half_up(list(1, 2, 3)), regexp = "must be a <numeric> vector, not a <list> vector")
})


test_that("signif_half_up results are as expected", {
# scalars
expect_equal(
signif_half_up(x = 12.5, digits = 2),
13
)
expect_equal(
signif_half_up(x = 0),
0
)
expect_equal(signif_half_up(x = -2.5, digits = 1), -3)
expect_equal(
signif_half_up(x = 123.45, digits = 4),
123.5
)
expect_equal(signif_half_up(x = -123.45, digits = 4), -123.5)
# vectors
expect_equal(
signif_half_up(
x = c(12.5, 0, -2.5, 123.45, -123.45),
digits = 2
),
c(13, 0, -2.5, 120, -120)
)
})

test_that("signif_half_up works correctly", {
expect_equal(signif_half_up(0.555, 2), 0.56)
expect_equal(signif_half_up(0.555, 1), 0.6)
expect_equal(signif_half_up(12345, 3), 12300)
})

test_that("as.character.percent works correctly", {
p <- as_percent(0.1234)
expect_equal(as.character(p), "12.34%")
expect_equal(as.character(p, digits = 1), "12.3%")
})

test_that("format.percent works correctly", {
p <- as_percent(0.1234)
expect_equal(format(p), "12.34%")
expect_equal(format(p, digits = 1), "12.3%")
expect_equal(format(p, symbol = " (%)"), "12.34 (%)")
})

test_that("print.percent works correctly", {
expect_output(print(as_percent(0.1234)), "12.34%")
expect_output(print(as_percent(0.567890)), "56.79%")
})

test_that("subsetting percent objects works correctly", {
p <- as_percent(c(0.1, 0.2, 0.3))
expect_s3_class(p[1:2], "percent")
expect_equal(p[1:2], as_percent(c(0.1, 0.2)))
})

test_that("unique.percent works correctly", {
p <- as_percent(c(0.1, 0.2, 0.1, 0.3))
expect_s3_class(unique(p), "percent")
expect_equal(unique(p), as_percent(c(0.1, 0.2, 0.3)))
})

test_that("rep.percent works correctly", {
p <- as_percent(0.1)
expect_s3_class(rep(p, 3), "percent")
expect_equal(rep(p, 3), as_percent(c(0.1, 0.1, 0.1)))
})

test_that("Math.percent works correctly", {
p <- as_percent(c(0.1234, 0.5678))
expect_equal(floor(p), as_percent(c(0.12, 0.56)))
expect_equal(ceiling(p), as_percent(c(0.13, 0.57)))
expect_equal(trunc(p), as_percent(c(0.12, 0.56)))
expect_equal(round(p), as_percent(c(0.12, 0.57)))
expect_equal(signif(p, 2), as_percent(c(0.12, 0.57)))
})

test_that("Summary.percent works correctly", {
p <- as_percent(c(0.1, 0.2, 0.3))
expect_s3_class(sum(p), "percent")
expect_equal(sum(p), as_percent(0.6))
expect_equal(prod(p), as_percent(0.006))
expect_equal(min(p), as_percent(0.1))
expect_equal(max(p), as_percent(0.3))
expect_equal(range(p), as_percent(c(0.1, 0.3)))
})

test_that("mean.percent works correctly", {
p <- as_percent(c(0.1, 0.2, 0.3))
expect_s3_class(mean(p), "percent")
expect_equal(mean(p), as_percent(0.2))
})