Skip to content

Commit

Permalink
Port tests to tinytest
Browse files Browse the repository at this point in the history
  • Loading branch information
jonclayden committed Oct 14, 2024
1 parent 5ee96dd commit c4e6bb9
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 186 deletions.
34 changes: 34 additions & 0 deletions inst/tinytest/test-05-shade.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Create shade objects from various objects
expect_error(shade(), "must not be empty")
expect_error(shade(character(0)), "must not be empty")
expect_stdout(print(shade("red")), "1 shade")
expect_equal(space(shade("red")), "sRGB")
expect_equivalent(coords("red"), matrix(c(1,0,0),nrow=1))
expect_equivalent(shade(matrix(c(1,0,0),nrow=1)), shade("#FF0000"))
# Using a factor to check the default initialisation method
expect_equivalent(shade(factor(rep("red",3))), rep(shade("red"),3))

if (requireNamespace("colorspace", quietly=TRUE))
expect_true(shade(colorspace::sRGB(1,0,0)) == "red")

# Convert shade objects between spaces
expect_equivalent(coords(warp("red","HSV")), matrix(c(0,1,1),nrow=1))
expect_equivalent(round(coords(warp(shade("red"),"LAB"))), matrix(c(53,80,67),nrow=1))
# Check that precision loss isn't too great when making a round-trip conversion
shade <- shade(matrix(runif(3),nrow=1), space="sRGB")
expect_equivalent(coords(warp(warp(shade,"HSV"),"sRGB")), coords(shade), tolerance=1e-4)

# Index, combine and compare shade objects
shades <- shade(c("red","green","blue"))
expect_true(shades[1] == "red")
shades[1] <- "darkred"
expect_false(shades[1] == "red")

expect_equal(c(shade("red"),shade("green")), shade(c("red","green")))
expect_true(shade("red") != shade("green"))
expect_true(shade("red") == shade(matrix(c(0,1,1),nrow=1),space="HSV"))
expect_equal(space(c(shade("red"), shade(matrix(c(0,1,1),nrow=1),space="HSV"))), "XYZ")

# expect_match exists in tinytest, but is broken in the current CRAN build
expect_true(grepl("Mean colour distance is", all.equal(shade("red"),shade("green"))))
expect_true(grepl("Lengths do not match", all.equal(shade("red"),shade(c("green","blue")))))
8 changes: 8 additions & 0 deletions inst/tinytest/test-10-scales.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using("shades")

# Create colour scales traversing different spaces
expect_equal_shades(gradient(c("red","blue"),3), c("#FF0000","#800080","#0000FF"))
expect_equal_shades(rev(gradient(c("red","blue"),3)), c("#0000FF","#800080","#FF0000"))
expect_equal_shades(gradient(c("red","blue"),3,space="LAB"), c("#FF0000","#C90089","#0000FF"))
expect_equal_shades(gradient("magma",3), c("#000004","#B63779","#FCFDBF"))
expect_error(gradient("nothing",3), "should specify a predefined colour map")
30 changes: 30 additions & 0 deletions inst/tinytest/test-15-properties.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using("shades")

# Extract colour properties
expect_equal(saturation("red"), 1)
expect_equal(brightness("red"), 1)
expect_equal(saturation("grey40"), 0)
expect_equal(brightness(c("grey40","grey60")), c(0.4,0.6))
expect_equal(round(chroma(c("black","white","red"))), c(0,0,105))
expect_equal(round(lightness(c("black","white","red"))), c(0,100,53))

# Manipulate colour properties
expect_equal_shades(saturation("red",0.5), shade("#FF8080"))
expect_equal_shades(brightness("red",0.5), shade("#800000"))
expect_equal_shades(hue("red",delta(240)), shade("#0000FF"))
expect_equal_shades(hue("blue",delta(240)), shade("#00FF00"))
expect_equal_shades(lightness("red",scalefac(0.5)), shade("#A60000"))
expect_equal_shades(brightness("red",scalefac(0.4,0.6)), c("#660000","#990000"))
expect_equivalent(coords(brightness("grey40",c(0.2,0.6))), matrix(c(0,0,0,0,0.2,0.6),nrow=2))

# Manipulate colour matrices and check dimensions
shades <- shade(c("red","green","blue"))
matrix <- rep(shades, 2)
dim(matrix) <- c(3L,2L)

