gopkg.in/essentialkaos/ek.v3@v3.5.1/arg/arg_test.go (about)

     1  package arg
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                     Copyright (c) 2009-2016 Essential Kaos                         //
     6  //      Essential Kaos Open Source License <http://essentialkaos.com/ekol?en>         //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"strings"
    12  	"testing"
    13  
    14  	. "pkg.re/check.v1"
    15  )
    16  
    17  // ////////////////////////////////////////////////////////////////////////////////// //
    18  
    19  func Test(t *testing.T) { TestingT(t) }
    20  
    21  type ArgUtilSuite struct{}
    22  
    23  // ////////////////////////////////////////////////////////////////////////////////// //
    24  
    25  var _ = Suite(&ArgUtilSuite{})
    26  
    27  // ////////////////////////////////////////////////////////////////////////////////// //
    28  
    29  func (s *ArgUtilSuite) TestAdd(c *C) {
    30  	args := &Arguments{}
    31  
    32  	c.Assert(args.Add("", &V{}), NotNil)
    33  	c.Assert(args.Add("t:", &V{}), NotNil)
    34  
    35  	c.Assert(args.Add("test", &V{}), IsNil)
    36  	c.Assert(args.Add(":test1", &V{}), IsNil)
    37  	c.Assert(args.Add("t:test2", &V{}), IsNil)
    38  
    39  	c.Assert(args.Add("t1:test", &V{}), NotNil)
    40  	c.Assert(args.Add("t:test3", &V{}), NotNil)
    41  	c.Assert(args.Add("t:test3", nil), NotNil)
    42  }
    43  
    44  func (s *ArgUtilSuite) TestAddMap(c *C) {
    45  	args := NewArguments()
    46  
    47  	m1 := Map{"": &V{}}
    48  	m2 := Map{"t:": &V{}}
    49  
    50  	c.Assert(args.AddMap(m1), Not(HasLen), 0)
    51  	c.Assert(args.AddMap(m2), Not(HasLen), 0)
    52  
    53  	m3 := Map{"test": &V{}}
    54  	m4 := Map{"t:test2": &V{}}
    55  
    56  	c.Assert(args.AddMap(m3), HasLen, 0)
    57  	c.Assert(args.AddMap(m4), HasLen, 0)
    58  
    59  	m5 := Map{
    60  		"t:test":  &V{},
    61  		"T:test1": &V{},
    62  		"t:test2": &V{},
    63  	}
    64  
    65  	c.Assert(args.AddMap(m5), HasLen, 2)
    66  }
    67  
    68  func (s *ArgUtilSuite) TestGlobal(c *C) {
    69  	long, short := ParseArgName("t:test")
    70  
    71  	c.Assert(short, Equals, "t")
    72  	c.Assert(long, Equals, "test")
    73  
    74  	c.Assert(GetS("s:string"), Equals, "")
    75  	c.Assert(GetI("i:int"), Equals, 0)
    76  	c.Assert(GetF("f:float"), Equals, 0.0)
    77  	c.Assert(GetB("b:bool"), Equals, false)
    78  	c.Assert(Has("s:string"), Equals, false)
    79  
    80  	c.Assert(Add("t:test", &V{}), IsNil)
    81  
    82  	global = nil
    83  
    84  	c.Assert(AddMap(Map{}), IsNil)
    85  
    86  	global = nil
    87  
    88  	Parse(Map{})
    89  
    90  	global = NewArguments()
    91  
    92  	_, errs := global.Parse(
    93  		strings.Split("-s Test -i 123 -f 100.5 -b", " "),
    94  		Map{
    95  			"s:string": &V{},
    96  			"i:int":    &V{Type: INT},
    97  			"f:float":  &V{Type: FLOAT},
    98  			"b:bool":   &V{Type: BOOL},
    99  		},
   100  	)
   101  
   102  	c.Assert(errs, HasLen, 0)
   103  
   104  	c.Assert(Has("s:string"), Equals, true)
   105  	c.Assert(Has("string1"), Equals, false)
   106  	c.Assert(GetS("s:string"), Equals, "Test")
   107  	c.Assert(GetI("i:int"), Equals, 123)
   108  	c.Assert(GetF("f:float"), Equals, 100.5)
   109  	c.Assert(GetB("b:bool"), Equals, true)
   110  }
   111  
   112  func (s *ArgUtilSuite) TestGetters(c *C) {
   113  	argline := "file.mp3 -s STRING --required TEST -i 320 -b -f 1.098765 -S2 100 -f1 5 -f2 1 -ms ABC --merg-string DEF -mi 6 --merg-int 6 -f3 12 -mf 10.1 -mf 10.1 -i1 5"
   114  
   115  	argsMap := Map{
   116  		"s:string":          &V{Type: STRING, Value: "STRING"},
   117  		"S:empty-string":    &V{Type: STRING},
   118  		"r:required":        &V{Required: true, Alias: "A:alias"},
   119  		"i:int":             &V{Type: INT},
   120  		"i1:int-between":    &V{Type: INT, Min: 1, Max: 3},
   121  		"I:not-set-int":     &V{Type: INT, Value: 0},
   122  		"b:bool":            &V{Type: BOOL},
   123  		"B:empty-bool":      &V{Type: BOOL},
   124  		"B1:not-set-bool":   &V{Type: BOOL, Value: false},
   125  		"f:float":           &V{Type: FLOAT},
   126  		"F:not-set-float":   &V{Type: FLOAT, Value: 0.0},
   127  		"f1:float-max":      &V{Type: FLOAT, Max: 3.0},
   128  		"f2:float-min":      &V{Type: FLOAT, Min: 3.0},
   129  		"f3:float-between":  &V{Type: FLOAT, Min: 3.0, Max: 10.0},
   130  		"ms:merg-string":    &V{Mergeble: true},
   131  		"mi:merg-int":       &V{Type: INT, Mergeble: true},
   132  		"mf:merg-float":     &V{Type: FLOAT, Mergeble: true},
   133  		"S1:not-set-string": &V{Type: STRING, Value: ""},
   134  		"S2:string-as-num":  &V{Type: STRING},
   135  	}
   136  
   137  	args := NewArguments()
   138  	args.Parse(strings.Split(argline, " "), argsMap)
   139  
   140  	c.Assert(args.GetS("_not_exist_"), Equals, "")
   141  	c.Assert(args.GetI("_not_exist_"), Equals, 0)
   142  	c.Assert(args.GetB("_not_exist_"), Equals, false)
   143  	c.Assert(args.GetF("_not_exist_"), Equals, 0.0)
   144  
   145  	c.Assert(args.GetS("s:string"), Equals, "STRING")
   146  	c.Assert(args.GetS("string"), Equals, "STRING")
   147  	c.Assert(args.GetS("S:empty-string"), Equals, "")
   148  	c.Assert(args.GetS("empty-string"), Equals, "")
   149  	c.Assert(args.GetB("string"), Equals, true)
   150  	c.Assert(args.GetI("string"), Equals, 0)
   151  	c.Assert(args.GetF("string"), Equals, 0.0)
   152  	c.Assert(args.GetB("empty-string"), Equals, false)
   153  	c.Assert(args.GetI("empty-string"), Equals, 0)
   154  	c.Assert(args.GetF("empty-string"), Equals, 0.0)
   155  	c.Assert(args.GetB("not-set-string"), Equals, false)
   156  	c.Assert(args.GetI("not-set-string"), Equals, 0)
   157  	c.Assert(args.GetF("not-set-string"), Equals, 0.0)
   158  	c.Assert(args.GetS("S2:string-as-num"), Equals, "100")
   159  	c.Assert(args.GetB("S2:string-as-num"), Equals, true)
   160  	c.Assert(args.GetI("S2:string-as-num"), Equals, 100)
   161  	c.Assert(args.GetF("S2:string-as-num"), Equals, 100.0)
   162  
   163  	c.Assert(args.GetS("r:required"), Equals, "TEST")
   164  	c.Assert(args.GetS("required"), Equals, "TEST")
   165  	c.Assert(args.GetS("A:alias"), Equals, "TEST")
   166  	c.Assert(args.GetS("alias"), Equals, "TEST")
   167  
   168  	c.Assert(args.GetS("int"), Equals, "320")
   169  	c.Assert(args.GetB("int"), Equals, true)
   170  	c.Assert(args.GetI("int"), Equals, 320)
   171  	c.Assert(args.GetF("int"), Equals, 320.0)
   172  	c.Assert(args.GetI("int-between"), Equals, 3)
   173  	c.Assert(args.GetS("not-set-int"), Equals, "0")
   174  	c.Assert(args.GetB("not-set-int"), Equals, false)
   175  	c.Assert(args.GetI("not-set-int"), Equals, 0)
   176  	c.Assert(args.GetF("not-set-int"), Equals, 0.0)
   177  
   178  	c.Assert(args.GetS("b:bool"), Equals, "true")
   179  	c.Assert(args.GetI("b:bool"), Equals, 1)
   180  	c.Assert(args.GetB("b:bool"), Equals, true)
   181  	c.Assert(args.GetF("b:bool"), Equals, 1.0)
   182  	c.Assert(args.GetS("empty-bool"), Equals, "")
   183  	c.Assert(args.GetI("empty-bool"), Equals, 0)
   184  	c.Assert(args.GetB("empty-bool"), Equals, false)
   185  	c.Assert(args.GetF("empty-bool"), Equals, 0.0)
   186  	c.Assert(args.GetS("not-set-bool"), Equals, "false")
   187  	c.Assert(args.GetI("not-set-bool"), Equals, 0)
   188  	c.Assert(args.GetB("not-set-bool"), Equals, false)
   189  	c.Assert(args.GetF("not-set-bool"), Equals, 0.0)
   190  
   191  	c.Assert(args.GetS("float"), Equals, "1.098765")
   192  	c.Assert(args.GetB("float"), Equals, true)
   193  	c.Assert(args.GetI("float"), Equals, 1)
   194  	c.Assert(args.GetF("float"), Equals, 1.098765)
   195  	c.Assert(args.GetS("not-set-float"), Equals, "0")
   196  	c.Assert(args.GetB("not-set-float"), Equals, false)
   197  	c.Assert(args.GetI("not-set-float"), Equals, 0)
   198  	c.Assert(args.GetF("not-set-float"), Equals, 0.0)
   199  
   200  	c.Assert(args.GetF("float-max"), Equals, 3.0)
   201  	c.Assert(args.GetF("float-min"), Equals, 3.0)
   202  	c.Assert(args.GetF("float-between"), Equals, 10.0)
   203  
   204  	c.Assert(args.GetS("merg-string"), Equals, "ABC DEF")
   205  	c.Assert(args.GetI("merg-int"), Equals, 12)
   206  	c.Assert(args.GetF("merg-float"), Equals, 20.2)
   207  
   208  	c.Assert(args.Has("_not_exist_"), Equals, false)
   209  	c.Assert(args.Has("empty-string"), Equals, false)
   210  	c.Assert(args.Has("s:string"), Equals, true)
   211  }
   212  
   213  func (s *ArgUtilSuite) TestParsing(c *C) {
   214  	_, errs := NewArguments().Parse([]string{}, Map{})
   215  
   216  	c.Assert(errs, HasLen, 0)
   217  
   218  	// //////////////////////////////////////////////////////////////////////////////// //
   219  
   220  	_, errs = NewArguments().Parse([]string{"--test", "100"}, Map{"t:test": &V{Type: 10}})
   221  
   222  	c.Assert(errs, Not(HasLen), 0)
   223  	c.Assert(errs[0].Error(), Equals, "Unsuported argument type 10")
   224  
   225  	// //////////////////////////////////////////////////////////////////////////////// //
   226  
   227  	_, errs = NewArguments().Parse([]string{}, Map{"s:string": &V{}, "s:trace": &V{}})
   228  
   229  	c.Assert(errs, Not(HasLen), 0)
   230  	c.Assert(errs[0].Error(), Equals, "Argument -s defined 2 or more times")
   231  
   232  	// //////////////////////////////////////////////////////////////////////////////// //
   233  
   234  	_, errs = NewArguments().Parse([]string{"-t"}, Map{"s:string": &V{}})
   235  
   236  	c.Assert(errs, Not(HasLen), 0)
   237  	c.Assert(errs[0].Error(), Equals, "Argument -t is not supported")
   238  
   239  	// //////////////////////////////////////////////////////////////////////////////// //
   240  
   241  	_, errs = NewArguments().Parse([]string{"-t=100"}, Map{"s:string": &V{}})
   242  
   243  	c.Assert(errs, Not(HasLen), 0)
   244  	c.Assert(errs[0].Error(), Equals, "Argument -t is not supported")
   245  
   246  	// //////////////////////////////////////////////////////////////////////////////// //
   247  
   248  	_, errs = NewArguments().Parse([]string{"--test"}, Map{"s:string": &V{}})
   249  
   250  	c.Assert(errs, Not(HasLen), 0)
   251  	c.Assert(errs[0].Error(), Equals, "Argument --test is not supported")
   252  
   253  	// //////////////////////////////////////////////////////////////////////////////// //
   254  
   255  	fArgs, errs := NewArguments().Parse([]string{"-", "--"}, Map{"t:test": &V{}})
   256  
   257  	c.Assert(errs, HasLen, 0)
   258  	c.Assert(fArgs, DeepEquals, []string{"-", "--"})
   259  
   260  	// //////////////////////////////////////////////////////////////////////////////// //
   261  
   262  	_, errs = NewArguments().Parse([]string{"--asd="}, Map{"t:test": &V{}})
   263  
   264  	c.Assert(errs, Not(HasLen), 0)
   265  	c.Assert(errs[0].Error(), Equals, "Argument --asd has wrong format")
   266  
   267  	// //////////////////////////////////////////////////////////////////////////////// //
   268  
   269  	_, errs = NewArguments().Parse([]string{"-j="}, Map{"t:test": &V{}})
   270  
   271  	c.Assert(errs, Not(HasLen), 0)
   272  	c.Assert(errs[0].Error(), Equals, "Argument -j has wrong format")
   273  
   274  	// //////////////////////////////////////////////////////////////////////////////// //
   275  
   276  	_, errs = NewArguments().Parse([]string{}, Map{"t:test": &V{Required: true}})
   277  
   278  	c.Assert(errs, Not(HasLen), 0)
   279  	c.Assert(errs[0].Error(), Equals, "Required argument test is not set")
   280  
   281  	// //////////////////////////////////////////////////////////////////////////////// //
   282  
   283  	_, errs = NewArguments().Parse([]string{"-t"}, Map{"t:test": &V{}})
   284  
   285  	c.Assert(errs, Not(HasLen), 0)
   286  	c.Assert(errs[0].Error(), Equals, "Non-boolean argument --test is empty")
   287  
   288  	// //////////////////////////////////////////////////////////////////////////////// //
   289  
   290  	_, errs = NewArguments().Parse([]string{"--test=!"}, Map{"t:test": &V{Type: FLOAT}})
   291  
   292  	c.Assert(errs, Not(HasLen), 0)
   293  	c.Assert(errs[0].Error(), Equals, "Argument --test has wrong format")
   294  
   295  	// //////////////////////////////////////////////////////////////////////////////// //
   296  
   297  	_, errs = NewArguments().Parse([]string{"-t=!"}, Map{"t:test": &V{Type: INT}})
   298  
   299  	c.Assert(errs, Not(HasLen), 0)
   300  	c.Assert(errs[0].Error(), Equals, "Argument --test has wrong format")
   301  
   302  	// //////////////////////////////////////////////////////////////////////////////// //
   303  
   304  	_, errs = NewArguments().Parse([]string{"--test", "!"}, Map{"t:test": &V{Type: INT}})
   305  
   306  	c.Assert(errs, Not(HasLen), 0)
   307  	c.Assert(errs[0].Error(), Equals, "Argument --test has wrong format")
   308  
   309  	// //////////////////////////////////////////////////////////////////////////////// //
   310  
   311  	_, errs = NewArguments().Parse([]string{}, Map{"t:test": nil})
   312  
   313  	c.Assert(errs, Not(HasLen), 0)
   314  	c.Assert(errs[0].Error(), Equals, "Struct for argument --test is nil")
   315  
   316  	// //////////////////////////////////////////////////////////////////////////////// //
   317  
   318  	_, errs = NewArguments().Parse([]string{}, Map{"": &V{}})
   319  
   320  	c.Assert(errs, Not(HasLen), 0)
   321  	c.Assert(errs[0].Error(), Equals, "Some argument does not have a name")
   322  }