1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Copyright © 2014 Steve Francia <spf@spf13.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package cast_test
import (
"encoding/json"
"html/template"
"testing"
"time"
"github.com/spf13/cast"
)
func TestBool(t *testing.T) {
var ptr *bool
testCases := []testCase{
{0, false, false},
{int(0), false, false},
{int8(0), false, false},
{int16(0), false, false},
{int32(0), false, false},
{int64(0), false, false},
{uint(0), false, false},
{uint8(0), false, false},
{uint16(0), false, false},
{uint32(0), false, false},
{uint64(0), false, false},
{float32(0), false, false},
{float32(0.0), false, false},
{float64(0), false, false},
{float64(0.0), false, false},
{time.Duration(0), false, false},
{json.Number("0"), false, false},
{nil, false, false},
{ptr, false, false},
{"false", false, false},
{"FALSE", false, false},
{"False", false, false},
{"f", false, false},
{"F", false, false},
{false, false, false},
{"true", true, false},
{"TRUE", true, false},
{"True", true, false},
{"t", true, false},
{"T", true, false},
{1, true, false},
{int(1), true, false},
{int8(1), true, false},
{int16(1), true, false},
{int32(1), true, false},
{int64(1), true, false},
{uint(1), true, false},
{uint8(1), true, false},
{uint16(1), true, false},
{uint32(1), true, false},
{uint64(1), true, false},
{float32(1), true, false},
{float32(1.0), true, false},
{float64(1), true, false},
{float64(1.0), true, false},
{time.Duration(1), true, false},
{json.Number("1"), true, false},
{json.Number("1.0"), true, false},
{true, true, false},
{-1, true, false},
{int(-1), true, false},
{int8(-1), true, false},
{int16(-1), true, false},
{int32(-1), true, false},
{int64(-1), true, false},
// Alias
{MyBool(true), true, false},
{MyBool(false), false, false},
// Failure cases
{"test", false, true},
{testing.T{}, false, true},
}
runTests(t, testCases, cast.ToBool, cast.ToBoolE)
}
func BenchmarkToBool(b *testing.B) {
for i := 0; i < b.N; i++ {
if !cast.ToBool(true) {
b.Fatal("ToBool returned false")
}
}
}
func TestString(t *testing.T) {
type Key struct {
k string
}
key := &Key{"foo"}
var ptr *string
testCases := []testCase{
{int(8), "8", false},
{int8(8), "8", false},
{int16(8), "8", false},
{int32(8), "8", false},
{int64(8), "8", false},
{uint(8), "8", false},
{uint8(8), "8", false},
{uint16(8), "8", false},
{uint32(8), "8", false},
{uint64(8), "8", false},
{float32(8.31), "8.31", false},
{float64(8.31), "8.31", false},
{json.Number("8"), "8", false},
{true, "true", false},
{false, "false", false},
{nil, "", false},
{ptr, "", false},
{[]byte("one time"), "one time", false},
{"one more time", "one more time", false},
{template.HTML("one time"), "one time", false},
{template.URL("http://somehost.foo"), "http://somehost.foo", false},
{template.JS("(1+2)"), "(1+2)", false},
{template.CSS("a"), "a", false},
{template.HTMLAttr("a"), "a", false},
// Alias
{MyString("foo"), "foo", false},
// Stringer and error
{foo{val: "bar"}, "bar", false},
{fu{val: "bar"}, "bar", false},
// Failure cases
{testing.T{}, "", true},
{key, "", true},
}
runTests(t, testCases, cast.ToString, cast.ToStringE)
}
type foo struct {
val string
}
func (x foo) String() string {
return x.val
}
type fu struct {
val string
}
func (x fu) Error() string {
return x.val
}
|