func TestRepeat(t *testing.T) { action := actions.Action{On: true, Repeat: 3} expected := []request{ {L: 0, On: maybe.NewBool(true), D: 0}, {L: 0, On: maybe.NewBool(true), D: 0}, {L: 0, On: maybe.NewBool(true), D: 0}} verifyAction(t, expected, action) }
func TestSeries(t *testing.T) { action := actions.Action{ Series: []*actions.Action{ {Lights: []int{2, 3}, On: true}, {Sleep: 3000}, {Off: true}}} expected := []request{ {L: 2, On: maybe.NewBool(true), D: 0}, {L: 3, On: maybe.NewBool(true), D: 0}, {L: 0, On: maybe.NewBool(false), D: 3000}} verifyAction(t, expected, action) }
// Get gets the properties of a light. lightId is the ID of the light. // properties is the returned properties. // response is the raw response from the hue bridge or nil if communication // failed. This function may return both a non-nil response and an error // if the response from the hue bridge indicates an error. For most // applications, it is enough just to look at properties and err. func (c *Context) Get(lightId int) ( properties *LightProperties, response []byte, err error) { request := &http.Request{ Method: "GET", URL: c.getLightUrl(lightId), } client := c.client var resp *http.Response if resp, err = client.Do(request); err != nil { return } defer resp.Body.Close() var respBuffer bytes.Buffer if _, err = respBuffer.ReadFrom(resp.Body); err != nil { return } response = respBuffer.Bytes() var jsonProps json_structs.LightState if err = json.Unmarshal(response, &jsonProps); err != nil { err = toError(response) return } if jsonProps.State != nil && len(jsonProps.State.XY) == 2 { state := jsonProps.State jsonColor := state.XY properties = &LightProperties{ C: NewMaybeColor(NewColor(jsonColor[0], jsonColor[1])), Bri: maybe.NewUint8(state.Bri), On: maybe.NewBool(state.On)} } else { err = GeneralError } return }
func TestOnBrightness(t *testing.T) { action := actions.Action{ On: true, Bri: maybe.NewUint8(135)} expected := []request{ {L: 0, Bri: maybe.NewUint8(135), On: maybe.NewBool(true), D: 0}} verifyAction(t, expected, action) }
func TestOnColor(t *testing.T) { action := actions.Action{ On: true, C: gohue.NewMaybeColor(gohue.NewColor(0.4, 0.2))} expected := []request{ {L: 0, C: gohue.NewMaybeColor(gohue.NewColor(0.4, 0.2)), On: maybe.NewBool(true), D: 0}} verifyAction(t, expected, action) }
func TestMaybeBool(t *testing.T) { var m, c maybe.Bool var v bool = true m.Set(v) if m != maybe.NewBool(v) { t.Error("Bool Set broken.") } verifyString(t, "Just true", m.String()) m.Clear() if m != c { t.Error("Bool Clear broken.") } verifyString(t, "Nothing", m.String()) }
func TestGradient(t *testing.T) { action := actions.Action{ Lights: []int{2}, G: &actions.Gradient{ Cds: []actions.ColorDuration{ {C: gohue.NewMaybeColor(gohue.NewColor(0.2, 0.1)), Bri: maybe.NewUint8(0), D: 0}, {C: gohue.NewMaybeColor(gohue.NewColor(0.3, 0.3)), Bri: maybe.NewUint8(30), D: 1000}, {C: gohue.NewMaybeColor(gohue.NewColor(0.9, 0.9)), Bri: maybe.NewUint8(100), D: 1000}, {C: gohue.NewMaybeColor(gohue.NewColor(0.8, 0.7)), Bri: maybe.NewUint8(100), D: 1000}, {C: gohue.NewMaybeColor(gohue.NewColor(0.2, 0.4)), Bri: maybe.NewUint8(10), D: 1750}, {C: gohue.NewMaybeColor(gohue.NewColor(0.29, 0.46)), Bri: maybe.NewUint8(22), D: 2500}}, Refresh: 500}, On: true} expected := []request{ {L: 2, C: gohue.NewMaybeColor(gohue.NewColor(0.2, 0.1)), Bri: maybe.NewUint8(0), On: maybe.NewBool(true), D: 0}, {L: 2, C: gohue.NewMaybeColor(gohue.NewColor(0.25, 0.2)), Bri: maybe.NewUint8(15), D: 500}, {L: 2, C: gohue.NewMaybeColor(gohue.NewColor(0.8, 0.7)), Bri: maybe.NewUint8(100), D: 1000}, {L: 2, C: gohue.NewMaybeColor(gohue.NewColor(0.4, 0.5)), Bri: maybe.NewUint8(40), D: 1500}, {L: 2, C: gohue.NewMaybeColor(gohue.NewColor(0.23, 0.42)), Bri: maybe.NewUint8(14), D: 2000}, {L: 2, C: gohue.NewMaybeColor(gohue.NewColor(0.29, 0.46)), Bri: maybe.NewUint8(22), D: 2500}} verifyAction(t, expected, action) }
func TestError(t *testing.T) { action := actions.Action{ Series: []*actions.Action{ {Lights: []int{2, 3}, On: true}, {Sleep: 3000}, {Off: true}}} expected := []request{ {L: 2, On: maybe.NewBool(true), D: 0}} clock := &tasks.ClockForTesting{kNow} context := &setterForTesting{ err: kSomeError, response: ([]byte)("goodbye"), clock: clock, now: kNow} err := tasks.RunForTesting(action.AsTask(context, nil), clock) if !reflect.DeepEqual(expected, context.requests) { t.Errorf("Expected %v, got %v", expected, context.requests) } _, isNoSuchLightIdError := err.(*actions.NoSuchLightIdError) if isNoSuchLightIdError { t.Error("Expected not to get NoSuchLightIdError.") return } if out := err.Error(); out != "goodbye" { t.Errorf("Expected to get 'goodbye', got %s", out) } }
func TestOff(t *testing.T) { action := actions.Action{Off: true} expected := []request{{L: 0, On: maybe.NewBool(false), D: 0}} verifyAction(t, expected, action) }