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{ "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.", }, "": { Message: Message{ Dialect: "dialect", ID: "0", }, NormalizedInput: "", }, "": { Message: Message{ Dialect: "dialect", ID: "0", ClosesChannel: true, }, NormalizedInput: "", }, "": { Message: Message{ Dialect: "dialect", ID: "0", ClosesChannel: true, Parameters: map[string]string{ "ctrl": "shutdown", }, }, NormalizedInput: "", }, "Hello": { Message: Message{Dialect: "dialect", Body: "Hello"}, NormalizedInput: "Hello", }, "this is chatter": { Message: Message{Body: "this is chatter"}, NormalizedInput: "this is chatter", }, // // test anonymous messages // ``: { // }, // these are malformed "": { 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) { 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) } }