diff --git a/client/commands.go b/client/commands.go index 88587fa..40febe1 100644 --- a/client/commands.go +++ b/client/commands.go @@ -30,16 +30,10 @@ const ( WHOIS = "WHOIS" ) -// this file contains the various commands you can -// send to the server using an Conn connection - -// This could be a lot less ugly with the ability to manipulate -// the symbol table and add methods/functions on the fly -// [ CMD, FMT, FMTARGS ] etc. - +// cutNewLines() pares down a string to the part before the first "\r" or "\n" func cutNewLines(s string) string { - r := strings.SplitN(s, "\\r", 2) - r = strings.SplitN(r[0], "\\n", 2) + r := strings.SplitN(s, "\r", 2) + r = strings.SplitN(r[0], "\n", 2) return r[0] } diff --git a/client/commands_test.go b/client/commands_test.go index c3bb6ee..65d5948 100644 --- a/client/commands_test.go +++ b/client/commands_test.go @@ -2,6 +2,25 @@ package client import "testing" +func TestCutNewLines(t *testing.T) { + tests := []struct{ in, out string }{ + {"", ""}, + {"foo bar", "foo bar"}, + {"foo bar\rbaz", "foo bar"}, + {"foo bar\nbaz", "foo bar"}, + {"blorp\r\n\r\nbloop", "blorp"}, + {"\n\rblaap", ""}, + {"\r\n", ""}, + {"boo\\r\\n\\n\r", "boo\\r\\n\\n"}, + } + for i, test := range tests { + out := cutNewLines(test.in) + if test.out != out { + t.Errorf("test %d: expected '%s', got '%s'", i, test.out, out) + } + } +} + func TestClientCommands(t *testing.T) { c, s := setUp(t) defer s.tearDown()