The main server, and probably only repository in this org.
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.

127 lines
2.5 KiB

  1. package models_test
  2. import (
  3. "git.aiterp.net/lucifer/lucifer/models"
  4. "github.com/stretchr/testify/assert"
  5. "testing"
  6. )
  7. func TestLight_SetColor_LoppingOffHash(t *testing.T) {
  8. light := models.Light{}
  9. err := light.SetColor("#FFB34F")
  10. assert.Nil(t, err)
  11. assert.Equal(t, "FFB34F", light.Color)
  12. }
  13. func TestLight_SetColor_InvalidLength(t *testing.T) {
  14. light := models.Light{}
  15. err := light.SetColor("FFB34F32")
  16. assert.NotNil(t, err)
  17. }
  18. func TestLight_SetColor_InvalidHex(t *testing.T) {
  19. light := models.Light{}
  20. err := light.SetColor("FFB34G")
  21. assert.NotNil(t, err)
  22. }
  23. func TestLight_SetColorRGB(t *testing.T) {
  24. light := models.Light{}
  25. light.SetColorRGB(32, 55, 100)
  26. assert.Equal(t, "203764", light.Color)
  27. }
  28. func TestLight_SetColorRGBf(t *testing.T) {
  29. light := models.Light{}
  30. light.SetColorRGBf(0.1, 0.2, 0.3)
  31. assert.Equal(t, "19334c", light.Color)
  32. }
  33. func TestLight_ColorRGB_Empty(t *testing.T) {
  34. light := models.Light{}
  35. light.Color = ""
  36. r, g, b, err := light.ColorRGB()
  37. assert.Equal(t, uint8(0), r)
  38. assert.Equal(t, uint8(0), g)
  39. assert.Equal(t, uint8(0), b)
  40. assert.Nil(t, err)
  41. }
  42. func TestLight_ColorRGB_Invalid(t *testing.T) {
  43. light := models.Light{}
  44. light.Color = "Utterly bonkers"
  45. _, _, _, err := light.ColorRGB()
  46. assert.NotNil(t, err)
  47. assert.Equal(t, models.ErrMalformedColor, err)
  48. }
  49. func TestLight_ColorRGB_TooShort(t *testing.T) {
  50. light := models.Light{}
  51. light.Color = "3002"
  52. _, _, _, err := light.ColorRGB()
  53. assert.NotNil(t, err)
  54. assert.Equal(t, models.ErrMalformedColor, err)
  55. }
  56. func TestLight_ColorRGB_TooLong(t *testing.T) {
  57. light := models.Light{}
  58. light.Color = "3002DB05"
  59. _, _, _, err := light.ColorRGB()
  60. assert.NotNil(t, err)
  61. assert.Equal(t, models.ErrMalformedColor, err)
  62. }
  63. func TestLight_ColorRGB_Valid(t *testing.T) {
  64. light := models.Light{}
  65. light.Color = "203764"
  66. r, g, b, err := light.ColorRGB()
  67. assert.Equal(t, uint8(32), r)
  68. assert.Equal(t, uint8(55), g)
  69. assert.Equal(t, uint8(100), b)
  70. assert.Nil(t, err)
  71. }
  72. func TestLight_ColorRGBf_Invalid(t *testing.T) {
  73. light := models.Light{}
  74. light.Color = "G0I1J2"
  75. r, g, b, err := light.ColorRGBf()
  76. assert.Equal(t, float64(0), r)
  77. assert.Equal(t, float64(0), g)
  78. assert.Equal(t, float64(0), b)
  79. assert.NotNil(t, err)
  80. }
  81. func TestLight_ColorRGBf_Valid(t *testing.T) {
  82. light := models.Light{}
  83. light.Color = "19334C"
  84. r, g, b, err := light.ColorRGBf()
  85. // Within half a percent
  86. assert.InDelta(t, float64(0.1), r, 0.005)
  87. assert.InDelta(t, float64(0.2), g, 0.005)
  88. assert.InDelta(t, float64(0.3), b, 0.005)
  89. assert.Nil(t, err)
  90. }