diff --git a/message.go b/message.go index 87ec26c..08cb0cd 100644 --- a/message.go +++ b/message.go @@ -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", m.Body, m.Dialect)) + } else if m.ClosesChannel { + builder.WriteString(" />") + } else { + builder.WriteString(fmt.Sprintf(">", m.Dialect)) + } + } else { + builder.WriteString(m.Body) + } return builder.String() } diff --git a/message_test.go b/message_test.go index f8fc7b0..855fd74 100644 --- a/message_test.go +++ b/message_test.go @@ -5,41 +5,83 @@ import ( "testing" ) -var TheTestObjects = map[string]Message{ +type MessageWithNormalizedInput struct { + Message + NormalizedInput string +} + +var TheTestObjects = map[string]MessageWithNormalizedInput{ "This message is opening this channel.": { - Dialect: "dialect", - ID: "0", - Body: "This message is opening this channel.", + Message: Message{ + Dialect: "dialect", + ID: "0", + Body: "This message is opening this channel.", + }, + NormalizedInput: "This message is opening this channel.", }, "": { - Dialect: "dialect", - ID: "0", + Message: Message{ + Dialect: "dialect", + ID: "0", + }, + NormalizedInput: "", }, "": { - Dialect: "dialect", - ID: "0", - ClosesChannel: true, + Message: Message{ + Dialect: "dialect", + ID: "0", + ClosesChannel: true, + }, + NormalizedInput: "", }, "": { - Dialect: "dialect", - ID: "0", - ClosesChannel: true, - Parameters: map[string]string{ - "ctrl": "shutdown", + Message: Message{ + Dialect: "dialect", + ID: "0", + ClosesChannel: true, + Parameters: map[string]string{ + "ctrl": "shutdown", + }, }, + NormalizedInput: "", + }, + "Hello": { + Message: Message{Dialect: "dialect", Body: "Hello"}, + NormalizedInput: "Hello", }, - "Hello": {Dialect: "dialect", Body: "Hello"}, - "this is chatter": { - Body: "this is chatter", + Message: Message{Body: "this is chatter"}, + NormalizedInput: "this is chatter", }, + // // test anonymous messages + // ``: { + + // }, // these are malformed - "": {Body: ""}, - "Hello": {Body: "Hello"}, - "Hello": {Body: "Hello"}, - "Hello": {Body: "Hello"}, - "Hello": {Body: "Hello"}, + "": { + Message: Message{Body: ""}, + NormalizedInput: "", + }, + "Hello": { + Message: Message{Body: "Hello"}, + NormalizedInput: "Hello", + }, + "Hello": { + Message: Message{Body: "Hello"}, + NormalizedInput: "Hello", + }, + "Hello": { + Message: Message{Body: "Hello"}, + NormalizedInput: "Hello", + }, + "Hello": { + Message: Message{Body: "Hello"}, + NormalizedInput: "Hello", + }, } 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) } } }