2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-03-15 00:22:39 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
|
|
package gconv_test
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2019-03-15 00:22:39 +08:00
|
|
|
)
|
|
|
|
|
|
2024-09-12 21:59:38 +08:00
|
|
|
var (
|
|
|
|
|
boolTestTrueValue = true
|
|
|
|
|
boolTestFalseValue = false
|
|
|
|
|
)
|
|
|
|
|
|
2024-05-20 19:40:30 +08:00
|
|
|
var boolTests = []struct {
|
|
|
|
|
value interface{}
|
|
|
|
|
expect bool
|
|
|
|
|
}{
|
|
|
|
|
{true, true},
|
|
|
|
|
{false, false},
|
2019-03-15 00:22:39 +08:00
|
|
|
|
2024-05-20 19:40:30 +08:00
|
|
|
{0, false},
|
|
|
|
|
{1, true},
|
|
|
|
|
|
|
|
|
|
{[]byte(""), false},
|
|
|
|
|
|
|
|
|
|
{"", false},
|
|
|
|
|
{"0", false},
|
|
|
|
|
{"1", true},
|
|
|
|
|
{"123.456", true},
|
|
|
|
|
{"true", true},
|
|
|
|
|
{"false", false},
|
|
|
|
|
{"on", true},
|
|
|
|
|
{"off", false},
|
|
|
|
|
|
|
|
|
|
{complex(1, 2), true},
|
|
|
|
|
{complex(123.456, 789.123), true},
|
|
|
|
|
|
|
|
|
|
{[3]int{1, 2, 3}, true},
|
|
|
|
|
{[]int{1, 2, 3}, true},
|
|
|
|
|
|
|
|
|
|
{map[int]int{1: 1}, true},
|
|
|
|
|
{map[string]string{"Earth": "印度洋"}, true},
|
|
|
|
|
|
|
|
|
|
{struct{}{}, true},
|
|
|
|
|
{&struct{}{}, true},
|
|
|
|
|
{nil, false},
|
2024-09-12 21:59:38 +08:00
|
|
|
{(*bool)(nil), false},
|
|
|
|
|
|
|
|
|
|
{&boolTestTrueValue, true},
|
|
|
|
|
{&boolTestFalseValue, false},
|
|
|
|
|
|
|
|
|
|
{myBool(true), true},
|
|
|
|
|
{myBool(false), false},
|
|
|
|
|
{(*myBool)(&boolTestTrueValue), true},
|
|
|
|
|
{(*myBool)(&boolTestFalseValue), false},
|
|
|
|
|
|
|
|
|
|
{(*myBool)(nil), false},
|
2024-05-20 19:40:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBool(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2024-05-20 19:40:30 +08:00
|
|
|
for _, test := range boolTests {
|
|
|
|
|
t.AssertEQ(gconv.Bool(test.value), test.expect)
|
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-15 00:22:39 +08:00
|
|
|
}
|