1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00
json-iterator/jsoniter_any_bool_test.go

65 lines
1.2 KiB
Go
Raw Normal View History

2017-06-17 21:32:48 +08:00
package jsoniter
import (
2017-07-03 19:10:49 +08:00
"fmt"
2017-06-19 23:43:53 +08:00
"testing"
2017-07-03 19:10:49 +08:00
"github.com/stretchr/testify/require"
2017-06-17 21:32:48 +08:00
)
2017-07-03 19:10:49 +08:00
var boolConvertMap = map[string]bool{
2017-07-04 17:02:46 +08:00
"null": false,
2017-07-03 19:10:49 +08:00
"true": true,
"false": false,
`"true"`: true,
`"false"`: true,
2017-07-04 15:41:28 +08:00
"123": true,
`"123"`: true,
"0": false,
`"0"`: false,
"-1": true,
`"-1"`: true,
2017-07-03 19:10:49 +08:00
"1.1": true,
"0.0": false,
"-1.1": true,
`""`: false,
"[1,2]": true,
"[]": false,
"{}": true,
`{"abc":1}`: true,
}
2017-06-17 21:32:48 +08:00
func Test_read_bool_as_any(t *testing.T) {
should := require.New(t)
2017-07-03 19:10:49 +08:00
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))
}
}
2017-07-05 11:40:20 +08:00
}
func Test_write_bool_to_stream(t *testing.T) {
should := require.New(t)
any := Get([]byte("true"))
stream := NewStream(ConfigDefault, nil, 32)
any.WriteTo(stream)
should.Equal("true", string(stream.Buffer()))
should.Equal(any.ValueType(), BoolValue)
2017-07-05 11:40:20 +08:00
any = Get([]byte("false"))
stream = NewStream(ConfigDefault, nil, 32)
any.WriteTo(stream)
should.Equal("false", string(stream.Buffer()))
should.Equal(any.ValueType(), BoolValue)
2017-06-17 21:32:48 +08:00
}