msmsg/message_test.go

136 lines
3.9 KiB
Go
Raw Normal View History

package msmsg
import (
2026-07-22 20:02:13 -04:00
"math/rand"
"reflect"
"testing"
)
2026-07-22 12:47:50 -04:00
type MessageWithNormalizedInput struct {
Message
NormalizedInput string
}
2026-07-22 20:02:13 -04:00
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]
}
2026-07-22 12:47:50 -04:00
var TheTestObjects = map[string]MessageWithNormalizedInput{
"<dialect ch= '0'>This message is opening this channel.</dialect>": {
2026-07-22 12:47:50 -04:00
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>": {
2026-07-22 12:47:50 -04:00
Message: Message{
Dialect: "dialect",
ID: "0",
},
NormalizedInput: "<dialect ch=\"0\"></dialect>",
},
"<dialect ch='0'/>": {
2026-07-22 12:47:50 -04:00
Message: Message{
Dialect: "dialect",
ID: "0",
ClosesChannel: true,
},
NormalizedInput: "<dialect ch=\"0\" />",
},
"<dialect ch = '0' ctrl='shutdown' />": {
2026-07-22 12:47:50 -04:00
Message: Message{
Dialect: "dialect",
ID: "0",
ClosesChannel: true,
Parameters: map[string]string{
"ctrl": "shutdown",
},
},
2026-07-22 12:47:50 -04:00
NormalizedInput: "<dialect ch=\"0\" ctrl=\"shutdown\" />",
},
"<dialect>Hello</dialect>": {
Message: Message{Dialect: "dialect", Body: "Hello"},
NormalizedInput: "<dialect>Hello</dialect>",
},
"this is chatter": {
2026-07-22 12:47:50 -04:00
Message: Message{Body: "this is chatter"},
NormalizedInput: "this is chatter",
},
2026-07-22 12:47:50 -04:00
// // test anonymous messages
// `<dialect ctrl="heartbeat"></dialect>`: {
// },
// these are malformed
2026-07-22 12:47:50 -04:00
"<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)
2026-07-22 12:47:50 -04:00
} 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)
}
}
}
2026-07-22 20:02:13 -04:00
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)
}
}