Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
I also tried to remove the definition of titleGrob() because of the conflict with ggplot2 but it does not seem to be exported by ggplot2.
  • Loading branch information
aphalo committed Apr 17, 2024
1 parent 836166c commit 4f57756
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: ggpp
Type: Package
Title: Grammar Extensions to 'ggplot2'
Version: 0.5.6.9001
Date: 2024-03-04
Date: 2024-04-18
Authors@R:
c(
person("Pedro J.", "Aphalo", email = "pedro.aphalo@helsinki.fi", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3385-972X")),
Expand Down
Binary file modified tests/testthat/Rplots.pdf
Binary file not shown.
39 changes: 39 additions & 0 deletions tests/testthat/test-compute_npc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
context("compute_npc")

test_that("character input value returns correct numeric value", {
expect_equal(compute_npc("right"), 0.95)
expect_equal(compute_npc("left"), 0.05)
expect_equal(compute_npc(c("left", "right")), c(0.05, 0.95))
expect_equal(compute_npc("centre"), 0.5)
expect_equal(compute_npc("center"), 0.5)
expect_equal(compute_npc("middle"), 0.5)
expect_equal(compute_npc("bottom"), 0.05)
expect_equal(compute_npc("top"), 0.95)
})

test_that("factor input values returns correct numeric value", {
x <- factor(c("right", "left"))
expect_equal(compute_npc(x), c(0.95, 0.05))

})

test_that("numeric input value returns the same value if
between 0 and 1", {
x <- c(0.5, 1)
expect_equal(compute_npc(x), x)
})

test_that("numeric input value
returns 0 if less than 0,
returns 1 if greater than 1", {
x <- c(-0.5, 2)
expect_equal(compute_npc(x), c(0,1))
})

test_that("Value is returned 'AsIs'", {
expect_is(as_npc("right"), "AsIs")
expect_equal(as_npc("right"), I(0.95))
expect_equal(as_npc("left"), I(0.05))
expect_equal(as_npc("centre"), I(0.5))
expect_equal(as_npc("center"), I(0.5))
})
8 changes: 8 additions & 0 deletions tests/testthat/test-compute_npcx.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,11 @@ test_that("numeric input value
x <- c(-0.5, 2)
expect_equal(compute_npcx(x), c(0,1))
})

test_that("Value is returned 'AsIs'", {
expect_is(as_npcx("right"), "AsIs")
expect_equal(as_npcx("right"), I(0.95))
expect_equal(as_npcx("left"), I(0.05))
expect_equal(as_npcx("centre"), I(0.5))
expect_equal(as_npcx("center"), I(0.5))
})
9 changes: 9 additions & 0 deletions tests/testthat/test-compute_npcy.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ test_that("numeric input values
expect_equal(compute_npcy(y), c(0,1))
})

test_that("Value is returned 'AsIs'", {
expect_is(as_npcy("top"), "AsIs")
expect_equal(as_npcy("top"), I(0.95))
expect_equal(as_npcy("bottom"), I(0.05))
expect_equal(as_npcy("middle"), I(0.5))
expect_equal(as_npcx("center"), I(0.5))
expect_equal(as_npcx("centre"), I(0.5))
})

19 changes: 19 additions & 0 deletions tests/testthat/test-geom-text.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,24 @@ test_that("multiple_rows_tb", {
geom_text_npc(data = df,
mapping = aes(npcx = x, npcy = y, label = to.parse),
parse = TRUE))
expect_error(
ggplot(data = mtcars) +
geom_point(mapping = aes(wt, mpg)) +
geom_text_npc(data = df,
mapping = aes(npcx = x, npcy = y, label = to.parse),
parse = TRUE,
nudge_y = 0.1,
position = "stack")
)

expect_no_error(
ggplot(data = mtcars) +
geom_point(mapping = aes(wt, mpg)) +
geom_text_npc(data = df,
mapping = aes(npcx = x, npcy = y, label = to.parse),
parse = TRUE,
nudge_x = 0.1,
position = "identity")
)
})

0 comments on commit 4f57756

Please sign in to comment.