github.com/tcnksm/gcli@v0.2.4-0.20170129033839-7eb950507e5a/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 { 30 Name: "add", 31 FunctionName: "add", 32 Synopsis: "Add new task", 33 }, 34 }, 35 }, 36 { 37 arg: `add:"Add new task",delete:"Delete task"`, 38 success: true, 39 expect: []*skeleton.Command{ 40 {Name: "add", FunctionName: "add", Synopsis: "Add new task"}, 41 {Name: "delete", FunctionName: "delete", Synopsis: "Delete task"}, 42 }, 43 }, 44 { 45 arg: `add,delete,list`, 46 success: true, 47 expect: []*skeleton.Command{ 48 {Name: "add", FunctionName: "add"}, 49 {Name: "delete", FunctionName: "delete"}, 50 {Name: "list", FunctionName: "list"}, 51 }, 52 }, 53 { 54 arg: `include:"Include " character inside"`, 55 success: true, 56 expect: []*skeleton.Command{ 57 {Name: "include", FunctionName: "include", Synopsis: "Include \" character inside"}, 58 }, 59 }, 60 } 61 62 for i, tt := range tests { 63 c := new(CommandFlag) 64 err := c.Set(tt.arg) 65 if tt.success && err != nil { 66 t.Fatalf("#%d Set(%q) expects not to happen error: %s", i, tt.arg, err) 67 } 68 69 if !reflect.DeepEqual(*c, tt.expect) { 70 t.Errorf("#%d expects %v to be eq %v", i, *c, tt.expect) 71 } 72 } 73 }