go-pattern-examples/creation/04_factory_method/factorymethod_test.go
2020-04-24 11:15:47 +08:00

28 lines
532 B
Go

package factorymethod
import "testing"
func doWork(factory IRobotFactory, cleanhour int) string {
robot := factory.Build()
robot.Clean(cleanhour)
robot.Speak("robot name")
return robot.Work()
}
func TestRobotFactory(t *testing.T) {
var factory IRobotFactory
factory = FightingRobotFactory{}
if doWork(factory, 2) != "i can fighting2" {
t.Fatal("error with factory method pattern")
}
factory = HomeRobotFactory{}
if doWork(factory, 1) != "i can do homework1" {
t.Fatal("error with factory method pattern")
}
}