1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "testing"
|
---|
5 | )
|
---|
6 |
|
---|
7 | func assertSplit(t *testing.T, input string, expected []string) {
|
---|
8 | actual, err := splitWords(input)
|
---|
9 | if err != nil {
|
---|
10 | t.Errorf("%q: %v", input, err)
|
---|
11 | return
|
---|
12 | }
|
---|
13 | if len(actual) != len(expected) {
|
---|
14 | t.Errorf("%q: expected %d words, got %d\nexpected: %v\ngot: %v", input, len(expected), len(actual), expected, actual)
|
---|
15 | return
|
---|
16 | }
|
---|
17 | for i := 0; i < len(actual); i++ {
|
---|
18 | if actual[i] != expected[i] {
|
---|
19 | t.Errorf("%q: expected word #%d to be %q, got %q\nexpected: %v\ngot: %v", input, i, expected[i], actual[i], expected, actual)
|
---|
20 | }
|
---|
21 | }
|
---|
22 | }
|
---|
23 |
|
---|
24 | func TestSplit(t *testing.T) {
|
---|
25 | assertSplit(t, " ch 'up' #soju 'relay'-det\"ache\"d message ", []string{
|
---|
26 | "ch",
|
---|
27 | "up",
|
---|
28 | "#soju",
|
---|
29 | "relay-detached",
|
---|
30 | "message",
|
---|
31 | })
|
---|
32 | assertSplit(t, "net update \\\"free\\\"node -pass 'political \"stance\" desu!' -realname '' -nick lee", []string{
|
---|
33 | "net",
|
---|
34 | "update",
|
---|
35 | "\"free\"node",
|
---|
36 | "-pass",
|
---|
37 | "political \"stance\" desu!",
|
---|
38 | "-realname",
|
---|
39 | "",
|
---|
40 | "-nick",
|
---|
41 | "lee",
|
---|
42 | })
|
---|
43 | assertSplit(t, "Omedeto,\\ Yui! ''", []string{
|
---|
44 | "Omedeto, Yui!",
|
---|
45 | "",
|
---|
46 | })
|
---|
47 |
|
---|
48 | if _, err := splitWords("end of 'file"); err == nil {
|
---|
49 | t.Errorf("expected error on unterminated single quote")
|
---|
50 | }
|
---|
51 | if _, err := splitWords("end of backquote \\"); err == nil {
|
---|
52 | t.Errorf("expected error on unterminated backquote sequence")
|
---|
53 | }
|
---|
54 | }
|
---|