go-pattern-examples/behavior/10_state/state_test.go

53 lines
970 B
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package state
import (
"testing"
"time"
)
2020-05-05 17:43:54 +03:00
func TestRobotState(t *testing.T) {
robot := NewSportRobot("home keeper")
//主动驱动行为
robot.Move()
//跑
robot.UpdateState(&RuningState{})
//跑一会
time.Sleep(time.Millisecond * 234)
//走
robot.UpdateState(&WalkState{})
//走一会儿
time.Sleep(time.Millisecond * 544)
//继续跑
robot.UpdateState(&RuningState{})
}
func TestAlarmState(t *testing.T) {
2020-05-05 17:43:54 +03:00
expect := "vibrating humming ... vibrating humming...vibrating humming..." +
"vibrating humming ... vibrating humming...vibrating humming..." +
"sun rise ,get up ,get up get up..."
2020-05-06 06:13:33 +03:00
//创建一个手机闹铃
mobile := NewAlert()
2020-05-05 17:43:54 +03:00
2020-05-06 06:13:33 +03:00
ringsSounds := mobile.Alert()
2020-05-05 17:43:54 +03:00
2020-05-06 06:13:33 +03:00
//叠加振铃声音,振铃响两遍
ringsSounds += mobile.Alert()
2020-05-05 17:43:54 +03:00
2020-05-06 06:13:33 +03:00
//设置振铃的铃声
mobile.SetState(&AlertSong{})
2020-05-05 17:43:54 +03:00
2020-05-06 06:13:33 +03:00
ringsSounds += mobile.Alert()
2020-04-21 17:50:21 +03:00
2020-05-06 06:13:33 +03:00
if ringsSounds != expect {
t.Errorf("Expect result to equal %s, but %s.\n", expect, ringsSounds)
2020-04-21 17:50:21 +03:00
}
}