func TestPrintErrorInvalidArgument(t *testing.T) {
	// We assume that getTypesRecursive works well
	config := newConfiguration()
	flagMap := make(map[string]reflect.StructField)

	if err := getTypesRecursive(reflect.ValueOf(config), flagMap, ""); err != nil {
		t.Fatal(err)
	}

	// init parsers
	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

	// init args
	args := []string{
		"--timeout=ItsAnError",
	}

	// init defaultValMap
	checkDefaultStr := "DefaultOwnerNamePointer"
	checkDefaultDob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
	checkDob, _ := time.Parse(time.RFC3339, "1993-09-12T07:32:00Z")
	defaultValMap := map[string]reflect.Value{
		"loglevel":           reflect.ValueOf("DEBUG"),
		"timeout":            reflect.ValueOf(parse.Duration(time.Second)),
		"db":                 reflect.ValueOf(&DatabaseInfo{ServerInfo: ServerInfo{Watch: true, IP: "192.168.1.2", Load: 32, Load64: 64}, ConnectionMax: 3200000000, ConnectionMax64: 6400000000000000000}),
		"db.watch":           reflect.ValueOf(true),
		"db.ip":              reflect.ValueOf("192.168.1.2"),
		"db.load":            reflect.ValueOf(32),
		"db.load64":          reflect.ValueOf(int64(64)),
		"db.comax":           reflect.ValueOf(uint(3200000000)),
		"db.connectionmax64": reflect.ValueOf(uint64(6400000000000000000)),
		"owner":              reflect.ValueOf(&OwnerInfo{Name: &checkDefaultStr, DateOfBirth: checkDefaultDob, Rate: 0.111, Servers: []ServerInfo{{Watch: false, IP: "192.168.1.2", Load: 0, Load64: 0}, {Watch: false, IP: "192.168.1.3", Load: 0, Load64: 0}, {Watch: false, IP: "192.168.1.4", Load: 0, Load64: 0}}}),
		"owner.name":         reflect.ValueOf(&checkDefaultStr),
		"owner.dob":          reflect.ValueOf(checkDob),
		"owner.rate":         reflect.ValueOf(float64(0.999)),
		"owner.servers":      reflect.ValueOf(*new([]ServerInfo)),
	}

	// catch stdout
	rescueStdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w

	// Test
	checkErr := "invalid argument"
	_, err := parseArgs(args, flagMap, parsers)
	if err != nil && strings.Contains(err.Error(), checkErr) {
		PrintError(err, flagMap, defaultValMap, parsers)
	} else {
		t.Errorf("Expected Error : invalid argument got Error : %s", err)
	}

	// read and restore stdout
	err = w.Close()
	if err != nil {
		t.Fatal(err)
	}

	out, _ := ioutil.ReadAll(r)
	os.Stdout = rescueStdout

	// check
	if !strings.Contains(string(out), checkErr) {
		t.Errorf("Expexted error %s\ngot %s", checkErr, out)
	}
}
// Test Print Error with an invalid argument
Test Print Error with an invalid argument
// Test Print Error with an invalid argument
func TestPrintErrorInvalidArgument(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{
		"--timeout=ItsAnError",
	}
	checkDefaultStr := "DefaultOwnerNamePointer"
	checkDefaultDob, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
	checkDob, _ := time.Parse(time.RFC3339, "1993-09-12T07:32:00Z")
	defaultValMap := map[string]reflect.Value{
		"loglevel":           reflect.ValueOf("DEBUG"),
		"timeout":            reflect.ValueOf(parse.Duration(time.Second)),
		"db":                 reflect.ValueOf(&DatabaseInfo{ServerInfo: ServerInfo{Watch: true, IP: "192.168.1.2", Load: 32, Load64: 64}, ConnectionMax: 3200000000, ConnectionMax64: 6400000000000000000}),
		"db.watch":           reflect.ValueOf(true),
		"db.ip":              reflect.ValueOf("192.168.1.2"),
		"db.load":            reflect.ValueOf(32),
		"db.load64":          reflect.ValueOf(int64(64)),
		"db.comax":           reflect.ValueOf(uint(3200000000)),
		"db.connectionmax64": reflect.ValueOf(uint64(6400000000000000000)),
		"owner":              reflect.ValueOf(&OwnerInfo{Name: &checkDefaultStr, DateOfBirth: checkDefaultDob, Rate: 0.111, Servers: []ServerInfo{{Watch: false, IP: "192.168.1.2", Load: 0, Load64: 0}, {Watch: false, IP: "192.168.1.3", Load: 0, Load64: 0}, {Watch: false, IP: "192.168.1.4", Load: 0, Load64: 0}}}),
		"owner.name":         reflect.ValueOf(&checkDefaultStr),
		"owner.dob":          reflect.ValueOf(checkDob),
		"owner.rate":         reflect.ValueOf(float64(0.999)),
		"owner.servers":      reflect.ValueOf(*new([]ServerInfo)),
	}
	rescueStdout := os.Stdout
	r, w, _ := os.Pipe()
	os.Stdout = w
	checkErr := "invalid argument"
	_, err := parseArgs(args, flagMap, parsers)
	if err != nil && strings.Contains(err.Error(), checkErr) {
		PrintError(err, flagMap, defaultValMap, parsers)
	} else {
		t.Errorf("Expected Error : invalid argument got Error : %s", err)
	}
	err = w.Close()
	if err != nil {
		t.Fatal(err)
	}
	out, _ := ioutil.ReadAll(r)
	os.Stdout = rescueStdout
	if !strings.Contains(string(out), checkErr) {
		t.Errorf("Expexted error %s\ngot %s", checkErr, out)
	}
}
func TestPrintErrorInvalidArgument ( 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 { " " , } checkDefaultStr := " " checkDefaultDob , _ := time . Parse ( time . RFC3339 , " " ) checkDob , _ := time . Parse ( time . RFC3339 , " " ) defaultValMap := map [ string ] reflect . Value { " " : reflect . ValueOf ( " " ) , " " : reflect . ValueOf ( parse . Duration ( time . Second ) ) , " " : reflect . ValueOf ( & DatabaseInfo { ServerInfo : ServerInfo { Watch : true , IP : " " , Load : 32 , Load64 : 64 } , ConnectionMax : 3200000000 , ConnectionMax64 : 6400000000000000000 } ) , " " : reflect . ValueOf ( true ) , " " : reflect . ValueOf ( " " ) , " " : reflect . ValueOf ( 32 ) , " " : reflect . ValueOf ( int64 ( 64 ) ) , " " : reflect . ValueOf ( uint ( 3200000000 ) ) , " " : reflect . ValueOf ( uint64 ( 6400000000000000000 ) ) , " " : reflect . ValueOf ( & OwnerInfo { Name : & checkDefaultStr , DateOfBirth : checkDefaultDob , Rate : 0.111 , Servers : [ ] ServerInfo { { Watch : false , IP : " " , Load : 0 , Load64 : 0 } , { Watch : false , IP : " " , Load : 0 , Load64 : 0 } , { Watch : false , IP : " " , Load : 0 , Load64 : 0 } } } ) , " " : reflect . ValueOf ( & checkDefaultStr ) , " " : reflect . ValueOf ( checkDob ) , " " : reflect . ValueOf ( float64 ( 0.999 ) ) , " " : reflect . ValueOf ( * new ( [ ] ServerInfo ) ) , } rescueStdout := os . Stdout r , w , _ := os . Pipe ( ) os . Stdout = w checkErr := " " _ , err := parseArgs ( args , flagMap , parsers ) if err != nil && strings . Contains ( err . Error ( ) , checkErr ) { PrintError ( err , flagMap , defaultValMap , parsers ) } else { t . Errorf ( " " , err ) } err = w . Close ( ) if err != nil { t . Fatal ( err ) } out , _ := ioutil . ReadAll ( r ) os . Stdout = rescueStdout if ! strings . Contains ( string ( out ) , checkErr ) { t . Errorf ( " \n " , checkErr , out ) } }
Test Print Error with an invalid argument
Test Print Error with an invalid argument
// We assume that getTypesRecursive works well // init parsers // init args // init defaultValMap // catch stdout // Test // read and restore stdout // check