blob: 303874a86f81e45a27b71a078b9fcf619f1d4044 [file] [log] [blame]
Cosmos Nicolaou7c659ac2014-06-09 22:47:04 -07001package main
2
3import (
4 "fmt"
5 "reflect"
6 "testing"
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -07007
8 "veyron.io/veyron/veyron/lib/modules"
Cosmos Nicolaou7c659ac2014-06-09 22:47:04 -07009)
10
11func TestFields(t *testing.T) {
12 cases := []struct {
13 input string
14 output []string
15 }{
16 {"", []string{}},
17 {"a", []string{"a"}},
18 {" z", []string{"z"}},
19 {" zz zz", []string{"zz", "zz"}},
20 {"ab", []string{"ab"}},
21 {"a b", []string{"a", "b"}},
22 {`a " b"`, []string{"a", " b"}},
23 {`a " b zz"`, []string{"a", " b zz"}},
24 {`a " b zz"`, []string{"a", " b zz"}},
25 {`a " b" cc`, []string{"a", " b", "cc"}},
26 {`a "z b" cc`, []string{"a", "z b", "cc"}},
27 }
28 for i, c := range cases {
29 got, err := splitQuotedFields(c.input)
30 if err != nil {
31 t.Errorf("%d: %q: unexpected error: %v", i, c.input, err)
32 }
33 if !reflect.DeepEqual(got, c.output) {
34 t.Errorf("%d: %q: got %#v, want %#v", i, c.input, got, c.output)
35 }
36 }
37 if _, err := splitQuotedFields(`a b "c`); err == nil {
38 t.Errorf("expected error for unterminated quote")
39 }
40}
41
42func TestVariables(t *testing.T) {
Cosmos Nicolaou344cc4a2014-11-26 15:38:43 -080043 sh, err := modules.NewShell(nil)
44 if err != nil {
45 t.Fatalf("unexpected error: %s", err)
46 }
Robin Thellendcf140c02014-12-08 14:56:24 -080047 defer sh.Cleanup(nil, nil)
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070048 sh.SetVar("foo", "bar")
Cosmos Nicolaou7c659ac2014-06-09 22:47:04 -070049 cases := []struct {
50 input string
51 output []string
52 }{
53 {"a b", []string{"a", "b"}},
54 {"a $foo", []string{"a", "bar"}},
55 {"$foo a", []string{"bar", "a"}},
56 {`a "$foo "`, []string{"a", "bar "}},
57 {"a xx$foo", []string{"a", "xxbar"}},
58 {"a xx${foo}yy", []string{"a", "xxbaryy"}},
59 {`a "foo"`, []string{"a", "foo"}},
60 }
61 for i, c := range cases {
62 fields, err := splitQuotedFields(c.input)
63 if err != nil {
64 t.Errorf("%d: %q: unexpected error: %v", i, c.input, err)
65 }
Cosmos Nicolaou9c9918d2014-09-23 08:45:56 -070066 got, err := subVariables(sh, fields)
Cosmos Nicolaou7c659ac2014-06-09 22:47:04 -070067 if err != nil {
68 t.Errorf("%d: %q: unexpected error: %v", i, c.input, err)
69 }
70 if !reflect.DeepEqual(got, c.output) {
71 t.Errorf("%d: %q: got %#v, want %#v", i, c.input, got, c.output)
72 }
73 }
74
75 errors := []struct {
76 input string
77 err error
78 }{
79 {"$foox", fmt.Errorf("unknown variable: %q", "foox")},
80 {"${fo", fmt.Errorf("unterminated variable: %q", "{fo")},
81 }
82 for i, c := range errors {
Matt Rosencrantz0dd7ce02014-09-23 11:34:26 -070083 vars, got := subVariables(sh, []string{c.input})
84 if (got == nil && c.err != nil) || got.Error() != c.err.Error() {
85 t.Errorf("%d: %q: expected error: got %v (with results %#v) want %v", i, c.input, got, vars, c.err)
Cosmos Nicolaou7c659ac2014-06-09 22:47:04 -070086 }
87 }
88}