2016-12-02 05:22:20 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-01-07 16:08:45 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-17 15:11:23 +02:00
|
|
|
"encoding/json"
|
2017-06-06 17:27:00 +02:00
|
|
|
"testing"
|
2017-07-06 05:44:39 +02:00
|
|
|
|
2017-07-07 03:13:25 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-01-07 16:08:45 +02:00
|
|
|
)
|
2016-12-02 05:22:20 +02:00
|
|
|
|
|
|
|
func Test_true(t *testing.T) {
|
2017-01-21 10:09:38 +02:00
|
|
|
should := require.New(t)
|
2017-06-17 04:21:37 +02:00
|
|
|
iter := ParseString(ConfigDefault, `true`)
|
2017-01-21 10:09:38 +02:00
|
|
|
should.True(iter.ReadBool())
|
2017-06-17 04:21:37 +02:00
|
|
|
iter = ParseString(ConfigDefault, `true`)
|
2017-01-21 10:09:38 +02:00
|
|
|
should.Equal(true, iter.Read())
|
2016-12-02 05:22:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_false(t *testing.T) {
|
2017-01-21 10:09:38 +02:00
|
|
|
should := require.New(t)
|
2017-06-17 04:21:37 +02:00
|
|
|
iter := ParseString(ConfigDefault, `false`)
|
2017-01-21 10:09:38 +02:00
|
|
|
should.False(iter.ReadBool())
|
2016-12-02 05:22:20 +02:00
|
|
|
}
|
2017-01-07 16:08:45 +02:00
|
|
|
|
|
|
|
func Test_write_true_false(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-17 04:21:37 +02:00
|
|
|
stream := NewStream(ConfigDefault, buf, 4096)
|
2017-01-07 16:08:45 +02:00
|
|
|
stream.WriteTrue()
|
|
|
|
stream.WriteFalse()
|
2017-07-06 05:44:39 +02:00
|
|
|
stream.WriteBool(false)
|
2017-01-07 16:08:45 +02:00
|
|
|
stream.Flush()
|
|
|
|
should.Nil(stream.Error)
|
2017-07-06 05:44:39 +02:00
|
|
|
should.Equal("truefalsefalse", buf.String())
|
2017-01-09 13:19:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_write_val_bool(t *testing.T) {
|
|
|
|
should := require.New(t)
|
|
|
|
buf := &bytes.Buffer{}
|
2017-06-17 04:21:37 +02:00
|
|
|
stream := NewStream(ConfigDefault, buf, 4096)
|
2017-01-09 13:19:48 +02:00
|
|
|
stream.WriteVal(true)
|
2017-07-06 05:44:39 +02:00
|
|
|
should.Equal(stream.Buffered(), 4)
|
2017-01-09 13:19:48 +02:00
|
|
|
stream.Flush()
|
2017-07-06 05:44:39 +02:00
|
|
|
should.Equal(stream.Buffered(), 0)
|
2017-01-09 13:19:48 +02:00
|
|
|
should.Nil(stream.Error)
|
|
|
|
should.Equal("true", buf.String())
|
2017-06-06 17:27:00 +02:00
|
|
|
}
|
2017-06-17 05:38:09 +02:00
|
|
|
|
|
|
|
func Test_encode_string_bool(t *testing.T) {
|
|
|
|
type TestObject struct {
|
|
|
|
Field bool `json:",omitempty,string"`
|
|
|
|
}
|
|
|
|
should := require.New(t)
|
|
|
|
output, err := json.Marshal(TestObject{true})
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(`{"Field":"true"}`, string(output))
|
|
|
|
output, err = Marshal(TestObject{true})
|
|
|
|
should.Nil(err)
|
|
|
|
should.Equal(`{"Field":"true"}`, string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_decode_string_bool(t *testing.T) {
|
|
|
|
type TestObject struct {
|
|
|
|
Field bool `json:",omitempty,string"`
|
|
|
|
}
|
|
|
|
should := require.New(t)
|
|
|
|
obj := TestObject{}
|
|
|
|
err := json.Unmarshal([]byte(`{"Field":"true"}`), &obj)
|
|
|
|
should.Nil(err)
|
|
|
|
should.True(obj.Field)
|
|
|
|
|
|
|
|
obj = TestObject{}
|
|
|
|
err = json.Unmarshal([]byte(`{"Field":true}`), &obj)
|
|
|
|
should.NotNil(err)
|
|
|
|
|
|
|
|
obj = TestObject{}
|
|
|
|
err = Unmarshal([]byte(`{"Field":"true"}`), &obj)
|
|
|
|
should.Nil(err)
|
|
|
|
should.True(obj.Field)
|
|
|
|
|
|
|
|
obj = TestObject{}
|
|
|
|
err = Unmarshal([]byte(`{"Field":true}`), &obj)
|
|
|
|
should.NotNil(err)
|
|
|
|
}
|