61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
package msmsg
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
var TheTestObjects = map[string]Message{
|
|
"<dialect ch= '0'>This message is opening this channel.</dialect>": {
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
Body: "This message is opening this channel.",
|
|
},
|
|
"<dialect ch ='0'></dialect>": {
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
},
|
|
"<dialect ch='0'/>": {
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
ClosesChannel: true,
|
|
},
|
|
"<dialect ch = '0' ctrl='shutdown' />": {
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
ClosesChannel: true,
|
|
Parameters: map[string]string{
|
|
"ctrl": "shutdown",
|
|
},
|
|
},
|
|
"<dialect>Hello</dialect>": {Dialect: "dialect", Body: "Hello"},
|
|
|
|
"this is chatter": {
|
|
Body: "this is chatter",
|
|
},
|
|
// these are malformed
|
|
"<dialect ch=10 />": {Body: "<dialect ch=10 />"},
|
|
"<dia": {Body: "<dia"},
|
|
"<dialect>Hello": {Body: "<dialect>Hello"},
|
|
"<dialect>Hello<dialect>": {Body: "<dialect>Hello<dialect>"},
|
|
"<dialect>Hello<dict>": {Body: "<dialect>Hello<dict>"},
|
|
"<dialect>Hello<dial ect>": {Body: "<dialect>Hello<dial ect>"},
|
|
}
|
|
|
|
func TestParseMessage(t *testing.T) {
|
|
for input, wantOutput := range TheTestObjects {
|
|
gotOutput := ParseMessage(input)
|
|
if want, got := wantOutput.Dialect, gotOutput.Dialect; want != got {
|
|
t.Fatalf("error parsing %s: wanted dialect %q; got %q\n", input, want, got)
|
|
} else if want, got := wantOutput.ID, gotOutput.ID; want != got {
|
|
t.Fatalf("error parsing %s: wanted ID %q; got %q\n", input, want, got)
|
|
} else if want, got := wantOutput.Body, gotOutput.Body; want != got {
|
|
t.Fatalf("error parsing %s: wanted body %q; got %q\n", input, want, got)
|
|
} else if !reflect.DeepEqual(wantOutput.Parameters, gotOutput.Parameters) {
|
|
t.Fatalf("error parsing %s: wanted parameters %+v; got %+v\n", input, wantOutput.Parameters, gotOutput.Parameters)
|
|
} else if want, got := wantOutput.ClosesChannel, gotOutput.ClosesChannel; want != got {
|
|
t.Fatalf("error parsing %s: wanted ClosesChannel=%v; got %v\n", input, want, got)
|
|
}
|
|
}
|
|
}
|