136 lines
3.9 KiB
Go
136 lines
3.9 KiB
Go
package msmsg
|
|
|
|
import (
|
|
"math/rand"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
type MessageWithNormalizedInput struct {
|
|
Message
|
|
NormalizedInput string
|
|
}
|
|
|
|
var TheTestObjectsKeys = func() []string {
|
|
results := make([]string, 0, len(TheTestObjects))
|
|
for key := range TheTestObjects {
|
|
results = append(results, key)
|
|
}
|
|
return results
|
|
}()
|
|
|
|
func RandomTestObject() MessageWithNormalizedInput {
|
|
key := TheTestObjectsKeys[rand.Intn(len(TheTestObjectsKeys))]
|
|
return TheTestObjects[key]
|
|
|
|
}
|
|
|
|
var TheTestObjects = map[string]MessageWithNormalizedInput{
|
|
"<dialect ch= '0'>This message is opening this channel.</dialect>": {
|
|
Message: Message{
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
Body: "This message is opening this channel.",
|
|
},
|
|
NormalizedInput: "<dialect ch=\"0\">This message is opening this channel.</dialect>",
|
|
},
|
|
"<dialect ch ='0'></dialect>": {
|
|
Message: Message{
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
},
|
|
NormalizedInput: "<dialect ch=\"0\"></dialect>",
|
|
},
|
|
"<dialect ch='0'/>": {
|
|
Message: Message{
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
ClosesChannel: true,
|
|
},
|
|
NormalizedInput: "<dialect ch=\"0\" />",
|
|
},
|
|
"<dialect ch = '0' ctrl='shutdown' />": {
|
|
Message: Message{
|
|
Dialect: "dialect",
|
|
ID: "0",
|
|
ClosesChannel: true,
|
|
Parameters: map[string]string{
|
|
"ctrl": "shutdown",
|
|
},
|
|
},
|
|
NormalizedInput: "<dialect ch=\"0\" ctrl=\"shutdown\" />",
|
|
},
|
|
"<dialect>Hello</dialect>": {
|
|
Message: Message{Dialect: "dialect", Body: "Hello"},
|
|
NormalizedInput: "<dialect>Hello</dialect>",
|
|
},
|
|
"this is chatter": {
|
|
Message: Message{Body: "this is chatter"},
|
|
NormalizedInput: "this is chatter",
|
|
},
|
|
// // test anonymous messages
|
|
// `<dialect ctrl="heartbeat"></dialect>`: {
|
|
|
|
// },
|
|
// these are malformed
|
|
"<dialect ch=10 />": {
|
|
Message: Message{Body: "<dialect ch=10 />"},
|
|
NormalizedInput: "<dialect ch=10 />",
|
|
},
|
|
"<dia": {
|
|
Message: Message{Body: "<dia"},
|
|
NormalizedInput: "<dia",
|
|
},
|
|
"<dialect>Hello": {
|
|
Message: Message{Body: "<dialect>Hello"},
|
|
NormalizedInput: "<dialect>Hello",
|
|
},
|
|
"<dialect>Hello<dialect>": {
|
|
Message: Message{Body: "<dialect>Hello<dialect>"},
|
|
NormalizedInput: "<dialect>Hello<dialect>",
|
|
},
|
|
"<dialect>Hello<dict>": {
|
|
Message: Message{Body: "<dialect>Hello<dict>"},
|
|
NormalizedInput: "<dialect>Hello<dict>",
|
|
},
|
|
"<dialect>Hello<dial ect>": {
|
|
Message: Message{Body: "<dialect>Hello<dial ect>"},
|
|
NormalizedInput: "<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)
|
|
} else if want, got := wantOutput.NormalizedInput, gotOutput.String(); want != got {
|
|
t.Fatalf("error parsing %s: wanted %s; got %s\n", input, want, got)
|
|
} else if want, got := wantOutput.Anonymous(), gotOutput.Anonymous(); want != got {
|
|
t.Fatalf("error parsing %s: wanted anonymous=%v; got %v\n", input, want, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkMessageClone(b *testing.B) {
|
|
for b.Loop() {
|
|
msg := RandomTestObject()
|
|
msg.Message.Clone()
|
|
}
|
|
}
|
|
|
|
func BenchmarkMessageParse(b *testing.B) {
|
|
for b.Loop() {
|
|
msg := RandomTestObject()
|
|
ParseMessage(msg.NormalizedInput)
|
|
}
|
|
}
|