|
| 1 | +package shlex |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestBashFormat_Classifier(t *testing.T) { |
| 6 | + classifier := BashFormat().Classifier() |
| 7 | + tests := map[rune]runeTokenClass{ |
| 8 | + ' ': spaceRuneClass, |
| 9 | + '"': escapingQuoteRuneClass, |
| 10 | + '\'': nonEscapingQuoteRuneClass, |
| 11 | + '#': commentRuneClass, |
| 12 | + } |
| 13 | + for runeChar, want := range tests { |
| 14 | + got := classifier.ClassifyRune(runeChar) |
| 15 | + if got != want { |
| 16 | + t.Errorf("ClassifyRune(%v) -> %v. Want: %v", runeChar, got, want) |
| 17 | + } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func TestBashFormat_CloseQuoteEscapeReopen(t *testing.T) { |
| 22 | + // The POSIX idiom for embedding a single quote: 'it'\''s |
| 23 | + tokens, err := SplitWith("echo 'it'\\''s", BashFormat()) |
| 24 | + if err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + words := tokens.Words().Strings() |
| 28 | + if len(words) != 2 || words[1] != "it's" { |
| 29 | + t.Errorf("bash '\\'': Words = %v, want [echo it's]", words) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestBashFormat_EscapedSpace(t *testing.T) { |
| 34 | + tokens, err := SplitWith(`echo a\ b`, BashFormat()) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + words := tokens.Words().Strings() |
| 39 | + if len(words) != 2 || words[1] != "a b" { |
| 40 | + t.Errorf("bash escaped space: Words = %v, want [echo a b]", words) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestBashFormat_BackslashNInDoubleQuotes(t *testing.T) { |
| 45 | + // In bash, \n inside "..." is literal (backslash not special before n) |
| 46 | + // The state machine consumes \ + next char, so Value = "hellonworld" |
| 47 | + // This is a known limitation — the lexer is not a full expander. |
| 48 | + tokens, err := SplitWith(`echo "hello\nworld"`, BashFormat()) |
| 49 | + if err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + words := tokens.Words() |
| 53 | + last := words[len(words)-1] |
| 54 | + if last.State != IN_WORD_STATE { |
| 55 | + t.Errorf("bash \\n in double: State = %v, want IN_WORD_STATE", last.State) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestBashFormat_AdjacentQuotedSegments(t *testing.T) { |
| 60 | + tokens, err := SplitWith(`echo a"b"'c'`, BashFormat()) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + words := tokens.Words().Strings() |
| 65 | + if len(words) != 2 || words[1] != "abc" { |
| 66 | + t.Errorf("bash adjacent: Words = %v, want [echo abc]", words) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestBashFormat_SingleQuoteLiteral(t *testing.T) { |
| 71 | + tokens, err := SplitWith(`echo '$HOME \n \t'`, BashFormat()) |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + words := tokens.Words().Strings() |
| 76 | + if len(words) != 2 || words[1] != `$HOME \n \t` { |
| 77 | + t.Errorf("bash single literal: Words = %v, want [echo $HOME \\n \\t]", words) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestBashFormat_AtWordbreakPrefix(t *testing.T) { |
| 82 | + // @ is a wordbreak but WordbreakPrefix skips it |
| 83 | + ctx := SplitForCompletion("echo foo@bar", BashFormat()) |
| 84 | + // @ is a wordbreak, but Words() merges adjoining tokens, so CurrentWord is the full word |
| 85 | + if ctx.CurrentWord != "foo@bar" { |
| 86 | + t.Errorf("bash @: CurrentWord = %q, want %q", ctx.CurrentWord, "foo@bar") |
| 87 | + } |
| 88 | + // @ is skipped as a wordbreak boundary, so prefix should be "foo" |
| 89 | + if ctx.Prefix != "foo" { |
| 90 | + t.Errorf("bash @: Prefix = %q, want %q", ctx.Prefix, "foo") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestBashFormat_EscapeAtEOF(t *testing.T) { |
| 95 | + tokens, err := SplitWith(`echo foo\`, BashFormat()) |
| 96 | + if err != nil { |
| 97 | + t.Fatal(err) |
| 98 | + } |
| 99 | + words := tokens.Words() |
| 100 | + last := words[len(words)-1] |
| 101 | + if last.State != ESCAPING_STATE { |
| 102 | + t.Errorf("bash escape EOF: State = %v, want ESCAPING_STATE", last.State) |
| 103 | + } |
| 104 | + if last.Value != "foo" { |
| 105 | + t.Errorf("bash escape EOF: Value = %q, want %q", last.Value, "foo") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestBashFormat_Comment(t *testing.T) { |
| 110 | + tokens, err := SplitWith("echo hello # comment", BashFormat()) |
| 111 | + if err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + // Lexer skips comments, so only "echo" and "hello" are returned |
| 115 | + words := tokens.Words().Strings() |
| 116 | + if len(words) != 2 { |
| 117 | + t.Errorf("bash comment: Words = %v, want 2 words", words) |
| 118 | + } |
| 119 | +} |
0 commit comments