...
1 package any_tests
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/json-iterator/go"
8 "github.com/stretchr/testify/require"
9 )
10
11 var boolConvertMap = map[string]bool{
12 "null": false,
13 "true": true,
14 "false": false,
15
16 `"true"`: true,
17 `"false"`: true,
18
19 "123": true,
20 `"123"`: true,
21 "0": false,
22 `"0"`: false,
23 "-1": true,
24 `"-1"`: true,
25
26 "1.1": true,
27 "0.0": false,
28 "-1.1": true,
29 `""`: false,
30 "[1,2]": true,
31 "[]": false,
32 "{}": true,
33 `{"abc":1}`: true,
34 }
35
36 func Test_read_bool_as_any(t *testing.T) {
37 should := require.New(t)
38
39 var any jsoniter.Any
40 for k, v := range boolConvertMap {
41 any = jsoniter.Get([]byte(k))
42 if v {
43 should.True(any.ToBool(), fmt.Sprintf("origin val is %v", k))
44 } else {
45 should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k))
46 }
47 }
48
49 }
50
51 func Test_write_bool_to_stream(t *testing.T) {
52 should := require.New(t)
53 any := jsoniter.Get([]byte("true"))
54 stream := jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
55 any.WriteTo(stream)
56 should.Equal("true", string(stream.Buffer()))
57 should.Equal(any.ValueType(), jsoniter.BoolValue)
58
59 any = jsoniter.Get([]byte("false"))
60 stream = jsoniter.NewStream(jsoniter.ConfigDefault, nil, 32)
61 any.WriteTo(stream)
62 should.Equal("false", string(stream.Buffer()))
63
64 should.Equal(any.ValueType(), jsoniter.BoolValue)
65 }
66
View as plain text