parse_test.go 1.33 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) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14
	cmd := &commands.Command{
		Options: []commands.Option{
			commands.Option{Names: []string{"b"}, Type: commands.String},
		},
Matt Bell's avatar
Matt Bell committed
15 16 17
		Subcommands: map[string]*commands.Command{
			"test": &commands.Command{},
		},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18
	}
19

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33
	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" {
		t.Error("Returned options were defferent than expected: %v", opts)
	}
	if len(input) != 2 || input[0] != "test2" || input[1] != "beep" {
		t.Error("Returned input was different than expected: %v", input)
	}
Matt Bell's avatar
Matt Bell committed
34

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40
	path, args := parsePath([]string{"test", "beep", "boop"}, cmd)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41 42 43 44 45 46
	if len(path) != 1 || path[0] != "test" {
		t.Error("Returned path was defferent than expected: %v", path)
	}
	if len(args) != 2 || args[0] != "beep" || args[1] != "boop" {
		t.Error("Returned args were different than expected: %v", args)
	}
47
}