package models_test import ( "git.aiterp.net/lucifer/lucifer/models" "github.com/stretchr/testify/assert" "testing" ) func TestLight_SetColor_LoppingOffHash(t *testing.T) { light := models.Light{} err := light.SetColor("#FFB34F") assert.Nil(t, err) assert.Equal(t, "FFB34F", light.Color) } func TestLight_SetColor_InvalidLength(t *testing.T) { light := models.Light{} err := light.SetColor("FFB34F32") assert.NotNil(t, err) } func TestLight_SetColor_InvalidHex(t *testing.T) { light := models.Light{} err := light.SetColor("FFB34G") assert.NotNil(t, err) } func TestLight_SetColorRGB(t *testing.T) { light := models.Light{} light.SetColorRGB(32, 55, 100) assert.Equal(t, "203764", light.Color) } func TestLight_SetColorRGBf(t *testing.T) { light := models.Light{} light.SetColorRGBf(0.1, 0.2, 0.3) assert.Equal(t, "19334c", light.Color) } func TestLight_ColorRGB_Empty(t *testing.T) { light := models.Light{} light.Color = "" r, g, b, err := light.ColorRGB() assert.Equal(t, uint8(0), r) assert.Equal(t, uint8(0), g) assert.Equal(t, uint8(0), b) assert.Nil(t, err) } func TestLight_ColorRGB_Invalid(t *testing.T) { light := models.Light{} light.Color = "Utterly bonkers" _, _, _, err := light.ColorRGB() assert.NotNil(t, err) assert.Equal(t, models.ErrMalformedColor, err) } func TestLight_ColorRGB_TooShort(t *testing.T) { light := models.Light{} light.Color = "3002" _, _, _, err := light.ColorRGB() assert.NotNil(t, err) assert.Equal(t, models.ErrMalformedColor, err) } func TestLight_ColorRGB_TooLong(t *testing.T) { light := models.Light{} light.Color = "3002DB05" _, _, _, err := light.ColorRGB() assert.NotNil(t, err) assert.Equal(t, models.ErrMalformedColor, err) } func TestLight_ColorRGB_Valid(t *testing.T) { light := models.Light{} light.Color = "203764" r, g, b, err := light.ColorRGB() assert.Equal(t, uint8(32), r) assert.Equal(t, uint8(55), g) assert.Equal(t, uint8(100), b) assert.Nil(t, err) } func TestLight_ColorRGBf_Invalid(t *testing.T) { light := models.Light{} light.Color = "G0I1J2" r, g, b, err := light.ColorRGBf() assert.Equal(t, float64(0), r) assert.Equal(t, float64(0), g) assert.Equal(t, float64(0), b) assert.NotNil(t, err) } func TestLight_ColorRGBf_Valid(t *testing.T) { light := models.Light{} light.Color = "19334C" r, g, b, err := light.ColorRGBf() // Within half a percent assert.InDelta(t, float64(0.1), r, 0.005) assert.InDelta(t, float64(0.2), g, 0.005) assert.InDelta(t, float64(0.3), b, 0.005) assert.Nil(t, err) }