😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

103 lines
3.1KB

  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_warning(prepare_colors(c("light/blue" = "#88f")), "light/blue")
  14. expect_warning(prepare_colors(c("-lightblue" = "#88f")), "-lightblue")
  15. expect_warning(prepare_colors(c("_lightblue" = "#88f")), "_lightblue")
  16. colors <- c("light_blue" = "#88f", "light-blue" = "#88f")
  17. expect_silent(
  18. expect_equal(
  19. prepare_colors(colors),
  20. list(
  21. list(color_name = "light_blue", value = "#88f"),
  22. list(color_name = "light-blue", value = "#88f")
  23. )
  24. )
  25. )
  26. })
  27. it("returns list with color_name and value for each color", {
  28. colors <- c('test' = "#4ed4ed")
  29. prepared <- prepare_colors(colors)
  30. expect_equal(names(prepared[[1]]), c("color_name", "value"))
  31. expect_equal(prepared[[1]]$color_name, names(colors)[[1]])
  32. expect_equal(prepared[[1]]$value, colors[[1]])
  33. })
  34. })
  35. describe("full_length_hex()", {
  36. it("makes 3-length hex to 6-length", {
  37. expect_equal(full_length_hex("#123"), "#112233")
  38. })
  39. it("keeps length 6 hex", {
  40. expect_equal(full_length_hex("#123456"), "#123456")
  41. })
  42. it("errors if not a hex color", {
  43. expect_error(full_length_hex("123abc"))
  44. expect_error(full_length_hex("#1234567"))
  45. expect_error(full_length_hex("#00000Z"))
  46. })
  47. })
  48. describe("check_color_is_hex", {
  49. it("returns TRUE when color is valid hex", {
  50. expect_true(check_color_is_hex("#123456"))
  51. expect_true(check_color_is_hex("#abc123"))
  52. expect_true(check_color_is_hex("#abc"))
  53. expect_true(check_color_is_hex("#123"))
  54. })
  55. it("returns FALSE and warns if requested", {
  56. expect_false(check_color_is_hex("123456", throw = NULL))
  57. expect_false(check_color_is_hex("123", throw = NULL))
  58. expect_false(check_color_is_hex("apple", throw = NULL))
  59. expect_warning(check_color_is_hex("123456"))
  60. expect_warning(check_color_is_hex("123", msg = "{color} is bad"), "123 is bad")
  61. expect_warning(check_color_is_hex("apple"))
  62. })
  63. it("errors if throw = stop", {
  64. expect_error(check_color_is_hex("123", throw = stop), "123 is not")
  65. })
  66. })
  67. describe("lighten_color() and darken_color()", {
  68. it("errors if strength not in [0, 1]", {
  69. expect_error(lighten_color("#123", 9))
  70. expect_error(darken_color("#123", 9))
  71. })
  72. it("returns original color if strength = 0", {
  73. expect_equal(lighten_color("#808080", 0), "#808080")
  74. expect_equal(darken_color("#808080", 0), "#808080")
  75. })
  76. it("returns entire blend color if strength = 0", {
  77. expect_equal(lighten_color("#808080", 1), "#FFFFFF")
  78. expect_equal(darken_color("#808080", 1), "#000000")
  79. })
  80. it("returns midpoint if strength = 0.5", {
  81. expect_equal(lighten_color("#000000", 0.5), "#7F7F7F")
  82. expect_equal(darken_color("#FFFFFF", 0.5), "#7F7F7F")
  83. })
  84. })