viye/internal/viye/viye_test.go (view raw)
| 1 | package viye |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "olexsmir.xyz/x/is" |
| 7 | ) |
| 8 | |
| 9 | func TestSplitArgs(t *testing.T) { |
| 10 | tests := []struct{ args, want, wantB []string }{ |
| 11 | {[]string{"/tmp/foo", "bar/baz"}, []string{"/tmp/foo", "bar/baz"}, nil}, |
| 12 | {[]string{"./foo", "bar"}, []string{"./foo", "bar"}, nil}, |
| 13 | {[]string{"./"}, []string{"./"}, nil}, |
| 14 | {[]string{"././foo", "./bar"}, []string{"././foo", "./bar"}, nil}, |
| 15 | {[]string{"demo"}, []string{"demo"}, nil}, |
| 16 | {[]string{"demo", "--", ": name: olex"}, []string{"demo"}, []string{"name: olex"}}, |
| 17 | {[]string{"demo", "--", ": name: olex", ": age: 30"}, []string{"demo"}, []string{"name: olex", "age: 30"}}, |
| 18 | {[]string{"--", ": body"}, nil, []string{"body"}}, |
| 19 | {[]string{"demo", "--", "raw body"}, []string{"demo"}, []string{"raw body"}}, |
| 20 | } |
| 21 | for _, tt := range tests { |
| 22 | got, gotB := splitArgs(tt.args) |
| 23 | is.Equal(t, len(tt.want), len(got)) |
| 24 | if len(got) == len(tt.want) { |
| 25 | for i := range got { |
| 26 | is.Equal(t, tt.want[i], got[i]) |
| 27 | } |
| 28 | } |
| 29 | is.Equal(t, len(tt.wantB), len(gotB)) |
| 30 | if len(gotB) == len(tt.wantB) { |
| 31 | for i := range gotB { |
| 32 | is.Equal(t, tt.wantB[i], gotB[i]) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestSplitLeaf(t *testing.T) { |
| 39 | for _, tt := range []struct { |
| 40 | path []string |
| 41 | cmd string |
| 42 | wantArgs []string |
| 43 | }{ |
| 44 | {[]string{"get", "http://site.com"}, "get", []string{"http://site.com"}}, |
| 45 | {[]string{"$", "go", "run", "."}, "$", []string{"go", "run", "."}}, |
| 46 | {[]string{"ip"}, "ip", []string{}}, |
| 47 | } { |
| 48 | gotCmd, gotArgs := splitLeaf(tt.path) |
| 49 | is.Equal(t, tt.cmd, gotCmd) |
| 50 | is.Equal(t, tt.wantArgs, gotArgs) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestExpandEnv(t *testing.T) { |
| 55 | t.Setenv("VIYE_TEST_ONE", "alpha") |
| 56 | for _, tt := range []struct{ in, want string }{ |
| 57 | {"plain", "plain"}, |
| 58 | {"$VIYE_TEST_ONE", "alpha"}, |
| 59 | {"${VIYE_TEST_ONE}/x", "alpha/x"}, |
| 60 | {"$VIYE_TEST_UNSET", ""}, |
| 61 | {"$$", "$$"}, |
| 62 | } { |
| 63 | is.Equal(t, tt.want, expandEnv(tt.in)) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestDispatch(t *testing.T) { |
| 68 | for _, tt := range []struct { |
| 69 | path []string |
| 70 | wantCmd string |
| 71 | wantArgs []string |
| 72 | }{ |
| 73 | {[]string{"get", "http://site.com"}, "get", []string{"http://site.com"}}, |
| 74 | {[]string{"ip"}, "ip", []string{}}, |
| 75 | {[]string{"$", "go", "run", "."}, "$", []string{"go", "run", "."}}, |
| 76 | {[]string{"$$", "sleep", "5"}, "$$", []string{"sleep", "5"}}, |
| 77 | } { |
| 78 | tool := &mockTool{cmd: tt.wantCmd} |
| 79 | v := &Viye{tools: []Tool{tool}} |
| 80 | ctx := &Context{Path: tt.path} |
| 81 | v.dispatch(ctx) |
| 82 | is.Equal(t, tt.wantCmd, ctx.Cmd) |
| 83 | is.Equal(t, tt.wantArgs, ctx.Args) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | type mockTool struct { |
| 88 | name, cmd string |
| 89 | args []string |
| 90 | } |
| 91 | |
| 92 | func (m *mockTool) Name() string { return m.name } |
| 93 | func (m *mockTool) Match(c *Context) bool { return c.Cmd == m.cmd } |
| 94 | func (m *mockTool) Execute(c *Context) (string, error) { |
| 95 | m.args = c.Args |
| 96 | return c.Cmd, nil |
| 97 | } |