Skip to content

Commit

Permalink
Clean up test_helper
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 13, 2014
1 parent 1a63727 commit 56848d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
8 changes: 4 additions & 4 deletions gobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
var g *Gobot

func init() {
log.SetOutput(new(null))
log.SetOutput(new(Null))
g = NewGobot()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
g.Robots = []*Robot{
newTestRobot("Robot 1"),
newTestRobot("Robot 2"),
newTestRobot("Robot 3"),
NewTestRobot("Robot 1"),
NewTestRobot("Robot 2"),
NewTestRobot("Robot 3"),
}
}

Expand Down
61 changes: 29 additions & 32 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"testing"
)

type testStruct struct {
i int
f float64
}

func Expect(t *testing.T, a interface{}, b interface{}) {
if !reflect.DeepEqual(a, b) {
_, file, line, _ := runtime.Caller(1)
Expand All @@ -21,13 +16,25 @@ func Expect(t *testing.T, a interface{}, b interface{}) {
}
}

type testStruct struct {
i int
f float64
}

func NewTestStruct() *testStruct {
return &testStruct{
i: 10,
f: 0.2,
}
}

func (t *testStruct) Hello(name string, message string) string {
return fmt.Sprintf("Hello %v! %v", name, message)
}

type null struct{}
type Null struct{}

func (null) Write(p []byte) (int, error) {
func (Null) Write(p []byte) (int, error) {
return len(p), nil
}

Expand All @@ -40,14 +47,7 @@ func (t *testDriver) Init() bool { return true }
func (t *testDriver) Start() bool { return true }
func (t *testDriver) Halt() bool { return true }

type testAdaptor struct {
Adaptor
}

func (t *testAdaptor) Finalize() bool { return true }
func (t *testAdaptor) Connect() bool { return true }

func newTestDriver(name string, adaptor *testAdaptor) *testDriver {
func NewTestDriver(name string, adaptor *testAdaptor) *testDriver {
t := &testDriver{
Driver: Driver{
Commands: make(map[string]func(map[string]interface{}) interface{}),
Expand All @@ -69,7 +69,14 @@ func newTestDriver(name string, adaptor *testAdaptor) *testDriver {
return t
}

func newTestAdaptor(name string) *testAdaptor {
type testAdaptor struct {
Adaptor
}

func (t *testAdaptor) Finalize() bool { return true }
func (t *testAdaptor) Connect() bool { return true }

func NewTestAdaptor(name string) *testAdaptor {
return &testAdaptor{
Adaptor: Adaptor{
Name: name,
Expand All @@ -82,15 +89,12 @@ func newTestAdaptor(name string) *testAdaptor {
}

func NewTestRobot(name string) *Robot {
return newTestRobot(name)
}
func newTestRobot(name string) *Robot {
adaptor1 := newTestAdaptor("Connection 1")
adaptor2 := newTestAdaptor("Connection 2")
adaptor3 := newTestAdaptor("Connection 3")
driver1 := newTestDriver("Device 1", adaptor1)
driver2 := newTestDriver("Device 2", adaptor2)
driver3 := newTestDriver("Device 3", adaptor3)
adaptor1 := NewTestAdaptor("Connection 1")
adaptor2 := NewTestAdaptor("Connection 2")
adaptor3 := NewTestAdaptor("Connection 3")
driver1 := NewTestDriver("Device 1", adaptor1)
driver2 := NewTestDriver("Device 2", adaptor2)
driver3 := NewTestDriver("Device 3", adaptor3)
work := func() {}
r := NewRobot(name, []Connection{adaptor1, adaptor2, adaptor3}, []Device{driver1, driver2, driver3}, work)
r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} {
Expand All @@ -100,10 +104,3 @@ func newTestRobot(name string) *Robot {
})
return r
}

func newTestStruct() *testStruct {
return &testStruct{
i: 10,
f: 0.2,
}
}
6 changes: 3 additions & 3 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ func TestRand(t *testing.T) {
}

func TestFieldByName(t *testing.T) {
testInterface := *newTestStruct()
testInterface := *NewTestStruct()
Expect(t, FieldByName(testInterface, "i").Int(), int64(10))
}

func TestFieldByNamePtr(t *testing.T) {
testInterface := newTestStruct()
testInterface := NewTestStruct()
Expect(t, FieldByNamePtr(testInterface, "f").Float(), 0.2)
}

func TestCall(t *testing.T) {
testInterface := newTestStruct()
testInterface := NewTestStruct()
Expect(t, Call(testInterface, "Hello", "Human", "How are you?")[0].String(), "Hello Human! How are you?")
}

0 comments on commit 56848d5

Please sign in to comment.