github.com/goonzoid/gcli@v0.2.3-0.20150926213610-155587606ea1/command/flag_command_test.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/tcnksm/gcli/skeleton"
     9  )
    10  
    11  func TestCommandFlag_implements(t *testing.T) {
    12  	var raw interface{}
    13  	raw = new(CommandFlag)
    14  	if _, ok := raw.(flag.Value); !ok {
    15  		t.Fatal("CommandFlag should be flag.Value")
    16  	}
    17  }
    18  
    19  func TestCommandFlag_Set(t *testing.T) {
    20  	tests := []struct {
    21  		arg     string
    22  		success bool
    23  		expect  CommandFlag
    24  	}{
    25  		{
    26  			arg:     `add:"Add new task"`,
    27  			success: true,
    28  			expect: []skeleton.Command{
    29  				{Name: "add", Synopsis: "Add new task"},
    30  			},
    31  		},
    32  		{
    33  			arg:     `add:"Add new task",delete:"Delete task"`,
    34  			success: true,
    35  			expect: []skeleton.Command{
    36  				{Name: "add", Synopsis: "Add new task"},
    37  				{Name: "delete", Synopsis: "Delete task"},
    38  			},
    39  		},
    40  		{
    41  			arg:     `add,delete,list`,
    42  			success: true,
    43  			expect: []skeleton.Command{
    44  				{Name: "add"},
    45  				{Name: "delete"},
    46  				{Name: "list"},
    47  			},
    48  		},
    49  		{
    50  			arg:     `include:"Include " character inside"`,
    51  			success: true,
    52  			expect: []skeleton.Command{
    53  				{Name: "include", Synopsis: "Include \" character inside"},
    54  			},
    55  		},
    56  	}
    57  
    58  	for i, tt := range tests {
    59  		c := new(CommandFlag)
    60  		err := c.Set(tt.arg)
    61  		if tt.success && err != nil {
    62  			t.Fatalf("#%d Set(%q) expects not to happen error: %s", i, tt.arg, err)
    63  		}
    64  
    65  		if !reflect.DeepEqual(*c, tt.expect) {
    66  			t.Errorf("#%d expects %v to be eq %v", i, *c, tt.expect)
    67  		}
    68  	}
    69  }