github.com/blend/go-sdk@v1.20220411.3/stringutil/parse_bool_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package stringutil
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  	"github.com/blend/go-sdk/ex"
    15  	"github.com/blend/go-sdk/uuid"
    16  )
    17  
    18  func TestParseBool(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	testCases := [...]struct {
    22  		Input    string
    23  		Expected bool
    24  		Err      error
    25  	}{
    26  		{"true", true, nil},
    27  		{"t", true, nil},
    28  		{"yes", true, nil},
    29  		{"y", true, nil},
    30  		{"1", true, nil},
    31  		{"enabled", true, nil},
    32  		{"on", true, nil},
    33  
    34  		{"false", false, nil},
    35  		{"f", false, nil},
    36  		{"no", false, nil},
    37  		{"n", false, nil},
    38  		{"0", false, nil},
    39  		{"disabled", false, nil},
    40  		{"off", false, nil},
    41  
    42  		{"foo", false, ErrInvalidBoolValue},
    43  		{"", false, ErrInvalidBoolValue},
    44  		{"00", false, ErrInvalidBoolValue},
    45  		{uuid.V4().String(), false, ErrInvalidBoolValue},
    46  	}
    47  
    48  	var boolValue bool
    49  	var err error
    50  	for _, tc := range testCases {
    51  		boolValue, err = ParseBool(tc.Input)
    52  		if tc.Err != nil {
    53  			assert.Equal(tc.Err, ex.ErrClass(err))
    54  		} else {
    55  			assert.Equal(tc.Expected, boolValue)
    56  		}
    57  	}
    58  }