2013-05-03 18:49:15 -04:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/asserts"
|
|
|
|
"net/rpc"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testUi struct {
|
2013-06-14 18:47:06 -04:00
|
|
|
askCalled bool
|
|
|
|
askQuery string
|
2013-06-03 14:30:38 -04:00
|
|
|
errorCalled bool
|
|
|
|
errorMessage string
|
|
|
|
messageCalled bool
|
|
|
|
messageMessage string
|
|
|
|
sayCalled bool
|
|
|
|
sayMessage string
|
2013-05-03 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
2013-06-15 21:24:38 -04:00
|
|
|
func (u *testUi) Ask(query string) (string, error) {
|
2013-06-14 18:17:03 -04:00
|
|
|
u.askCalled = true
|
|
|
|
u.askQuery = query
|
2013-06-15 21:24:38 -04:00
|
|
|
return "foo", nil
|
2013-06-14 18:17:03 -04:00
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
func (u *testUi) Error(message string) {
|
2013-05-08 18:12:48 -04:00
|
|
|
u.errorCalled = true
|
2013-05-27 18:12:48 -04:00
|
|
|
u.errorMessage = message
|
2013-05-08 18:12:48 -04:00
|
|
|
}
|
|
|
|
|
2013-06-03 14:30:38 -04:00
|
|
|
func (u *testUi) Message(message string) {
|
|
|
|
u.messageCalled = true
|
|
|
|
u.messageMessage = message
|
|
|
|
}
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
func (u *testUi) Say(message string) {
|
2013-05-03 18:49:15 -04:00
|
|
|
u.sayCalled = true
|
2013-05-27 18:12:48 -04:00
|
|
|
u.sayMessage = message
|
2013-05-03 18:49:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUiRPC(t *testing.T) {
|
|
|
|
assert := asserts.NewTestingAsserts(t, true)
|
|
|
|
|
|
|
|
// Create the UI to test
|
|
|
|
ui := new(testUi)
|
|
|
|
|
|
|
|
// Start the RPC server
|
2013-05-05 20:38:50 -04:00
|
|
|
server := rpc.NewServer()
|
|
|
|
RegisterUi(server, ui)
|
|
|
|
address := serveSingleConn(server)
|
2013-05-03 18:49:15 -04:00
|
|
|
|
|
|
|
// Create the client over RPC and run some methods to verify it works
|
2013-05-05 20:38:50 -04:00
|
|
|
client, err := rpc.Dial("tcp", address)
|
2013-05-03 18:49:15 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
uiClient := &Ui{client}
|
|
|
|
|
2013-05-21 14:58:14 -04:00
|
|
|
// Basic error and say tests
|
2013-06-15 21:24:38 -04:00
|
|
|
result, err := uiClient.Ask("query")
|
|
|
|
assert.Nil(err, "should not error")
|
2013-06-14 18:17:03 -04:00
|
|
|
assert.True(ui.askCalled, "ask should be called")
|
|
|
|
assert.Equal(ui.askQuery, "query", "should be correct")
|
|
|
|
assert.Equal(result, "foo", "should have correct result")
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
uiClient.Error("message")
|
|
|
|
assert.Equal(ui.errorMessage, "message", "message should be correct")
|
2013-05-08 18:12:48 -04:00
|
|
|
|
2013-06-03 14:30:38 -04:00
|
|
|
uiClient.Message("message")
|
|
|
|
assert.Equal(ui.messageMessage, "message", "message should be correct")
|
|
|
|
|
2013-05-27 18:12:48 -04:00
|
|
|
uiClient.Say("message")
|
|
|
|
assert.Equal(ui.sayMessage, "message", "message should be correct")
|
2013-05-03 18:49:15 -04:00
|
|
|
}
|