parse_test.go 4.54 KB
Newer Older
1 2 3
package cli

import (
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
4 5
	//"fmt"
	"testing"
Matt Bell's avatar
Matt Bell committed
6

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	"github.com/jbenet/go-ipfs/commands"
8 9 10
)

func TestOptionParsing(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
11
	subCmd := &commands.Command{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
	cmd := &commands.Command{
		Options: []commands.Option{
Matt Bell's avatar
Matt Bell committed
14
			commands.StringOption("b", "some option"),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
15
		},
Matt Bell's avatar
Matt Bell committed
16
		Subcommands: map[string]*commands.Command{
Matt Bell's avatar
Matt Bell committed
17
			"test": subCmd,
Matt Bell's avatar
Matt Bell committed
18
		},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
19
	}
20

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29
	opts, input, err := parseOptions([]string{"--beep", "-boop=lol", "test2", "-c", "beep", "--foo=5"})
	/*for k, v := range opts {
	    fmt.Printf("%s: %s\n", k, v)
	  }
	  fmt.Printf("%s\n", input)*/
	if err != nil {
		t.Error("Should have passed")
	}
	if len(opts) != 4 || opts["beep"] != "" || opts["boop"] != "lol" || opts["c"] != "" || opts["foo"] != "5" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30
		t.Errorf("Returned options were defferent than expected: %v", opts)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32
	}
	if len(input) != 2 || input[0] != "test2" || input[1] != "beep" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
33
		t.Errorf("Returned input was different than expected: %v", input)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
	}
Matt Bell's avatar
Matt Bell committed
35

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37 38 39
	_, _, err = parseOptions([]string{"-beep=1", "-boop=2", "-beep=3"})
	if err == nil {
		t.Error("Should have failed (duplicate option name)")
	}
40

Matt Bell's avatar
Matt Bell committed
41
	path, args, sub := parsePath([]string{"test", "beep", "boop"}, cmd)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
	if len(path) != 1 || path[0] != "test" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43
		t.Errorf("Returned path was defferent than expected: %v", path)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45
	}
	if len(args) != 2 || args[0] != "beep" || args[1] != "boop" {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46
		t.Errorf("Returned args were different than expected: %v", args)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
47
	}
Matt Bell's avatar
Matt Bell committed
48 49 50
	if sub != subCmd {
		t.Errorf("Returned command was different than expected")
	}
51
}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

func TestArgumentParsing(t *testing.T) {
	rootCmd := &commands.Command{
		Subcommands: map[string]*commands.Command{
			"noarg": &commands.Command{},
			"onearg": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, false, "some arg"),
				},
			},
			"twoargs": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, false, "some arg"),
					commands.StringArg("b", true, false, "another arg"),
				},
			},
			"variadic": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, true, "some arg"),
				},
			},
			"optional": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("b", false, true, "another arg"),
				},
			},
			"reversedoptional": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", false, false, "some arg"),
					commands.StringArg("b", true, false, "another arg"),
				},
			},
		},
	}

	_, _, _, err := Parse([]string{"noarg"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"noarg", "value!"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (provided an arg, but command didn't define any)")
	}

	_, _, _, err = Parse([]string{"onearg", "value!"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"onearg"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (didn't provide any args, arg is required)")
	}

	_, _, _, err = Parse([]string{"twoargs", "value1", "value2"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (only provided 1 arg, needs 2)")
	}
	_, _, _, err = Parse([]string{"twoargs"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (didn't provide any args, 2 required)")
	}

	_, _, _, err = Parse([]string{"variadic", "value!"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"variadic", "value1", "value2", "value3"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"variadic"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (didn't provide any args, 1 required)")
	}

	_, _, _, err = Parse([]string{"optional", "value!"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"optional"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}

	_, _, _, err = Parse([]string{"reversedoptional", "value1", "value2"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"reversedoptional", "value!"}, nil, rootCmd)
	if err != nil {
		t.Error("Should have passed")
	}
	_, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (didn't provide any args, 1 required)")
	}
	_, _, _, err = Parse([]string{"reversedoptional", "value1", "value2", "value3"}, nil, rootCmd)
	if err == nil {
		t.Error("Should have failed (provided too many args, only takes 1)")
	}
}