// Test ParseArgs with all flags possible with custom parsers
Test ParseArgs with all flags possible with custom parsers
// Test ParseArgs with all flags possible with custom parsers
func TestParseArgsAll(t *testing.T) {
	config := newConfiguration()
	flagMap := make(map[string]reflect.StructField)
	if err := getTypesRecursive(reflect.ValueOf(config), flagMap, ""); err != nil {
		t.Fatal(err)
	}
	parsers := map[reflect.Type]parse.Parser{
		reflect.TypeOf([]ServerInfo{}): &sliceServerValue{},
	}
	var boolParser parse.BoolValue
	parsers[reflect.TypeOf(true)] = &boolParser
	var intParser parse.IntValue
	parsers[reflect.TypeOf(1)] = &intParser
	var int64Parser parse.Int64Value
	parsers[reflect.TypeOf(int64(1))] = &int64Parser
	var uintParser parse.UintValue
	parsers[reflect.TypeOf(uint(1))] = &uintParser
	var uint64Parser parse.Uint64Value
	parsers[reflect.TypeOf(uint64(1))] = &uint64Parser
	var stringParser parse.StringValue
	parsers[reflect.TypeOf("")] = &stringParser
	var float64Parser parse.Float64Value
	parsers[reflect.TypeOf(float64(1.5))] = &float64Parser
	var durationParser parse.Duration
	parsers[reflect.TypeOf(parse.Duration(time.Second))] = &durationParser
	var timeParser parse.TimeValue
	parsers[reflect.TypeOf(time.Now())] = &timeParser
	args := []string{
		"--loglevel=INFO",
		"--timeout=1s",
		"--db",
		"--db.watch",
		"--db.ip=192.168.0.1",
		"--db.load=-1",
		"--db.load64=-164",
		"--db.comax=2",
		"--db.connectionmax64=264",
		"--owner",
		"--owner.name",
		"--owner.dob=2016-04-20T17:39:00Z",
		"--owner.rate=0.222",
		"--owner.servers=1.0.0.1",
	}
	valMap, err := parseArgs(args, flagMap, parsers)
	if err != nil {
		t.Fatal(err)
	}
	check := map[string]parse.Parser{}
	stringParser.SetValue("INFO")
	check["loglevel"] = &stringParser
	durationParser.SetValue(parse.Duration(time.Second))
	check["timeout"] = &durationParser
	boolParser.SetValue(true)
	check["db"] = &boolParser
	check["db.watch"] = &boolParser
	checkDcIP := parse.StringValue("192.168.0.1")
	check["db.ip"] = &checkDcIP
	intParser.SetValue(-1)
	check["db.load"] = &intParser
	int64Parser.SetValue(int64(-164))
	check["db.load64"] = &int64Parser
	uintParser.SetValue(uint(2))
	check["db.comax"] = &uintParser
	uint64Parser.SetValue(uint64(264))
	check["db.connectionmax64"] = &uint64Parser
	check["owner"] = &boolParser
	check["owner.name"] = &boolParser
	timeParser.Set("2016-04-20T17:39:00Z")
	check["owner.dob"] = &timeParser
	float64Parser.SetValue(0.222)
	check["owner.rate"] = &float64Parser
	checkOwnerServers := sliceServerValue{
		ServerInfo{IP: "1.0.0.1"},
	}
	check["owner.servers"] = &checkOwnerServers
	if len(check) != len(valMap) {
		t.Errorf("expected %d elements in valMap got %d", len(check), len(valMap))
	}
	for flag, parser := range valMap {
		if !reflect.DeepEqual(parser, check[flag]) {
			t.Errorf("Got %s expected %s\n", parser, check[flag])
		}
	}
}
func TestParseArgsAll ( t * testing . T ) { config := newConfiguration ( ) flagMap := make ( map [ string ] reflect . StructField ) if err := getTypesRecursive ( reflect . ValueOf ( config ) , flagMap , " " ) ; err != nil { t . Fatal ( err ) } parsers := map [ reflect . Type ] parse . Parser { reflect . TypeOf ( [ ] ServerInfo { } ) : & sliceServerValue { } , } var boolParser parse . BoolValue parsers [ reflect . TypeOf ( true ) ] = & boolParser var intParser parse . IntValue parsers [ reflect . TypeOf ( 1 ) ] = & intParser var int64Parser parse . Int64Value parsers [ reflect . TypeOf ( int64 ( 1 ) ) ] = & int64Parser var uintParser parse . UintValue parsers [ reflect . TypeOf ( uint ( 1 ) ) ] = & uintParser var uint64Parser parse . Uint64Value parsers [ reflect . TypeOf ( uint64 ( 1 ) ) ] = & uint64Parser var stringParser parse . StringValue parsers [ reflect . TypeOf ( " " ) ] = & stringParser var float64Parser parse . Float64Value parsers [ reflect . TypeOf ( float64 ( 1.5 ) ) ] = & float64Parser var durationParser parse . Duration parsers [ reflect . TypeOf ( parse . Duration ( time . Second ) ) ] = & durationParser var timeParser parse . TimeValue parsers [ reflect . TypeOf ( time . Now ( ) ) ] = & timeParser args := [ ] string { " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , } valMap , err := parseArgs ( args , flagMap , parsers ) if err != nil { t . Fatal ( err ) } check := map [ string ] parse . Parser { } stringParser . SetValue ( " " ) check [ " " ] = & stringParser durationParser . SetValue ( parse . Duration ( time . Second ) ) check [ " " ] = & durationParser boolParser . SetValue ( true ) check [ " " ] = & boolParser check [ " " ] = & boolParser checkDcIP := parse . StringValue ( " " ) check [ " " ] = & checkDcIP intParser . SetValue ( - 1 ) check [ " " ] = & intParser int64Parser . SetValue ( int64 ( - 164 ) ) check [ " " ] = & int64Parser uintParser . SetValue ( uint ( 2 ) ) check [ " " ] = & uintParser uint64Parser . SetValue ( uint64 ( 264 ) ) check [ " " ] = & uint64Parser check [ " " ] = & boolParser check [ " " ] = & boolParser timeParser . Set ( " " ) check [ " " ] = & timeParser float64Parser . SetValue ( 0.222 ) check [ " " ] = & float64Parser checkOwnerServers := sliceServerValue { ServerInfo { IP : " " } , } check [ " " ] = & checkOwnerServers if len ( check ) != len ( valMap ) { t . Errorf ( " " , len ( check ) , len ( valMap ) ) } for flag , parser := range valMap { if ! reflect . DeepEqual ( parser , check [ flag ] ) { t . Errorf ( " \n " , parser , check [ flag ] ) } } }
Test ParseArgs with all flags possible with custom parsers
Test ParseArgs with all flags possible with custom parsers
// We assume that getTypesRecursive works well // init parsers // init args // test // check