effects_test.go 3.9 KB

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