effects_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package rgb
  2. import (
  3. "testing"
  4. )
  5. func TestParseEffect(t *testing.T) {
  6. cases := []struct {
  7. name string
  8. input string
  9. want Effect
  10. err bool
  11. }{
  12. {"static", "static", Static, false},
  13. {"breathing", "breathing", Breathing, false},
  14. {"rainbow", "rainbow", Rainbow, false},
  15. {"rainbow_wave", "rainbow_wave", RainbowWave, false},
  16. {"snake", "snake", Snake, false},
  17. {"gradient", "gradient", Gradient, false},
  18. {"tail", "tail", Tail, false},
  19. {"unknown", "flubber", 0, true},
  20. {"empty", "", 0, true},
  21. }
  22. for _, tc := range cases {
  23. t.Run(tc.name, func(t *testing.T) {
  24. got, err := ParseEffect(tc.input)
  25. if tc.err {
  26. if err == nil {
  27. t.Errorf("ParseEffect(%q) expected error, got nil", tc.input)
  28. }
  29. return
  30. }
  31. if err != nil {
  32. t.Errorf("ParseEffect(%q) unexpected error: %v", tc.input, err)
  33. return
  34. }
  35. if got != tc.want {
  36. t.Errorf("ParseEffect(%q) = %d, want %d", tc.input, got, tc.want)
  37. }
  38. })
  39. }
  40. }
  41. func TestEffectString(t *testing.T) {
  42. cases := []struct {
  43. effect Effect
  44. want string
  45. }{
  46. {Static, "static"},
  47. {Breathing, "breathing"},
  48. {Rainbow, "rainbow"},
  49. {RainbowWave, "rainbow_wave"},
  50. {Effect(99), "unknown"},
  51. }
  52. for _, tc := range cases {
  53. t.Run(tc.want, func(t *testing.T) {
  54. got := tc.effect.String()
  55. if got != tc.want {
  56. t.Errorf("Effect(%d).String() = %q, want %q", tc.effect, got, tc.want)
  57. }
  58. })
  59. }
  60. }
  61. func TestParseHexColor(t *testing.T) {
  62. cases := []struct {
  63. name string
  64. input string
  65. want Color
  66. err bool
  67. }{
  68. {"red", "ff0000", Color{R: 255, G: 0, B: 0}, false},
  69. {"green", "00ff00", Color{R: 0, G: 255, B: 0}, false},
  70. {"blue", "0000ff", Color{R: 0, G: 0, B: 255}, false},
  71. {"white", "ffffff", Color{R: 255, G: 255, B: 255}, false},
  72. {"black", "000000", Color{R: 0, G: 0, B: 0}, false},
  73. {"lowercase", "aabbcc", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
  74. {"uppercase", "AABBCC", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
  75. {"short", "ff000", Color{0, 0, 0}, true},
  76. {"long", "ff00000", Color{0, 0, 0}, true},
  77. {"empty", "", Color{0, 0, 0}, true},
  78. {"invalid", "gggggg", Color{R: 0, G: 0, B: 0}, false},
  79. }
  80. for _, tc := range cases {
  81. t.Run(tc.name, func(t *testing.T) {
  82. got, err := ParseHexColor(tc.input)
  83. if tc.err {
  84. if err == nil {
  85. t.Errorf("ParseHexColor(%q) expected error, got nil", tc.input)
  86. }
  87. return
  88. }
  89. if err != nil {
  90. t.Errorf("ParseHexColor(%q) unexpected error: %v", tc.input, err)
  91. return
  92. }
  93. if got != tc.want {
  94. t.Errorf("ParseHexColor(%q) = %+v, want %+v", tc.input, got, tc.want)
  95. }
  96. })
  97. }
  98. }
  99. func TestColorToHSV(t *testing.T) {
  100. cases := []struct {
  101. name string
  102. color Color
  103. wantH uint8
  104. wantS uint8
  105. wantV uint8
  106. }{
  107. {"red", Color{R: 255, G: 0, B: 0}, 0, 255, 255},
  108. {"green", Color{R: 0, G: 255, B: 0}, 85, 255, 255},
  109. {"blue", Color{R: 0, G: 0, B: 255}, 170, 255, 255},
  110. {"white", Color{R: 255, G: 255, B: 255}, 0, 0, 255},
  111. {"black", Color{R: 0, G: 0, B: 0}, 0, 0, 0},
  112. {"yellow", Color{R: 255, G: 255, B: 0}, 42, 255, 255},
  113. {"cyan", Color{R: 0, G: 255, B: 255}, 127, 255, 255},
  114. {"magenta", Color{R: 255, G: 0, B: 255}, 212, 255, 255},
  115. {"gray", Color{R: 128, G: 128, B: 128}, 0, 0, 128},
  116. {"dark red", Color{R: 128, G: 0, B: 0}, 0, 255, 128},
  117. {"light red", Color{R: 255, G: 128, B: 128}, 0, 127, 255},
  118. }
  119. for _, tc := range cases {
  120. t.Run(tc.name, func(t *testing.T) {
  121. h, s, v := tc.color.HSV()
  122. if h != tc.wantH {
  123. t.Errorf("HSV(%q).H = %d, want %d", tc.name, h, tc.wantH)
  124. }
  125. if s != tc.wantS {
  126. t.Errorf("HSV(%q).S = %d, want %d", tc.name, s, tc.wantS)
  127. }
  128. if v != tc.wantV {
  129. t.Errorf("HSV(%q).V = %d, want %d", tc.name, v, tc.wantV)
  130. }
  131. })
  132. }
  133. }