parse_test.go 1.42 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
}