github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_bool_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gconv_test 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/test/gtest" 13 "github.com/gogf/gf/v2/util/gconv" 14 ) 15 16 var ( 17 boolTestTrueValue = true 18 boolTestFalseValue = false 19 ) 20 21 var boolTests = []struct { 22 value interface{} 23 expect bool 24 }{ 25 {true, true}, 26 {false, false}, 27 28 {0, false}, 29 {1, true}, 30 31 {[]byte(""), false}, 32 33 {"", false}, 34 {"0", false}, 35 {"1", true}, 36 {"123.456", true}, 37 {"true", true}, 38 {"false", false}, 39 {"on", true}, 40 {"off", false}, 41 42 {complex(1, 2), true}, 43 {complex(123.456, 789.123), true}, 44 45 {[3]int{1, 2, 3}, true}, 46 {[]int{1, 2, 3}, true}, 47 48 {map[int]int{1: 1}, true}, 49 {map[string]string{"Earth": "印度洋"}, true}, 50 51 {struct{}{}, true}, 52 {&struct{}{}, true}, 53 {nil, false}, 54 {(*bool)(nil), false}, 55 56 {&boolTestTrueValue, true}, 57 {&boolTestFalseValue, false}, 58 59 {myBool(true), true}, 60 {myBool(false), false}, 61 {(*myBool)(&boolTestTrueValue), true}, 62 {(*myBool)(&boolTestFalseValue), false}, 63 64 {(*myBool)(nil), false}, 65 } 66 67 func TestBool(t *testing.T) { 68 gtest.C(t, func(t *gtest.T) { 69 for _, test := range boolTests { 70 t.AssertEQ(gconv.Bool(test.value), test.expect) 71 } 72 }) 73 }