src.dualinventive.com/go/lib/cp3000/cp3000_test.go

97 lines
2.4 KiB
Go

package cp3000
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSend(t *testing.T) {
var buffer bytes.Buffer
assert.Nil(t, Send(&buffer, "SENDSMS", "0655866244", "I still know what you did last summer"))
assert.Equal(t, "$SENDSMS,\"0655866244\",\"I still know what you did last summer\"*5C\n", buffer.String())
buffer.Reset()
assert.Nil(t, Send(&buffer, "SENDSMS", "0629584133", "I still know what you did last summer"))
assert.Equal(t, "$SENDSMS,\"0629584133\",\"I still know what you did last summer\"*55\n", buffer.String())
buffer.Reset()
assert.Nil(t, Send(&buffer, "SENDSMS", "0629584133", "Rik is cool"))
assert.Equal(t, "$SENDSMS,\"0629584133\",\"Rik is cool\"*11\n", buffer.String())
}
func TestDecode(t *testing.T) {
testcmds := []struct {
input string
cmd Command
args []string
t Type
}{
{
input: `$SENDSMS,"+31627151473","Project 'RC functional test' has been returned"*6E`,
cmd: CommandSendSMS,
args: []string{"+31627151473", "Project 'RC functional test' has been returned"},
t: TypeCommand,
},
{
input: `$WD*13`,
cmd: CommandWatchdog,
args: []string{},
t: TypeCommand,
},
{
input: `!00*00`,
cmd: Command("00"),
args: []string{},
t: TypeReply,
},
{
input: `$RETR,hallo*5B`,
cmd: CommandRetrieve,
args: []string{"hallo"},
t: TypeCommand,
},
{
input: `$SENDSMS,"+31627151473","Periode 'A' \"van\" project 'RC functional test' is gedeactiveerd"*2D`,
cmd: CommandSendSMS,
args: []string{"+31627151473", "Periode 'A' \"van\" project 'RC functional test' is gedeactiveerd"},
t: TypeCommand,
},
}
for _, cmd := range testcmds {
msg, err := Decode(cmd.input)
assert.Nil(t, err)
assert.Equal(t, cmd.cmd, msg.Command)
assert.Equal(t, cmd.t, msg.Type)
assert.Equal(t, len(cmd.args), len(msg.Params))
assert.ElementsMatch(t, cmd.args, msg.Params)
}
}
func TestDecodeError(t *testing.T) {
testcmds := []struct {
input string
err string
}{
{
input: `#SENDSMS,"+31627151473","Project 'RC functional test' has been returned"*6E`,
err: "no cmd start: #",
},
{
input: `$WD*14`,
err: "checksum mismatch (recv 14, calc 13)",
},
{
input: `$SENDSMS,"+31627151473","Perd*2D`,
err: "incomplete message",
},
}
for _, cmd := range testcmds {
msg, err := Decode(cmd.input)
assert.NotNil(t, err)
assert.Nil(t, msg)
assert.Equal(t, cmd.err, err.Error())
}
}