enhancing test coverage

This commit is contained in:
William Dillon 2026-07-22 12:47:50 -04:00
parent f20fcb466d
commit 800d55deec
2 changed files with 87 additions and 23 deletions

View File

@ -20,7 +20,25 @@ func (m Message) String() string {
return m.Body
}
var builder strings.Builder
if !m.Chatter() {
builder.WriteString(fmt.Sprintf("<%s", m.Dialect))
if len(m.ID) > 0 {
builder.WriteString(fmt.Sprintf(" %s=%q", "ch", m.ID))
}
for k, v := range m.Parameters {
builder.WriteString(fmt.Sprintf(" %s=%q", k, v))
}
if len(m.Body) > 0 {
builder.WriteString(fmt.Sprintf(">%s</%s>", m.Body, m.Dialect))
} else if m.ClosesChannel {
builder.WriteString(" />")
} else {
builder.WriteString(fmt.Sprintf("></%s>", m.Dialect))
}
} else {
builder.WriteString(m.Body)
}
return builder.String()
}

View File

@ -5,22 +5,37 @@ import (
"testing"
)
var TheTestObjects = map[string]Message{
type MessageWithNormalizedInput struct {
Message
NormalizedInput string
}
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,
@ -28,18 +43,45 @@ var TheTestObjects = map[string]Message{
"ctrl": "shutdown",
},
},
"<dialect>Hello</dialect>": {Dialect: "dialect", Body: "Hello"},
"this is chatter": {
Body: "this is chatter",
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 />": {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>"},
"<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) {
@ -55,6 +97,10 @@ func TestParseMessage(t *testing.T) {
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)
}
}
}