summaryrefslogtreecommitdiffstats
path: root/rsh_test.go
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2026-08-24 22:49:01 -0700
committerRose Hogenson <rosehogenson@posteo.net>2026-08-24 22:49:01 -0700
commitec47b533e2d748f6831f0fd2f68e93d0954c1d8f (patch)
tree3f456337a0fafbf51fda17ac52e2102ddcf47468 /rsh_test.go
downloadrsh-ec47b533e2d748f6831f0fd2f68e93d0954c1d8f.tar.zst
Initial commitHEADmain
Diffstat (limited to 'rsh_test.go')
-rw-r--r--rsh_test.go93
1 files changed, 93 insertions, 0 deletions
diff --git a/rsh_test.go b/rsh_test.go
new file mode 100644
index 0000000..988a2d8
--- /dev/null
+++ b/rsh_test.go
@@ -0,0 +1,93 @@
+package main
+
+import (
+ "bufio"
+ "strings"
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func TestSplitTokens(t *testing.T) {
+ t.Parallel()
+
+ for _, tc := range []struct {
+ desc string
+ input string
+ want []string
+ }{{
+ desc: "SingleWord",
+ input: "a",
+ want: []string{"a"},
+ }, {
+ desc: "SplitWords",
+ input: "a b c",
+ want: []string{"a", "b", "c"},
+ }, {
+ desc: "Star",
+ input: "a*b",
+ want: []string{"a", "", "*", "", "b"},
+ }, {
+ desc: "Background",
+ input: "a&b",
+ want: []string{"a", "&", "b"},
+ }, {
+ desc: "Pipe",
+ input: "a|b",
+ want: []string{"a", "|", "b"},
+ }, {
+ desc: "And",
+ input: "a&&b",
+ want: []string{"a", "&&", "b"},
+ }, {
+ desc: "Newline",
+ input: "a\n",
+ want: []string{"a", ";"},
+ }, {
+ desc: "CaptureOutputAsFile",
+ input: "a<(b)",
+ want: []string{"a", "<(", "b", ")"},
+ }, {
+ desc: "Substitution",
+ input: "a$(b)c",
+ want: []string{"a", "", "$(", "b", ")", "", "c"},
+ }, {
+ desc: "Comment",
+ input: "abc#de;fg",
+ want: []string{"abc"},
+ }, {
+ desc: "CommentNewline",
+ input: "a#b\n",
+ want: []string{"a", ";"},
+ }, {
+ desc: "BackslashEOF",
+ input: `a\`,
+ want: []string{`a\`},
+ }} {
+ t.Run(tc.desc, func(t *testing.T) {
+ scanner := bufio.NewScanner(strings.NewReader(tc.input))
+ scanner.Split(splitTokens())
+ var got []string
+ for scanner.Scan() {
+ got = append(got, scanner.Text())
+ }
+ if err := scanner.Err(); err != nil {
+ t.Fatalf("splitTokens failed: %s", err)
+ }
+ if diff := cmp.Diff(tc.want, got); diff != "" {
+ t.Errorf("splitTokens produced unexpected diff (-want +got):\n%s", diff)
+ }
+ })
+ }
+}
+
+func FuzzSplitTokens(f *testing.F) {
+ testcases := []string{"a", "a b c", "a*b", "a&b", "a|b", "a&&b", "a\n", "a<(b)", "a$(b)c", "abc#de;fg", "a#b\n", `a\`}
+ for _, tc := range testcases {
+ f.Add([]byte(tc))
+ }
+ f.Fuzz(func(t *testing.T, input []byte) {
+ splitTokens()(input, true)
+ splitTokens()(input, false)
+ })
+}