aboutsummaryrefslogtreecommitdiff
path: root/cmd/monwork/shell_fields_test.go
diff options
context:
space:
mode:
authorGibheer <gibheer+git@zero-knowledge.org>2018-12-14 14:02:58 +0100
committerGibheer <gibheer+git@zero-knowledge.org>2018-12-14 14:02:58 +0100
commit10fb89a017edb4fe35d87d22f1ff96d79e78c185 (patch)
treebc1ed9c2692a214e1ded8b47de85f68cdc221582 /cmd/monwork/shell_fields_test.go
parentff79584084436b74151e7549f70ee8f0a1d3aebe (diff)
monwork - readd stringToShellFields
This function was needed as bytes.Fields had some problems with the quoting. Now there are test cases too so that errors can be found more easily.
Diffstat (limited to 'cmd/monwork/shell_fields_test.go')
-rw-r--r--cmd/monwork/shell_fields_test.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/cmd/monwork/shell_fields_test.go b/cmd/monwork/shell_fields_test.go
new file mode 100644
index 0000000..4ee55cc
--- /dev/null
+++ b/cmd/monwork/shell_fields_test.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "bytes"
+ "fmt"
+ "testing"
+)
+
+func TestStringToShellFields(t *testing.T) {
+ type S struct {
+ source string
+ target []string
+ }
+ for i, e := range []S{
+ S{"foo", []string{"foo"}},
+ S{"foo bar", []string{"foo", "bar"}},
+ S{`foo "bar"`, []string{"foo", `bar`}},
+ S{`foo "bar baz"`, []string{"foo", `bar baz`}},
+ S{`foo "bar" "baz"`, []string{"foo", `bar`, `baz`}},
+ S{`foo "bar" "baz"`, []string{"foo", `bar`, `baz`}},
+ } {
+ result := stringToShellFields([]byte(e.source))
+ if err := compare(e.target, result); err != nil {
+ t.Errorf("test %d did not match: %s", i, err)
+ }
+ }
+}
+
+func compare(source []string, target [][]byte) error {
+ if len(source) != len(target) {
+ return fmt.Errorf("length mismatch %d vs %d", len(source), len(target))
+ }
+ for i, e := range source {
+ if bytes.Compare([]byte(e), target[i]) != 0 {
+ return fmt.Errorf("mismatch in content field %d: %s vs %s", i, e, target[i])
+ }
+ }
+ return nil
+}