enhancing test coverage
This commit is contained in:
parent
f20fcb466d
commit
800d55deec
18
message.go
18
message.go
@ -20,7 +20,25 @@ func (m Message) String() string {
|
|||||||
return m.Body
|
return m.Body
|
||||||
}
|
}
|
||||||
var builder strings.Builder
|
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()
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,41 +5,83 @@ import (
|
|||||||
"testing"
|
"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>": {
|
"<dialect ch= '0'>This message is opening this channel.</dialect>": {
|
||||||
Dialect: "dialect",
|
Message: Message{
|
||||||
ID: "0",
|
Dialect: "dialect",
|
||||||
Body: "This message is opening this channel.",
|
ID: "0",
|
||||||
|
Body: "This message is opening this channel.",
|
||||||
|
},
|
||||||
|
NormalizedInput: "<dialect ch=\"0\">This message is opening this channel.</dialect>",
|
||||||
},
|
},
|
||||||
"<dialect ch ='0'></dialect>": {
|
"<dialect ch ='0'></dialect>": {
|
||||||
Dialect: "dialect",
|
Message: Message{
|
||||||
ID: "0",
|
Dialect: "dialect",
|
||||||
|
ID: "0",
|
||||||
|
},
|
||||||
|
NormalizedInput: "<dialect ch=\"0\"></dialect>",
|
||||||
},
|
},
|
||||||
"<dialect ch='0'/>": {
|
"<dialect ch='0'/>": {
|
||||||
Dialect: "dialect",
|
Message: Message{
|
||||||
ID: "0",
|
Dialect: "dialect",
|
||||||
ClosesChannel: true,
|
ID: "0",
|
||||||
|
ClosesChannel: true,
|
||||||
|
},
|
||||||
|
NormalizedInput: "<dialect ch=\"0\" />",
|
||||||
},
|
},
|
||||||
"<dialect ch = '0' ctrl='shutdown' />": {
|
"<dialect ch = '0' ctrl='shutdown' />": {
|
||||||
Dialect: "dialect",
|
Message: Message{
|
||||||
ID: "0",
|
Dialect: "dialect",
|
||||||
ClosesChannel: true,
|
ID: "0",
|
||||||
Parameters: map[string]string{
|
ClosesChannel: true,
|
||||||
"ctrl": "shutdown",
|
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>",
|
||||||
},
|
},
|
||||||
"<dialect>Hello</dialect>": {Dialect: "dialect", Body: "Hello"},
|
|
||||||
|
|
||||||
"this is chatter": {
|
"this is chatter": {
|
||||||
Body: "this is chatter",
|
Message: Message{Body: "this is chatter"},
|
||||||
|
NormalizedInput: "this is chatter",
|
||||||
},
|
},
|
||||||
|
// // test anonymous messages
|
||||||
|
// `<dialect ctrl="heartbeat"></dialect>`: {
|
||||||
|
|
||||||
|
// },
|
||||||
// these are malformed
|
// these are malformed
|
||||||
"<dialect ch=10 />": {Body: "<dialect ch=10 />"},
|
"<dialect ch=10 />": {
|
||||||
"<dia": {Body: "<dia"},
|
Message: Message{Body: "<dialect ch=10 />"},
|
||||||
"<dialect>Hello": {Body: "<dialect>Hello"},
|
NormalizedInput: "<dialect ch=10 />",
|
||||||
"<dialect>Hello<dialect>": {Body: "<dialect>Hello<dialect>"},
|
},
|
||||||
"<dialect>Hello<dict>": {Body: "<dialect>Hello<dict>"},
|
"<dia": {
|
||||||
"<dialect>Hello<dial ect>": {Body: "<dialect>Hello<dial ect>"},
|
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) {
|
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)
|
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 {
|
} else if want, got := wantOutput.ClosesChannel, gotOutput.ClosesChannel; want != got {
|
||||||
t.Fatalf("error parsing %s: wanted ClosesChannel=%v; got %v\n", input, 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user