effects_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package rgb
  2. import (
  3. "testing"
  4. )
  5. func TestImpact80EffectName(t *testing.T) {
  6. cases := []struct {
  7. value uint8
  8. want string
  9. }{
  10. {0, "none"},
  11. {1, "wave"},
  12. {2, "fixed_wave"},
  13. {3, "spectrum"},
  14. {4, "breathing"},
  15. {5, "light"},
  16. {6, "shutdown"},
  17. {7, "unknown"},
  18. }
  19. for _, tc := range cases {
  20. if got := Impact80EffectName(tc.value); got != tc.want {
  21. t.Errorf("Impact80EffectName(%d) = %q, want %q", tc.value, got, tc.want)
  22. }
  23. }
  24. }
  25. func TestParseImpact80Effect(t *testing.T) {
  26. cases := []struct {
  27. input string
  28. want uint8
  29. }{
  30. {"none", 0},
  31. {"wave", 1},
  32. {"fixed_wave", 2},
  33. {"spectrum", 3},
  34. {"breathing", 4},
  35. {"light", 5},
  36. {"shutdown", 6},
  37. {"rainbow", 3},
  38. {"static", 5},
  39. }
  40. for _, tc := range cases {
  41. got, err := ParseImpact80Effect(tc.input)
  42. if err != nil {
  43. t.Errorf("ParseImpact80Effect(%q) unexpected error: %v", tc.input, err)
  44. continue
  45. }
  46. if got != tc.want {
  47. t.Errorf("ParseImpact80Effect(%q) = %d, want %d", tc.input, got, tc.want)
  48. }
  49. }
  50. }
  51. func TestQMKValueIDs(t *testing.T) {
  52. if Brightness != 1 {
  53. t.Errorf("Brightness = %d, want 1", Brightness)
  54. }
  55. if EffectID != 2 {
  56. t.Errorf("EffectID = %d, want 2", EffectID)
  57. }
  58. if Speed != 3 {
  59. t.Errorf("Speed = %d, want 3", Speed)
  60. }
  61. if ColorValue != 4 {
  62. t.Errorf("ColorValue = %d, want 4", ColorValue)
  63. }
  64. }
  65. func TestParseEffect(t *testing.T) {
  66. cases := []struct {
  67. name string
  68. input string
  69. want Effect
  70. err bool
  71. }{
  72. {"static", "static", Static, false},
  73. {"breathing", "breathing", Breathing, false},
  74. {"rainbow", "rainbow", Rainbow, false},
  75. {"rainbow_wave", "rainbow_wave", RainbowWave, false},
  76. {"snake", "snake", Snake, false},
  77. {"gradient", "gradient", Gradient, false},
  78. {"tail", "tail", Tail, false},
  79. {"unknown", "flubber", 0, true},
  80. {"empty", "", 0, true},
  81. }
  82. for _, tc := range cases {
  83. t.Run(tc.name, func(t *testing.T) {
  84. got, err := ParseEffect(tc.input)
  85. if tc.err {
  86. if err == nil {
  87. t.Errorf("ParseEffect(%q) expected error, got nil", tc.input)
  88. }
  89. return
  90. }
  91. if err != nil {
  92. t.Errorf("ParseEffect(%q) unexpected error: %v", tc.input, err)
  93. return
  94. }
  95. if got != tc.want {
  96. t.Errorf("ParseEffect(%q) = %d, want %d", tc.input, got, tc.want)
  97. }
  98. })
  99. }
  100. }
  101. func TestEffectString(t *testing.T) {
  102. cases := []struct {
  103. effect Effect
  104. want string
  105. }{
  106. {Static, "static"},
  107. {Breathing, "breathing"},
  108. {Rainbow, "rainbow"},
  109. {RainbowWave, "rainbow_wave"},
  110. {Effect(99), "unknown"},
  111. }
  112. for _, tc := range cases {
  113. t.Run(tc.want, func(t *testing.T) {
  114. got := tc.effect.String()
  115. if got != tc.want {
  116. t.Errorf("Effect(%d).String() = %q, want %q", tc.effect, got, tc.want)
  117. }
  118. })
  119. }
  120. }
  121. func TestParseHexColor(t *testing.T) {
  122. cases := []struct {
  123. name string
  124. input string
  125. want Color
  126. err bool
  127. }{
  128. {"red", "ff0000", Color{R: 255, G: 0, B: 0}, false},
  129. {"green", "00ff00", Color{R: 0, G: 255, B: 0}, false},
  130. {"blue", "0000ff", Color{R: 0, G: 0, B: 255}, false},
  131. {"white", "ffffff", Color{R: 255, G: 255, B: 255}, false},
  132. {"black", "000000", Color{R: 0, G: 0, B: 0}, false},
  133. {"lowercase", "aabbcc", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
  134. {"uppercase", "AABBCC", Color{R: 0xaa, G: 0xbb, B: 0xcc}, false},
  135. {"short", "ff000", Color{0, 0, 0}, true},
  136. {"long", "ff00000", Color{0, 0, 0}, true},
  137. {"empty", "", Color{0, 0, 0}, true},
  138. {"invalid", "gggggg", Color{}, true},
  139. }
  140. for _, tc := range cases {
  141. t.Run(tc.name, func(t *testing.T) {
  142. got, err := ParseHexColor(tc.input)
  143. if tc.err {
  144. if err == nil {
  145. t.Errorf("ParseHexColor(%q) expected error, got nil", tc.input)
  146. }
  147. return
  148. }
  149. if err != nil {
  150. t.Errorf("ParseHexColor(%q) unexpected error: %v", tc.input, err)
  151. return
  152. }
  153. if got != tc.want {
  154. t.Errorf("ParseHexColor(%q) = %+v, want %+v", tc.input, got, tc.want)
  155. }
  156. })
  157. }
  158. }
  159. func TestColorToHSV(t *testing.T) {
  160. cases := []struct {
  161. name string
  162. color Color
  163. wantH uint8
  164. wantS uint8
  165. wantV uint8
  166. }{
  167. {"red", Color{R: 255, G: 0, B: 0}, 0, 255, 255},
  168. {"green", Color{R: 0, G: 255, B: 0}, 85, 255, 255},
  169. {"blue", Color{R: 0, G: 0, B: 255}, 170, 255, 255},
  170. {"white", Color{R: 255, G: 255, B: 255}, 0, 0, 255},
  171. {"black", Color{R: 0, G: 0, B: 0}, 0, 0, 0},
  172. {"yellow", Color{R: 255, G: 255, B: 0}, 42, 255, 255},
  173. {"cyan", Color{R: 0, G: 255, B: 255}, 127, 255, 255},
  174. {"magenta", Color{R: 255, G: 0, B: 255}, 212, 255, 255},
  175. {"gray", Color{R: 128, G: 128, B: 128}, 0, 0, 128},
  176. {"dark red", Color{R: 128, G: 0, B: 0}, 0, 255, 128},
  177. {"light red", Color{R: 255, G: 128, B: 128}, 0, 127, 255},
  178. }
  179. for _, tc := range cases {
  180. t.Run(tc.name, func(t *testing.T) {
  181. h, s, v := tc.color.HSV()
  182. if h != tc.wantH {
  183. t.Errorf("HSV(%q).H = %d, want %d", tc.name, h, tc.wantH)
  184. }
  185. if s != tc.wantS {
  186. t.Errorf("HSV(%q).S = %d, want %d", tc.name, s, tc.wantS)
  187. }
  188. if v != tc.wantV {
  189. t.Errorf("HSV(%q).V = %d, want %d", tc.name, v, tc.wantV)
  190. }
  191. })
  192. }
  193. }