MCPcopy Create free account
hub / github.com/dim-an/cod / TestTokenizeStrings

Function TestTokenizeStrings

shells/tokenize_test.go:23–76  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

21)
22
23func TestTokenizeStrings(t *testing.T) {
24 check := func(cmd string, expectedArgs []string) {
25 var expectedBroken []bool
26 for range expectedArgs {
27 expectedBroken = append(expectedBroken, false)
28 }
29
30 tokens, err := Tokenize(cmd)
31 var args []string
32 var broken []bool
33 for _, t := range tokens {
34 args = append(args, t.Decoded)
35 broken = append(broken, t.IsBroken)
36 }
37
38 require.Nil(t, err)
39 require.Equal(t, expectedArgs, args)
40 require.Equal(t, expectedBroken, broken)
41 }
42
43 // word splitting
44 check(`echo foo bar`, []string{"echo", "foo", "bar"})
45 check(`echo foo bar`, []string{"echo", "foo", "bar"})
46 check("echo foo\tbar", []string{"echo", "foo", "bar"})
47
48 // backslash interpretation
49 check(`echo foo\ bar`, []string{"echo", "foo bar"})
50 check("echo foo\\\tbar", []string{"echo", "foo\tbar"})
51 check("echo foo\\\nbar", []string{"echo", "foobar"})
52 check(`echo foo\nbar`, []string{"echo", `foonbar`})
53
54 // single quotes
55 check(`echo 'foo bar'`, []string{"echo", "foo bar"})
56 check(`echo 'foo\''bar'`, []string{"echo", "foo\\bar"})
57 check(`echo 'foo\''bar'`, []string{"echo", "foo\\bar"})
58 check(`echo ''`, []string{"echo", ""})
59 check(`echo '' ''`, []string{"echo", "", ""})
60 check(`echo '' ''`, []string{"echo", "", ""})
61
62 // double quotes
63 check(`echo "foo bar"`, []string{"echo", "foo bar"})
64 check(`echo "foo \"bar\""`, []string{"echo", "foo \"bar\""})
65
66 // double quotes
67 check(`echo "foo bar"`, []string{"echo", "foo bar"})
68 check(`echo "foo \"bar\""`, []string{"echo", "foo \"bar\""})
69 check(`echo "foo \$bar"`, []string{"echo", "foo $bar"})
70 check(`echo "foo \\bar"`, []string{"echo", `foo \bar`})
71 check(`echo "foo \bar"`, []string{"echo", `foo \bar`})
72 check(`echo "foo \bar"`, []string{"echo", `foo \bar`})
73 check(`echo ""`, []string{"echo", ""})
74 check(`echo "" ""`, []string{"echo", "", ""})
75 check(`echo "" ""`, []string{"echo", "", ""})
76}

Callers

nothing calls this directly

Calls 1

TokenizeFunction · 0.85

Tested by

no test coverage detected