😎 Give your xaringan slides some style
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6KB

  1. # test_that()
  2. describe("prepare_colors()", {
  3. it("returns NULL if NULL or missing", {
  4. expect_null(prepare_colors())
  5. expect_null(prepare_colors(NULL))
  6. expect_null(prepare_colors(list()))
  7. })
  8. it("requires a named vector or list", {
  9. expect_error(prepare_colors("#00FF00"))
  10. })
  11. it("requires valid CSS names", {
  12. expect_error(prepare_colors(c("light blue" = "#88f")))
  13. expect_error(prepare_colors(c("light/blue" = "#88f")))
  14. })
  15. it("returns list with color_name and value for each color", {
  16. colors <- c('test' = "#4ed4ed")
  17. prepared <- prepare_colors(colors)
  18. expect_equal(names(prepared[[1]]), c("color_name", "value"))
  19. expect_equal(prepared[[1]]$color_name, names(colors)[[1]])
  20. expect_equal(prepared[[1]]$value, colors[[1]])
  21. })
  22. })
  23. describe("full_length_hex()", {
  24. it("makes 3-length hex to 6-length", {
  25. expect_equal(full_length_hex("#123"), "#112233")
  26. })
  27. it("keeps length 6 hex", {
  28. expect_equal(full_length_hex("#123456"), "#123456")
  29. })
  30. it("errors if not a hex color", {
  31. expect_error(full_length_hex("123abc"))
  32. expect_error(full_length_hex("#1234567"))
  33. expect_error(full_length_hex("#00000Z"))
  34. })
  35. })
  36. describe("check_color_is_hex", {
  37. it("returns TRUE when color is valid hex", {
  38. expect_true(check_color_is_hex("#123456"))
  39. expect_true(check_color_is_hex("#abc123"))
  40. expect_true(check_color_is_hex("#abc"))
  41. expect_true(check_color_is_hex("#123"))
  42. })
  43. it("returns FALSE and warns if requested", {
  44. expect_false(check_color_is_hex("123456", throw = NULL))
  45. expect_false(check_color_is_hex("123", throw = NULL))
  46. expect_false(check_color_is_hex("apple", throw = NULL))
  47. expect_warning(check_color_is_hex("123456"))
  48. expect_warning(check_color_is_hex("123", msg = "{color} is bad"), "123 is bad")
  49. expect_warning(check_color_is_hex("apple"))
  50. })
  51. it("errors if throw = stop", {
  52. expect_error(check_color_is_hex("123", throw = stop), "123 is not")
  53. })
  54. })
  55. describe("lighten_color() and darken_color()", {
  56. it("errors if strength not in [0, 1]", {
  57. expect_error(lighten_color("#123", 9))
  58. expect_error(darken_color("#123", 9))
  59. })
  60. it("returns original color if strength = 0", {
  61. expect_equal(lighten_color("#808080", 0), "#808080")
  62. expect_equal(darken_color("#808080", 0), "#808080")
  63. })
  64. it("returns entire blend color if strength = 0", {
  65. expect_equal(lighten_color("#808080", 1), "#FFFFFF")
  66. expect_equal(darken_color("#808080", 1), "#000000")
  67. })
  68. it("returns midpoint if strength = 0.5", {
  69. expect_equal(lighten_color("#000000", 0.5), "#7F7F7F")
  70. expect_equal(darken_color("#FFFFFF", 0.5), "#7F7F7F")
  71. })
  72. })