blob: c053962fb4413c891d1c18e92304dd31aaa3023f [file] [log] [blame]
Todd Wange8e67462015-05-11 22:38:16 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package v23cmd
6
7import (
8 "bytes"
9 "fmt"
10 "testing"
11
12 "v.io/v23"
13 "v.io/v23/context"
14 "v.io/x/lib/cmdline"
15 _ "v.io/x/ref/runtime/factories/generic"
16)
17
18var cmdRoot = &cmdline.Command{
19 Name: "root",
20 Short: "root",
21 Long: "root",
22 Children: []*cmdline.Command{cmdNoV23, cmdNoInit, cmdWithInit},
23}
24
25var cmdNoV23 = &cmdline.Command{
26 Name: "no_v23",
27 Short: "no_v23",
28 Long: "no_v23",
29 Runner: cmdline.RunnerFunc(runNoV23),
30}
31
32var cmdNoInit = &cmdline.Command{
33 Name: "no_init",
34 Short: "no_init",
35 Long: "no_init",
36 Runner: RunnerFunc(runNoInit),
37}
38
39var cmdWithInit = &cmdline.Command{
40 Name: "with_init",
41 Short: "with_init",
42 Long: "with_init",
43 Runner: RunnerFuncWithInit(runWithInit, initFunc),
44}
45
46func runNoV23(env *cmdline.Env, args []string) error {
47 fmt.Fprintf(env.Stdout, "NoV23")
48 return nil
49}
50
51func runNoInit(ctx *context.T, env *cmdline.Env, args []string) error {
52 fmt.Fprintf(env.Stdout, "NoInit %v %v", ctx.Value(initKey), ctx.Value(forTestKey))
53 return nil
54}
55
56func runWithInit(ctx *context.T, env *cmdline.Env, args []string) error {
57 fmt.Fprintf(env.Stdout, "WithInit %v %v", ctx.Value(initKey), ctx.Value(forTestKey))
58 return nil
59}
60
61const (
62 initKey = "init key"
63 initValue = "<init value>"
64 forTestKey = "for test key"
65 forTestValue = "<for test value>"
66)
67
68func initFunc() (*context.T, v23.Shutdown) {
69 ctx, shutdown := v23.Init()
70 return context.WithValue(ctx, initKey, initValue), shutdown
71}
72
73func TestParseAndRun(t *testing.T) {
74 tests := []struct {
75 cmd string
76 stdout string
77 }{
78 {"no_v23", "NoV23"},
79 {"no_init", "NoInit <nil> <nil>"},
80 {"with_init", "WithInit <init value> <nil>"},
81 }
82 for _, test := range tests {
83 var stdout bytes.Buffer
84 env := &cmdline.Env{Stdout: &stdout}
85 if err := cmdline.ParseAndRun(cmdRoot, env, []string{test.cmd}); err != nil {
86 t.Errorf("ParseAndRun failed: %v", err)
87 }
88 if got, want := stdout.String(), test.stdout; got != want {
89 t.Errorf("got stdout %q, want %q", got, want)
90 }
91 }
92}
93
94func TestParseAndRunForTest(t *testing.T) {
95 tests := []struct {
96 cmd string
97 stdout string
98 }{
99 {"no_v23", "NoV23"},
100 {"no_init", "NoInit <nil> <for test value>"},
101 {"with_init", "WithInit <nil> <for test value>"},
102 }
103 for _, test := range tests {
104 ctx, shutdown := v23.Init()
105 ctx = context.WithValue(ctx, forTestKey, forTestValue)
106
107 var stdout bytes.Buffer
108 env := &cmdline.Env{Stdout: &stdout}
109 if err := ParseAndRunForTest(cmdRoot, ctx, env, []string{test.cmd}); err != nil {
110 t.Errorf("ParseAndRunForTest failed: %v", err)
111 }
112 if got, want := stdout.String(), test.stdout; got != want {
113 t.Errorf("got stdout %q, want %q", got, want)
114 }
115 shutdown()
116 }
117}