expect_null(dim(brightness(shades,0.5)))
expect_equal(length(brightness(shades,0.5)), 3L)
expect_equal(dim(brightness(shades,c(0.4,0.6))), c(2L,3L))
expect_equal_shades(brightness(shades,c(0.4,0.6)), c("#660000","#990000","#006600","#009900","#000066","#000099"))
expect_equal(dim(brightness(matrix,c(0.4,0.6))), c(2L,3L,2L))
expect_equal(dim(brightness(matrix,recycle(0.4,0.6))), c(3L,2L))
11 changes: 11 additions & 0 deletions inst/tinytest/test-20-mixtures.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using("shades")

# Colour mixing and complements
expect_equivalent(coords(complement("cyan")), coords(shade("red")))
expect_equal(complement("cyan",space="HSV"), warp("red","HSV"))
expect_equal_shades(complement("cyan",space="Lab"), "#4F0002")

expect_equivalent(addmix("blue","green"), shade("#00FFFF"))
expect_equivalent(submix("cyan","yellow"), shade("#00FF00"))
expect_equivalent("blue" %.)% "green", shade("#00FFFF"))
expect_equivalent("cyan" %_/% "yellow", shade("#00FF00"))
2 changes: 2 additions & 0 deletions inst/tinytest/test-25-distance.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
expect_equal(round(distance(c("red","green","blue"),"red")), c(0,87,53))
expect_error(distance("red", c("red","green","blue")), "Reference should be a single shade")
7 changes: 7 additions & 0 deletions inst/tinytest/test-30-adaptation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using("shades")

primaries <- shade(c("red", "green", "blue"))
expect_equal_shades(dichromat(primaries), c("#4D4222","#FFF600","#0027FF"))
expect_equal_shades(dichromat(primaries,"protanopic"), c("#4D4222","#FFF600","#0027FF"))
expect_equal_shades(dichromat(primaries,"deuteranopic"), c("#C5A700","#D0B335","#0068FE"))
expect_equal_shades(dichromat(primaries,"tritanopic"), c("#FF0050","#00FBFF","#372828"))
2 changes: 2 additions & 0 deletions inst/tinytest/test-35-swatch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
expect_null(swatch(c("red", "green", "blue")))
expect_null(swatch(saturation(brightness(c("red", "green", "blue"), c(0.25,0.75)), c(0.25,0.75))))
25 changes: 25 additions & 0 deletions inst/tinytest/test-40-alpha.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using("shades")

expect_equal(opacity("red"), 1)
expect_equal(opacity("#FF000080"), 128/255)
expect_equal(opacity(saturation("#FF000080",0.5)), 128/255)
expect_equal_shades(warp("#FF000080","HSV"), "#FF000080")

expect_equal_shades(opacity("red",c(0,0.5)), c("#FF000000","#FF000080"))
expect_equal_shades(opacity("red",delta(-0.5)), "#FF000080")
expect_null(attr(opacity("#FF000080",delta(0.5)),"alpha"))

shades <- shade(c("red","green","blue"))
matrix <- rep(shades, 2)
dim(matrix) <- c(3L,2L)

expect_null(attr(shades,"alpha"))
expect_equal(opacity(c(shades,"#00000000")), c(1,1,1,0))
shades[2] <- "#00FF0000"
expect_equal(attr(shades,"alpha"), c(1,0,1))
expect_equal(dim(opacity(matrix,0.5)), c(3L,2L))
expect_equal(dim(opacity(matrix,recycle(0.4,0.6))), c(3L,2L))

expect_true(shade("#FF8080") == shade("#FF8080FF"))
expect_false(shade("#FF8080") == shade("#FF8080DD"))
expect_true(grepl("Alpha values do not match", all.equal(shade("#FF8080"), "#FF8080DD")))
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
context("Handling palette functions")
viridis <- gradient("viridis")
expect_equal(viridis(5L), gradient("viridis",5L))

