github.com/minio/console@v1.4.1/pkg/logger/config/bool-flag_test.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package config
    18  
    19  import "testing"
    20  
    21  // Test BoolFlag.String()
    22  func TestBoolFlagString(t *testing.T) {
    23  	var bf BoolFlag
    24  
    25  	testCases := []struct {
    26  		flag           BoolFlag
    27  		expectedResult string
    28  	}{
    29  		{bf, "off"},
    30  		{BoolFlag(true), "on"},
    31  		{BoolFlag(false), "off"},
    32  	}
    33  
    34  	for _, testCase := range testCases {
    35  		str := testCase.flag.String()
    36  		if testCase.expectedResult != str {
    37  			t.Fatalf("expected: %v, got: %v", testCase.expectedResult, str)
    38  		}
    39  	}
    40  }
    41  
    42  // Test BoolFlag.MarshalJSON()
    43  func TestBoolFlagMarshalJSON(t *testing.T) {
    44  	var bf BoolFlag
    45  
    46  	testCases := []struct {
    47  		flag           BoolFlag
    48  		expectedResult string
    49  	}{
    50  		{bf, `"off"`},
    51  		{BoolFlag(true), `"on"`},
    52  		{BoolFlag(false), `"off"`},
    53  	}
    54  
    55  	for _, testCase := range testCases {
    56  		data, _ := testCase.flag.MarshalJSON()
    57  		if testCase.expectedResult != string(data) {
    58  			t.Fatalf("expected: %v, got: %v", testCase.expectedResult, string(data))
    59  		}
    60  	}
    61  }
    62  
    63  // Test BoolFlag.UnmarshalJSON()
    64  func TestBoolFlagUnmarshalJSON(t *testing.T) {
    65  	testCases := []struct {
    66  		data           []byte
    67  		expectedResult BoolFlag
    68  		expectedErr    bool
    69  	}{
    70  		{[]byte(`{}`), BoolFlag(false), true},
    71  		{[]byte(`["on"]`), BoolFlag(false), true},
    72  		{[]byte(`"junk"`), BoolFlag(false), true},
    73  		{[]byte(`""`), BoolFlag(true), false},
    74  		{[]byte(`"on"`), BoolFlag(true), false},
    75  		{[]byte(`"off"`), BoolFlag(false), false},
    76  		{[]byte(`"true"`), BoolFlag(true), false},
    77  		{[]byte(`"false"`), BoolFlag(false), false},
    78  		{[]byte(`"ON"`), BoolFlag(true), false},
    79  		{[]byte(`"OFF"`), BoolFlag(false), false},
    80  	}
    81  
    82  	for _, testCase := range testCases {
    83  		var flag BoolFlag
    84  		err := (&flag).UnmarshalJSON(testCase.data)
    85  		if !testCase.expectedErr && err != nil {
    86  			t.Fatalf("error: expected = <nil>, got = %v", err)
    87  		}
    88  		if testCase.expectedErr && err == nil {
    89  			t.Fatalf("error: expected error, got = <nil>")
    90  		}
    91  		if err == nil && testCase.expectedResult != flag {
    92  			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, flag)
    93  		}
    94  	}
    95  }
    96  
    97  // Test ParseBoolFlag()
    98  func TestParseBoolFlag(t *testing.T) {
    99  	testCases := []struct {
   100  		flagStr        string
   101  		expectedResult BoolFlag
   102  		expectedErr    bool
   103  	}{
   104  		{"", BoolFlag(false), true},
   105  		{"junk", BoolFlag(false), true},
   106  		{"true", BoolFlag(true), false},
   107  		{"false", BoolFlag(false), false},
   108  		{"ON", BoolFlag(true), false},
   109  		{"OFF", BoolFlag(false), false},
   110  		{"on", BoolFlag(true), false},
   111  		{"off", BoolFlag(false), false},
   112  	}
   113  
   114  	for _, testCase := range testCases {
   115  		bf, err := ParseBoolFlag(testCase.flagStr)
   116  		if !testCase.expectedErr && err != nil {
   117  			t.Fatalf("error: expected = <nil>, got = %v", err)
   118  		}
   119  		if testCase.expectedErr && err == nil {
   120  			t.Fatalf("error: expected error, got = <nil>")
   121  		}
   122  		if err == nil && testCase.expectedResult != bf {
   123  			t.Fatalf("result: expected: %v, got: %v", testCase.expectedResult, bf)
   124  		}
   125  	}
   126  }