github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/pkg/util/util_test.go (about) 1 /* 2 Copyright 2018 The OpenEBS Authors. 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 util 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestCheckTruthy(t *testing.T) { 27 tests := map[string]struct { 28 checkString string 29 expected bool 30 }{ 31 "1 should return true": {checkString: "1", expected: true}, 32 "yes should return true": {checkString: "Yes", expected: true}, 33 "ok should return true": {checkString: "Ok", expected: true}, 34 "true should return true": {checkString: "True", expected: true}, 35 "0 should return false": {checkString: "0", expected: false}, 36 "no should return false": {checkString: "No", expected: false}, 37 "false should return false": {checkString: "False", expected: false}, 38 "blank should return false": {checkString: "", expected: false}, 39 } 40 for name, test := range tests { 41 t.Run(name, func(t *testing.T) { 42 assert.Equal(t, test.expected, CheckTruthy(test.checkString)) 43 }) 44 } 45 } 46 47 func TestCheckFalsy(t *testing.T) { 48 tests := map[string]struct { 49 checkString string 50 expected bool 51 }{ 52 "1 should return false": {checkString: "1", expected: false}, 53 "yes should return false": {checkString: "Yes", expected: false}, 54 "ok should return false": {checkString: "Ok", expected: false}, 55 "true should return false": {checkString: "True", expected: false}, 56 "0 should return true": {checkString: "0", expected: true}, 57 "no should return true": {checkString: "No", expected: true}, 58 "false should return true": {checkString: "False", expected: true}, 59 "blank should return true": {checkString: "", expected: true}, 60 } 61 for name, test := range tests { 62 t.Run(name, func(t *testing.T) { 63 assert.Equal(t, test.expected, CheckFalsy(test.checkString)) 64 }) 65 } 66 } 67 68 func TestStringToInt32(t *testing.T) { 69 tests := map[string]struct { 70 numberString string 71 expected int32 72 }{ 73 "string 6 should return int 6": {numberString: "6", expected: 6}, 74 "string 18 should return int 18": {numberString: "18", expected: 18}, 75 "string 32 should return int 32": {numberString: "32", expected: 32}, 76 " should return nil pinter and one error": {numberString: "", expected: 0}, 77 "test should return nil pinter and one error": {numberString: "test", expected: 0}, 78 } 79 for name, test := range tests { 80 t.Run(name, func(t *testing.T) { 81 actualInt, err := StringToInt32(test.numberString) 82 if err != nil { 83 return 84 } 85 assert.Equal(t, test.expected, *actualInt) 86 }) 87 } 88 } 89 90 func TestStrToInt32(t *testing.T) { 91 tests := map[string]struct { 92 numberString string 93 expected int32 94 }{ 95 "string 6 should return int 6": {numberString: "6", expected: 6}, 96 "string 18 should return int 18": {numberString: "18", expected: 18}, 97 "string 32 should return int 32": {numberString: "32", expected: 32}, 98 } 99 for name, test := range tests { 100 t.Run(name, func(t *testing.T) { 101 actualInt := StrToInt32(test.numberString) 102 assert.Equal(t, test.expected, *actualInt) 103 }) 104 } 105 } 106 107 func TestHash(t *testing.T) { 108 tests := map[string]struct { 109 hashString string 110 expected string 111 }{ 112 "check hash for string1": {hashString: "This is one string", expected: "6192a12ec601c65b8375743eb66167ab"}, 113 "check hash for string2": {hashString: "This is one string", expected: "6192a12ec601c65b8375743eb66167ab"}, 114 } 115 for name, test := range tests { 116 t.Run(name, func(t *testing.T) { 117 assert.Equal(t, test.expected, Hash(test.hashString)) 118 }) 119 } 120 } 121 122 func TestCheckErr(t *testing.T) { 123 errStr := "This is a test string" 124 err := errors.New(errStr) 125 var msgChannel = make(chan string) 126 handlerFunc := func(str string) { 127 msgChannel <- str 128 } 129 go func() { 130 msg := <-msgChannel 131 assert.Equal(t, errStr, msg) 132 }() 133 CheckErr(err, handlerFunc) 134 CheckErr(nil, handlerFunc) 135 } 136 137 func TestStateStatus(t *testing.T) { 138 tests := map[string]struct { 139 status string 140 state bool 141 }{ 142 "status is enable for true state": {state: true, status: "enable"}, 143 "status is disable for false state": {state: false, status: "disable"}, 144 } 145 for name, test := range tests { 146 t.Run(name, func(t *testing.T) { 147 assert.Equal(t, test.status, StateStatus(test.state)) 148 }) 149 } 150 }