test_that("property methods can be applied to functions", {
viridis <- gradient("viridis")
expect_equal(viridis(5L), gradient("viridis",5L))

# The distinction is in the parentheses here: in one case lightness() is called on colours generated by the colour ramp; in the other, the ramp function is wrapped by lightness() and then called
expect_equal(lightness(viridis(5L)), lightness(viridis)(5L))
expect_equal(lightness(viridis(5L),50), lightness(viridis,50)(5L))

expect_equal(opacity(viridis(5L),0.5), opacity(viridis,0.5)(5L))
expect_equal(complement(viridis(5L)), complement(viridis)(5L))
expect_equal(addmix(viridis(5L),"red"), addmix(viridis,"red")(5L))
expect_equal(submix(viridis(5L),"red"), submix(viridis,"red")(5L))

skip_if_not_installed("ggplot2")

# The distinction is in the parentheses here: in one case lightness() is called on colours generated by the colour ramp; in the other, the ramp function is wrapped by lightness() and then called
expect_equal(lightness(viridis(5L)), lightness(viridis)(5L))
expect_equal(lightness(viridis(5L),50), lightness(viridis,50)(5L))

expect_equal(opacity(viridis(5L),0.5), opacity(viridis,0.5)(5L))
expect_equal(complement(viridis(5L)), complement(viridis)(5L))
expect_equal(addmix(viridis(5L),"red"), addmix(viridis,"red")(5L))
expect_equal(submix(viridis(5L),"red"), submix(viridis,"red")(5L))

if (requireNamespace("ggplot2", quietly=TRUE))
{
library(ggplot2)

data <- data.frame(sex=c("M","M","F","F"), age=c(23,34,28,26), height=c(180,168,159,170))

# This relies on scales::viridis_pal (at time of writing) agreeing with our viridis scale, but this seems less fragile than assuming that scales and ggplot2 continue to interact the way they do now (and adding another suggested dependency)
Expand All @@ -34,4 +30,4 @@ test_that("property methods can be applied to functions", {

plot <- ggplot(data, aes(x=age,y=height,colour=sex)) + geom_point() + submix(scale_colour_viridis_d(), "red")
expect_true(all(layer_data(plot)$colour %in% submix(viridis(2), "red", space="sRGB")))
})
}
19 changes: 19 additions & 0 deletions inst/tinytest/test-50-missing.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using("shades")

# Missing shade handling
shades <- gradient(c("red","blue"), 3)
shades[2] <- NA

expect_equal(is.na(shades), c(FALSE,TRUE,FALSE))
expect_true(all(is.na(coords(shades)[2,])))
expect_equal(saturation(shades), c(1,NA,1))
expect_equal_shades(saturation(shades,0.5), shade(c("#FF8080",NA,"#8080FF")))
expect_equivalent(is.na(saturation(shades, c(0,0.5,1))), matrix(c(FALSE,TRUE,FALSE),3,3,byrow=TRUE))
expect_equivalent(complement(shades), shade(c("#00FFFF",NA,"#FFFF00")))
expect_equivalent(dichromat(shades)[2], shade(NA))
expect_equal_shades(opacity(shades,0.5), c("#FF000080",NA,"#0000FF80"))

# NAs as new property values should lead to pass-through
expect_equal(saturation(saturation("olivedrab",NA)), saturation("olivedrab"))
expect_equal_shades(opacity("red",NA), "red")
expect_equal_shades(opacity("red",c(0,NA,1)), c("#FF000000","#FF0000FF","#FF0000FF"))
4 changes: 0 additions & 4 deletions tests/testthat.R

This file was deleted.

38 changes: 0 additions & 38 deletions tests/testthat/test-05-shade.R

This file was deleted.

9 changes: 0 additions & 9 deletions tests/testthat/test-10-scales.R

This file was deleted.

33 changes: 0 additions & 33 deletions tests/testthat/test-15-properties.R

This file was deleted.

12 changes: 0 additions & 12 deletions tests/testthat/test-20-mixtures.R

This file was deleted.

6 changes: 0 additions & 6 deletions tests/testthat/test-25-distance.R

This file was deleted.

9 changes: 0 additions & 9 deletions tests/testthat/test-30-adaptation.R

This file was deleted.

9 changes: 0 additions & 9 deletions tests/testthat/test-35-swatch.R

This file was deleted.

27 changes: 0 additions & 27 deletions tests/testthat/test-40-alpha.R

This file was deleted.

21 changes: 0 additions & 21 deletions tests/testthat/test-50-missing.R

This file was deleted.

2 changes: 2 additions & 0 deletions tests/tinytest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if (requireNamespace("tinytest", quietly=TRUE))
tinytest::test_package("shades")

0 comments on commit c4e6bb9

Please sign in to comment.