waveterm/pkg/shparse/shparse_test.go

35 lines
773 B
Go
Raw Normal View History

2022-11-14 22:56:28 +01:00
package shparse
import (
2022-11-15 04:57:29 +01:00
"fmt"
2022-11-14 22:56:28 +01:00
"testing"
)
// $(ls f[*]); ./x
2022-11-15 04:57:29 +01:00
// ls f => raw["ls f"] -> lit["ls f"] -> lit["ls"] lit["f"]
2022-11-14 22:56:28 +01:00
// w; ls foo; => raw["w; ls foo;"]
// ls&"ls" => raw["ls&ls"] => lit["ls&"] dq["ls"] => lit["ls"] key["&"] dq["ls"]
// ls $x; echo `ls f => raw["ls $x; echo `ls f"]
// > echo $foo{x,y}
func testParse(t *testing.T, s string) {
2022-11-15 09:36:30 +01:00
words := Tokenize(s)
2022-11-15 04:57:29 +01:00
fmt.Printf("%s\n", s)
2022-11-15 09:36:30 +01:00
dumpWords(words, " ")
2022-11-15 04:57:29 +01:00
fmt.Printf("\n")
2022-11-15 09:39:53 +01:00
outStr := wordsToStr(words)
if outStr != s {
t.Errorf("tokenization output does not match input: %q => %q", s, outStr)
}
2022-11-14 22:56:28 +01:00
}
func Test1(t *testing.T) {
testParse(t, "ls")
testParse(t, "ls 'foo'")
testParse(t, `ls "hello" $'\''`)
2022-11-15 04:57:29 +01:00
testParse(t, `ls "foo`)
testParse(t, `echo $11 $xyz $ `)
2022-11-14 22:56:28 +01:00
}