if err := sendCommand("/ignore test", ignorer, ch, &buffer); err != nil {
t.Fatal(err)
}
- expectOutput(t, buffer, "-> Err: user not found: test"+message.Newline)
+ expected := "-> Err: user not found: test"+message.Newline
+ expectOutput(t, buffer, expected)
// test ignoring existing user
if err := sendCommand("/ignore "+ignored.user.Name(), ignorer, ch, &buffer); err != nil {
expectOutput(t, buffer, "-> Err: user not found"+message.Newline)
// test muting by non-op
+ other.screen.Read(&buffer)
if err := sendCommand("/mute "+muted.user.Name(), other, ch, &buffer); err != nil {
t.Fatal(err)
}
func expectOutput(t *testing.T, buffer []byte, expected string) {
t.Helper()
- bytes := []byte(expected)
- if !reflect.DeepEqual(buffer, bytes) {
- t.Errorf("Got: %q; Expected: %q", buffer, expected)
+ s := string(buffer)
+ // Just check the suffix instead of jumping through hoops.
+ if len(s) < len(expected) || s[len(s)-len(expected):] != expected {
+ t.Errorf("Got: %q; Expected: %q", s[len(s)-len(expected):], expected)
}
}
u.HandleMsg(u.ConsumeOne())
expected = []byte(" * foo joined. (Connected: 1)" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
ch.Send(message.NewSystemMsg("hello", u))
u.HandleMsg(u.ConsumeOne())
expected = []byte("-> hello" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
ch.Send(message.ParseInput("/me says hello.", u))
u.HandleMsg(u.ConsumeOne())
expected = []byte("** foo says hello." + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
}
func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) {
u.HandleMsg(u.ConsumeOne())
expected = []byte(" * foo joined. (Connected: 1)" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
ch.Send(message.ParseInput("/quiet", u))
u.HandleMsg(u.ConsumeOne())
expected = []byte("-> Quiet mode is toggled ON" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
ch.Send(message.ParseInput("/quiet", u))
u.HandleMsg(u.ConsumeOne())
expected = []byte("-> Quiet mode is toggled OFF" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
}
func TestRoomNames(t *testing.T) {
u.HandleMsg(u.ConsumeOne())
expected = []byte(" * foo joined. (Connected: 1)" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
ch.Send(message.ParseInput("/names", u))
u.HandleMsg(u.ConsumeOne())
expected = []byte("-> 1 connected: foo" + message.Newline)
s.Read(&actual)
- if !reflect.DeepEqual(actual, expected) {
- t.Errorf("Got: %q; Expected: %q", actual, expected)
- }
+ expectOutput(t, actual, string(expected))
}
func TestRoomNamesPrefix(t *testing.T) {
func stripPrompt(s string) string {
// FIXME: Is there a better way to do this?
- if endPos := strings.Index(s, "\x1b[K "); endPos > 0 {
- return s[endPos+3:]
- }
- if endPos := strings.Index(s, "\x1b[2K "); endPos > 0 {
- return s[endPos+4:]
+ if endPos := strings.Index(s, " * "); endPos > 0 {
+ return s[endPos:]
}
- if endPos := strings.Index(s, "\x1b[K-> "); endPos > 0 {
- return s[endPos+6:]
+ if endPos := strings.Index(s, "-> "); endPos > 0 {
+ return s[endPos+3:]
}
if endPos := strings.Index(s, "] "); endPos > 0 {
return s[endPos+2:]
}
- if strings.HasPrefix(s, "-> ") {
- return s[3:]
- }
return s
}
-func TestStripPrompt(t *testing.T) {
- tests := []struct {
- Input string
- Want string
- }{
- {
- Input: "\x1b[A\x1b[2K[quux] hello",
- Want: "hello",
- },
- {
- Input: "[foo] \x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[K * Guest1 joined. (Connected: 2)\r",
- Want: " * Guest1 joined. (Connected: 2)\r",
- },
- {
- Input: "[foo] \x1b[6D\x1b[K-> From your friendly system.\r",
- Want: "From your friendly system.\r",
- },
- {
- Input: "-> Err: must be op.\r",
- Want: "Err: must be op.\r",
- },
- }
-
- for i, tc := range tests {
- if got, want := stripPrompt(tc.Input), tc.Want; got != want {
- t.Errorf("case #%d:\n got: %q\nwant: %q", i, got, want)
- }
- }
-}
-
func TestHostGetPrompt(t *testing.T) {
var expected, actual string
Theme: &message.Themes[0],
})
actual = GetPrompt(u)
- expected = "[\033[38;05;88mfoo\033[0m] "
+ expected = "[foo] "
if actual != expected {
t.Errorf("Invalid host prompt:\n Got: %q;\nWant: %q", actual, expected)
}
input string
timeformat *string
}{
- {"", strptr("15:04")},
+ {"", strptr("2006-01-02 15:04:05")},
{"1", strptr("15:04")},
{"0", nil},
{"time +8h", strptr("15:04")},