mirror of
https://github.com/json-iterator/go.git
synced 2025-04-23 11:37:32 +02:00
Merge pull request #104 from cch123/feature-add-bool-convert-test
add bool convert test map
This commit is contained in:
commit
f0b07a2313
@ -25,9 +25,7 @@ func (any *objectLazyAny) LastError() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToBool() bool {
|
func (any *objectLazyAny) ToBool() bool {
|
||||||
iter := any.cfg.BorrowIterator(any.buf)
|
return true
|
||||||
defer any.cfg.ReturnIterator(iter)
|
|
||||||
return iter.ReadObject() != ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (any *objectLazyAny) ToInt() int {
|
func (any *objectLazyAny) ToInt() int {
|
||||||
|
@ -35,7 +35,7 @@ func (any *stringAny) LastError() error {
|
|||||||
|
|
||||||
func (any *stringAny) ToBool() bool {
|
func (any *stringAny) ToBool() bool {
|
||||||
str := any.ToString()
|
str := any.ToString()
|
||||||
if str == "false" {
|
if str == "0" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, c := range str {
|
for _, c := range str {
|
||||||
|
@ -1,12 +1,44 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/json-iterator/go/require"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var boolConvertMap = map[string]bool{
|
||||||
|
"true": true,
|
||||||
|
"false": false,
|
||||||
|
|
||||||
|
`"true"`: true,
|
||||||
|
`"false"`: true,
|
||||||
|
|
||||||
|
"123": true,
|
||||||
|
"0": false,
|
||||||
|
`"0"`: false,
|
||||||
|
"-1": true,
|
||||||
|
|
||||||
|
"1.1": true,
|
||||||
|
"0.0": false,
|
||||||
|
"-1.1": true,
|
||||||
|
`""`: false,
|
||||||
|
"[1,2]": true,
|
||||||
|
"[]": false,
|
||||||
|
"{}": true,
|
||||||
|
`{"abc":1}`: true,
|
||||||
|
}
|
||||||
|
|
||||||
func Test_read_bool_as_any(t *testing.T) {
|
func Test_read_bool_as_any(t *testing.T) {
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
any := Get([]byte("true"))
|
|
||||||
should.True(any.ToBool())
|
var any Any
|
||||||
|
for k, v := range boolConvertMap {
|
||||||
|
any = Get([]byte(k))
|
||||||
|
if v {
|
||||||
|
should.True(any.ToBool(), fmt.Sprintf("origin val is %v", k))
|
||||||
|
} else {
|
||||||
|
should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/json-iterator/go/require"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/json-iterator/go/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_read_string_as_any(t *testing.T) {
|
func Test_read_string_as_any(t *testing.T) {
|
||||||
@ -13,7 +14,7 @@ func Test_read_string_as_any(t *testing.T) {
|
|||||||
any = Get([]byte(`" "`))
|
any = Get([]byte(`" "`))
|
||||||
should.False(any.ToBool())
|
should.False(any.ToBool())
|
||||||
any = Get([]byte(`"false"`))
|
any = Get([]byte(`"false"`))
|
||||||
should.False(any.ToBool())
|
should.True(any.ToBool())
|
||||||
any = Get([]byte(`"123"`))
|
any = Get([]byte(`"123"`))
|
||||||
should.Equal(123, any.ToInt())
|
should.Equal(123, any.ToInt())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user