2019-03-15 00:22:39 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2019-03-15 00:22:39 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type boolStruct struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Bool(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.Case(t, func() {
|
|
|
|
var i interface{} = nil
|
|
|
|
gtest.AssertEQ(gconv.Bool(i), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool(false), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool(nil), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool(0), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool("0"), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool(""), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool("false"), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool("off"), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool([]byte{}), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool([]string{}), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool([]interface{}{}), false)
|
|
|
|
gtest.AssertEQ(gconv.Bool([]map[int]int{}), false)
|
2019-03-15 00:22:39 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
gtest.AssertEQ(gconv.Bool("1"), true)
|
|
|
|
gtest.AssertEQ(gconv.Bool("on"), true)
|
|
|
|
gtest.AssertEQ(gconv.Bool(1), true)
|
|
|
|
gtest.AssertEQ(gconv.Bool(123.456), true)
|
|
|
|
gtest.AssertEQ(gconv.Bool(boolStruct{}), true)
|
|
|
|
gtest.AssertEQ(gconv.Bool(&boolStruct{}), true)
|
|
|
|
})
|
2019-03-15 00:22:39 +08:00
|
|
|
}
|