storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/config/bool-flag_test.go (about) 1 /* 2 * MinIO Cloud Storage, (C) 2017-2019 MinIO, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package config 18 19 import ( 20 "testing" 21 ) 22 23 // Test BoolFlag.String() 24 func TestBoolFlagString(t *testing.T) { 25 var bf BoolFlag 26 27 testCases := []struct { 28 flag BoolFlag 29 expectedResult string 30 }{ 31 {bf, "off"}, 32 {BoolFlag(true), "on"}, 33 {BoolFlag(false), "off"}, 34 } 35 36 for _, testCase := range testCases { 37 str := testCase.flag.String() 38 if testCase.expectedResult != str { 39 t.Fatalf("expected: %v, got: %v", testCase.expectedResult, str) 40 } 41 } 42 } 43 44 // Test BoolFlag.MarshalJSON() 45 func TestBoolFlagMarshalJSON(t *testing.T) { 46 var bf BoolFlag 47 48 testCases := []struct { 49 flag BoolFlag 50 expectedResult string 51 }{ 52 {bf, `"off"`}, 53 {BoolFlag(true), `"on"`}, 54 {BoolFlag(false), `"off"`}, 55 } 56 57 for _, testCase := range testCases { 58 data, _ := testCase.flag.MarshalJSON() 59 if testCase.expectedResult != string(data) { 60 t.Fatalf("expected: %v, got: %v", testCase.expectedResult, string(data)) 61 } 62 } 63 } 64 65 // Test BoolFlag.UnmarshalJSON() 66 func TestBoolFlagUnmarshalJSON(t *testing.T) { 67 testCases := []struct { 68 data []byte 69 expectedResult BoolFlag 70 expectedErr bool 71 }{ 72 {[]byte(`{}`), BoolFlag(false), true}, 73 {[]byte(`["on"]`), BoolFlag(false), true}, 74 {[]byte(`"junk"`), BoolFlag(false), true}, 75 {[]byte(`""`), BoolFlag(true), false}, 76 {[]byte(`"on"`), BoolFlag(true), false}, 77 {[]byte(`"off"`), BoolFlag(false), false}, 78 {[]byte(`"true"`), BoolFlag(true), false}, 79 {[]byte(`"false"`), BoolFlag(false), false}, 80 {[]byte(`"ON"`), BoolFlag(true), false}, 81 {[]byte(`"OFF"`), BoolFlag(false), false}, 82 } 83 84 for _, testCase := range testCases { 85 var flag BoolFlag 86 err := (&flag).UnmarshalJSON(testCase.data) 87 if !testCase.expectedErr && err != nil { 88 t.Fatalf("error: expected = <nil>, got = %v", err) 89 } 90 if testCase.expectedErr && err == nil { 91 t.Fatalf("error: expected error, got = <nil>") 92 } 93 if err == nil && testCase.expectedResult != flag { 94 t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, flag) 95 } 96 } 97 } 98 99 // Test ParseBoolFlag() 100 func TestParseBoolFlag(t *testing.T) { 101 testCases := []struct { 102 flagStr string 103 expectedResult BoolFlag 104 expectedErr bool 105 }{ 106 {"", BoolFlag(false), true}, 107 {"junk", BoolFlag(false), true}, 108 {"true", BoolFlag(true), false}, 109 {"false", BoolFlag(false), false}, 110 {"ON", BoolFlag(true), false}, 111 {"OFF", BoolFlag(false), false}, 112 {"on", BoolFlag(true), false}, 113 {"off", BoolFlag(false), false}, 114 } 115 116 for _, testCase := range testCases { 117 bf, err := ParseBoolFlag(testCase.flagStr) 118 if !testCase.expectedErr && err != nil { 119 t.Fatalf("error: expected = <nil>, got = %v", err) 120 } 121 if testCase.expectedErr && err == nil { 122 t.Fatalf("error: expected error, got = <nil>") 123 } 124 if err == nil && testCase.expectedResult != bf { 125 t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, bf) 126 } 127 } 128 }