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

     1  package skeleton
     2  
     3  import "testing"
     4  
     5  func TestFix(t *testing.T) {
     6  	tests := []struct {
     7  		in      *Flag
     8  		exp     *Flag
     9  		success bool
    10  	}{
    11  		{
    12  			in: &Flag{
    13  				LongName:    "debug",
    14  				TypeString:  "bool",
    15  				Description: "Run as DEBUG mode",
    16  			},
    17  			exp: &Flag{
    18  				Name:        "debug",
    19  				ShortName:   "d",
    20  				LongName:    "debug",
    21  				TypeString:  TypeStringBool,
    22  				Default:     false,
    23  				Description: "Run as DEBUG mode",
    24  			},
    25  			success: true,
    26  		},
    27  
    28  		{
    29  			in: &Flag{
    30  				LongName:    "token",
    31  				TypeString:  "s",
    32  				Description: "",
    33  			},
    34  			exp: &Flag{
    35  				Name:        "token",
    36  				ShortName:   "t",
    37  				LongName:    "token",
    38  				TypeString:  TypeStringString,
    39  				Default:     "",
    40  				Description: "",
    41  			},
    42  			success: true,
    43  		},
    44  
    45  		{
    46  			in: &Flag{
    47  				LongName:    "token",
    48  				TypeString:  "s",
    49  				Description: "",
    50  				Default:     "ABCD1124",
    51  			},
    52  			exp: &Flag{
    53  				Name:        "token",
    54  				ShortName:   "t",
    55  				LongName:    "token",
    56  				TypeString:  TypeStringString,
    57  				Default:     "ABCD1124",
    58  				Description: "",
    59  			},
    60  			success: true,
    61  		},
    62  	}
    63  
    64  	for i, tt := range tests {
    65  
    66  		err := tt.in.Fix()
    67  		if err != nil && !tt.success {
    68  			continue
    69  		}
    70  
    71  		if err == nil && !tt.success {
    72  			t.Fatalf("#%d expect Fix to fail", i)
    73  		}
    74  
    75  		if err != nil {
    76  			t.Fatalf("#%d expect Fix not to fail but %q", i, err.Error())
    77  		}
    78  
    79  		if *tt.in != *tt.exp {
    80  			t.Errorf("#%d expect %v to eq %v", i, tt.in, tt.exp)
    81  		}
    82  	}
    83  }
    84  
    85  func TestFixTypeString(t *testing.T) {
    86  
    87  	tests := []struct {
    88  		in            *Flag
    89  		success       bool
    90  		expTypeString string
    91  		expDefault    interface{}
    92  	}{
    93  		{
    94  			in:            &Flag{TypeString: "int"},
    95  			success:       true,
    96  			expTypeString: TypeStringInt,
    97  			expDefault:    0,
    98  		},
    99  
   100  		{
   101  			in:            &Flag{TypeString: "Int"},
   102  			success:       true,
   103  			expTypeString: TypeStringInt,
   104  			expDefault:    0,
   105  		},
   106  
   107  		{
   108  			in:            &Flag{TypeString: "i"},
   109  			success:       true,
   110  			expTypeString: TypeStringInt,
   111  			expDefault:    0,
   112  		},
   113  
   114  		{
   115  			in:            &Flag{TypeString: "string"},
   116  			success:       true,
   117  			expTypeString: TypeStringString,
   118  			expDefault:    "",
   119  		},
   120  
   121  		{
   122  			in:            &Flag{TypeString: "s"},
   123  			success:       true,
   124  			expTypeString: TypeStringString,
   125  			expDefault:    "",
   126  		},
   127  
   128  		{
   129  			in:            &Flag{TypeString: "str"},
   130  			success:       true,
   131  			expTypeString: TypeStringString,
   132  			expDefault:    "",
   133  		},
   134  
   135  		{
   136  			in:            &Flag{TypeString: "bool"},
   137  			success:       true,
   138  			expTypeString: TypeStringBool,
   139  			expDefault:    false,
   140  		},
   141  
   142  		{
   143  			in:            &Flag{TypeString: "b"},
   144  			success:       true,
   145  			expTypeString: TypeStringBool,
   146  			expDefault:    false,
   147  		},
   148  
   149  		{
   150  			in:            &Flag{TypeString: "enexpected_type"},
   151  			success:       false,
   152  			expTypeString: TypeStringBool,
   153  			expDefault:    false,
   154  		},
   155  	}
   156  
   157  	for i, tt := range tests {
   158  
   159  		err := tt.in.fixTypeString()
   160  		if err != nil && !tt.success {
   161  			continue
   162  		}
   163  
   164  		if err == nil && !tt.success {
   165  			t.Fatalf("#%d expect fixTypeString to fail", i)
   166  		}
   167  
   168  		if err != nil {
   169  			t.Fatalf("#%d expect fixTypeString not to fail but %q", i, err.Error())
   170  		}
   171  
   172  		if tt.in.TypeString != tt.expTypeString {
   173  			t.Errorf("#%d expect %q to eq %q", i, tt.in.TypeString, tt.expTypeString)
   174  		}
   175  
   176  		if tt.in.Default != tt.expDefault {
   177  			t.Errorf("#%d expect %v to eq %v", i, tt.in.Default, tt.expDefault)
   178  		}
   179  	}
   180  }