github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/bool-flag_test.go (about)

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