github.com/sandwich-go/boost@v1.3.29/xconv/bool_test.go (about)

     1  package xconv
     2  
     3  import (
     4  	. "github.com/smartystreets/goconvey/convey"
     5  	"testing"
     6  )
     7  
     8  type testBool struct{ b bool }
     9  
    10  func (b testBool) Bool() bool { return b.b }
    11  
    12  type testStruct struct{}
    13  
    14  func TestBool(t *testing.T) {
    15  	Convey(`test bool`, t, func() {
    16  		for _, v := range []struct {
    17  			i  interface{}
    18  			is bool
    19  		}{
    20  			{nil, false}, {false, false}, {true, true},
    21  			{1, true}, {0, false}, {int8(1), true}, {int8(0), false},
    22  			{int16(1), true}, {int16(0), false}, {int32(1), true}, {int32(0), false}, {int64(1), true}, {int64(0), false},
    23  			{uint(1), true}, {uint(0), false}, {uint8(1), true}, {uint8(0), false},
    24  			{uint16(1), true}, {uint16(0), false}, {uint32(1), true}, {uint32(0), false}, {uint64(1), true}, {uint64(0), false},
    25  			{testBool{}, false}, {testBool{true}, true},
    26  			{&testStruct{}, true}, {[]int{}, false}, {map[int]int{}, false}, {[]int{1}, true}, {map[int]int{1: 1}, true},
    27  			{testStruct{}, true}, {1.0, true},
    28  		} {
    29  			if v.is {
    30  				So(Bool(v.i), ShouldBeTrue)
    31  			} else {
    32  				So(Bool(v.i), ShouldBeFalse)
    33  			}
    34  		}
    35  	})
    36  }