| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  |  * MinIO Cloud Storage, (C) 2017 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | package cmd | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"errors" | 
					
						
							|  |  |  |  | 	"testing" | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | // Test BoolFlag.String()
 | 
					
						
							|  |  |  |  | func TestBoolFlagString(t *testing.T) { | 
					
						
							|  |  |  |  | 	var bf BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		flag           BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 		expectedResult string | 
					
						
							|  |  |  |  | 	}{ | 
					
						
							|  |  |  |  | 		{bf, "off"}, | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		{BoolFlag(true), "on"}, | 
					
						
							|  |  |  |  | 		{BoolFlag(false), "off"}, | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  |  | 		str := testCase.flag.String() | 
					
						
							|  |  |  |  | 		if testCase.expectedResult != str { | 
					
						
							|  |  |  |  | 			t.Fatalf("expected: %v, got: %v", testCase.expectedResult, str) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | // Test BoolFlag.MarshalJSON()
 | 
					
						
							|  |  |  |  | func TestBoolFlagMarshalJSON(t *testing.T) { | 
					
						
							|  |  |  |  | 	var bf BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	testCases := []struct { | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		flag           BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 		expectedResult string | 
					
						
							|  |  |  |  | 	}{ | 
					
						
							|  |  |  |  | 		{bf, `"off"`}, | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		{BoolFlag(true), `"on"`}, | 
					
						
							|  |  |  |  | 		{BoolFlag(false), `"off"`}, | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for _, testCase := range testCases { | 
					
						
							|  |  |  |  | 		data, _ := testCase.flag.MarshalJSON() | 
					
						
							|  |  |  |  | 		if testCase.expectedResult != string(data) { | 
					
						
							|  |  |  |  | 			t.Fatalf("expected: %v, got: %v", testCase.expectedResult, string(data)) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | // Test BoolFlag.UnmarshalJSON()
 | 
					
						
							|  |  |  |  | func TestBoolFlagUnmarshalJSON(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  |  | 		data           []byte | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		expectedResult BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 		expectedErr    error | 
					
						
							|  |  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		{[]byte(`{}`), BoolFlag(false), errors.New("json: cannot unmarshal object into Go value of type string")}, | 
					
						
							|  |  |  |  | 		{[]byte(`["on"]`), BoolFlag(false), errors.New("json: cannot unmarshal array into Go value of type string")}, | 
					
						
							|  |  |  |  | 		{[]byte(`"junk"`), BoolFlag(false), errors.New("invalid value ‘junk’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{[]byte(`"true"`), BoolFlag(false), errors.New("invalid value ‘true’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{[]byte(`"false"`), BoolFlag(false), errors.New("invalid value ‘false’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{[]byte(`"ON"`), BoolFlag(false), errors.New("invalid value ‘ON’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{[]byte(`"OFF"`), BoolFlag(false), errors.New("invalid value ‘OFF’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{[]byte(`""`), BoolFlag(true), nil}, | 
					
						
							|  |  |  |  | 		{[]byte(`"on"`), BoolFlag(true), nil}, | 
					
						
							|  |  |  |  | 		{[]byte(`"off"`), BoolFlag(false), nil}, | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for _, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		var flag BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 		err := (&flag).UnmarshalJSON(testCase.data) | 
					
						
							|  |  |  |  | 		if testCase.expectedErr == nil { | 
					
						
							|  |  |  |  | 			if err != nil { | 
					
						
							|  |  |  |  | 				t.Fatalf("error: expected = <nil>, got = %v", err) | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} else if err == nil { | 
					
						
							|  |  |  |  | 			t.Fatalf("error: expected = %v, got = <nil>", testCase.expectedErr) | 
					
						
							|  |  |  |  | 		} else if testCase.expectedErr.Error() != err.Error() { | 
					
						
							|  |  |  |  | 			t.Fatalf("error: expected = %v, got = %v", testCase.expectedErr, err) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if err == nil && testCase.expectedResult != flag { | 
					
						
							|  |  |  |  | 			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, flag) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | // Test ParseBoolFlag()
 | 
					
						
							|  |  |  |  | func TestParseBoolFlag(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  |  | 		flagStr        string | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		expectedResult BoolFlag | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 		expectedErr    error | 
					
						
							|  |  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		{"", BoolFlag(false), errors.New("invalid value ‘’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{"junk", BoolFlag(false), errors.New("invalid value ‘junk’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{"true", BoolFlag(false), errors.New("invalid value ‘true’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{"false", BoolFlag(false), errors.New("invalid value ‘false’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{"ON", BoolFlag(false), errors.New("invalid value ‘ON’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{"OFF", BoolFlag(false), errors.New("invalid value ‘OFF’ for BoolFlag")}, | 
					
						
							|  |  |  |  | 		{"on", BoolFlag(true), nil}, | 
					
						
							|  |  |  |  | 		{"off", BoolFlag(false), nil}, | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	for _, testCase := range testCases { | 
					
						
							| 
									
										
										
										
											2018-06-07 09:10:51 +08:00
										 |  |  |  | 		bf, err := ParseBoolFlag(testCase.flagStr) | 
					
						
							| 
									
										
										
										
											2017-03-27 03:00:27 +08:00
										 |  |  |  | 		if testCase.expectedErr == nil { | 
					
						
							|  |  |  |  | 			if err != nil { | 
					
						
							|  |  |  |  | 				t.Fatalf("error: expected = <nil>, got = %v", err) | 
					
						
							|  |  |  |  | 			} | 
					
						
							|  |  |  |  | 		} else if err == nil { | 
					
						
							|  |  |  |  | 			t.Fatalf("error: expected = %v, got = <nil>", testCase.expectedErr) | 
					
						
							|  |  |  |  | 		} else if testCase.expectedErr.Error() != err.Error() { | 
					
						
							|  |  |  |  | 			t.Fatalf("error: expected = %v, got = %v", testCase.expectedErr, err) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		if err == nil && testCase.expectedResult != bf { | 
					
						
							|  |  |  |  | 			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, bf) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |