Fixed scanner tests.

This commit is contained in:
Zach Klippenstein 2015-07-19 06:43:13 -07:00
parent 34b3a07ca8
commit 946c9e8ff8
3 changed files with 18 additions and 6 deletions

View file

@ -94,3 +94,13 @@ func (err *Err) Error() string {
} }
return msg return msg
} }
// HasErrCode returns true if err is an *Err and err.Code == code.
func HasErrCode(err error, code ErrCode) bool {
switch err := err.(type) {
case *Err:
return err.Code == code
default:
return false
}
}

View file

@ -7,6 +7,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zach-klippenstein/goadb/util"
) )
func TestReadStatusOkay(t *testing.T) { func TestReadStatusOkay(t *testing.T) {
@ -20,7 +21,7 @@ func TestReadStatusOkay(t *testing.T) {
func TestReadIncompleteStatus(t *testing.T) { func TestReadIncompleteStatus(t *testing.T) {
s := NewScannerString("oka") s := NewScannerString("oka")
_, err := s.ReadStatus() _, err := s.ReadStatus()
assert.Equal(t, incompleteMessage("status", 3, 4), err) assert.Equal(t, errIncompleteMessage("status", 3, 4), err)
assertEof(t, s) assertEof(t, s)
} }
@ -35,7 +36,7 @@ func TestReadLength(t *testing.T) {
func TestReadIncompleteLength(t *testing.T) { func TestReadIncompleteLength(t *testing.T) {
s := NewScannerString("aaa") s := NewScannerString("aaa")
_, err := s.readLength() _, err := s.readLength()
assert.Equal(t, incompleteMessage("length", 3, 4), err) assert.Equal(t, errIncompleteMessage("length", 3, 4), err)
assertEof(t, s) assertEof(t, s)
} }
@ -78,7 +79,7 @@ func TestReadIncompleteMessage(t *testing.T) {
s := NewScannerString("0005hel") s := NewScannerString("0005hel")
msg, err := ReadMessageString(s) msg, err := ReadMessageString(s)
assert.Error(t, err) assert.Error(t, err)
assert.Equal(t, incompleteMessage("message data", 3, 5), err) assert.Equal(t, errIncompleteMessage("message data", 3, 5), err)
assert.Equal(t, "hel\000\000", msg) assert.Equal(t, "hel\000\000", msg)
assertEof(t, s) assertEof(t, s)
} }
@ -97,7 +98,7 @@ func NewEofBuffer(str string) *TestReader {
func assertEof(t *testing.T, s *realScanner) { func assertEof(t *testing.T, s *realScanner) {
msg, err := s.ReadMessage() msg, err := s.ReadMessage()
assert.Equal(t, io.EOF, err) assert.True(t, util.HasErrCode(err, util.NetworkError))
assert.Nil(t, msg) assert.Nil(t, msg)
} }

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zach-klippenstein/goadb/util"
) )
var ( var (
@ -35,7 +36,7 @@ func TestSyncSendOctetStringTooLong(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
s := NewSyncSender(&buf) s := NewSyncSender(&buf)
err := s.SendOctetString("hello") err := s.SendOctetString("hello")
assert.EqualError(t, err, "octet string must be exactly 4 bytes: 'hello'") assert.Equal(t, util.AssertionErrorf("octet string must be exactly 4 bytes: 'hello'"), err)
} }
func TestSyncReadTime(t *testing.T) { func TestSyncReadTime(t *testing.T) {
@ -63,7 +64,7 @@ func TestSyncReadString(t *testing.T) {
func TestSyncReadStringTooShort(t *testing.T) { func TestSyncReadStringTooShort(t *testing.T) {
s := NewSyncScanner(strings.NewReader("\005\000\000\000h")) s := NewSyncScanner(strings.NewReader("\005\000\000\000h"))
_, err := s.ReadString() _, err := s.ReadString()
assert.EqualError(t, err, "incomplete bytes: read 1 bytes, expecting 5") assert.Equal(t, errIncompleteMessage("bytes", 1, 5), err)
} }
func TestSyncSendString(t *testing.T) { func TestSyncSendString(t *testing.T) {