2020-08-11 20:44:10 +00:00
|
|
|
|
package shellwords
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"reflect"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type wordsTest struct {
|
|
|
|
|
line string
|
|
|
|
|
args []string
|
|
|
|
|
doErr bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
|
var tests = []wordsTest{
|
|
|
|
|
{
|
2020-12-20 02:02:31 +00:00
|
|
|
|
"",
|
|
|
|
|
nil,
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"'",
|
|
|
|
|
nil,
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`this is a "te""st"`,
|
2020-08-11 20:44:10 +00:00
|
|
|
|
[]string{"this", "is", "a", "test"},
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`hanging "quote`,
|
|
|
|
|
[]string{"hanging", "quote"},
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
`Hello, 世界`,
|
|
|
|
|
[]string{"Hello,", "世界"},
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"this is `inline code`",
|
|
|
|
|
[]string{"this", "is", "inline code"},
|
|
|
|
|
false,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"how about a ```go\npackage main\n```\ngo code?",
|
|
|
|
|
[]string{"how", "about", "a", "go\npackage main\n", "go", "code?"},
|
|
|
|
|
false,
|
|
|
|
|
},
|
2020-08-30 05:09:52 +00:00
|
|
|
|
{
|
|
|
|
|
"this should not crash `",
|
|
|
|
|
[]string{"this", "should", "not", "crash"},
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"this should not crash '",
|
|
|
|
|
[]string{"this", "should", "not", "crash"},
|
|
|
|
|
true,
|
|
|
|
|
},
|
2021-01-15 00:23:14 +00:00
|
|
|
|
{
|
|
|
|
|
"iPhone “double quoted” text",
|
|
|
|
|
[]string{"iPhone", "double quoted", "text"},
|
|
|
|
|
true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"iPhone ‘single quoted’ text",
|
|
|
|
|
[]string{"iPhone", "single quoted", "text"},
|
|
|
|
|
true,
|
|
|
|
|
},
|
2020-08-11 20:44:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
w, err := Parse(test.line)
|
|
|
|
|
if err != nil && !test.doErr {
|
|
|
|
|
t.Errorf("Error at %q: %v", test.line, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(w, test.args) {
|
|
|
|
|
t.Errorf("Inequality:\n%#v !=\n%#v", w, test.args)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|