1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00

Merge pull request #113 from cch123/feature-increase-coverage

increase coverage
This commit is contained in:
Tao Wen 2017-07-06 12:27:18 +08:00 committed by GitHub
commit 84ed6b3caf
7 changed files with 107 additions and 4 deletions

View File

@ -191,5 +191,7 @@ func Test_read_uint64_to_any(t *testing.T) {
func Test_int_lazy_any_get(t *testing.T) {
should := require.New(t)
any := Get([]byte("1234"))
// panic!!
//should.Equal(any.LastError(), io.EOF)
should.Equal(Invalid, any.Get(1, "2").ValueType())
}

View File

@ -3,8 +3,9 @@ package jsoniter
import (
"bytes"
"encoding/json"
"github.com/json-iterator/go/require"
"testing"
"github.com/json-iterator/go/require"
)
func Test_true(t *testing.T) {
@ -27,9 +28,10 @@ func Test_write_true_false(t *testing.T) {
stream := NewStream(ConfigDefault, buf, 4096)
stream.WriteTrue()
stream.WriteFalse()
stream.WriteBool(false)
stream.Flush()
should.Nil(stream.Error)
should.Equal("truefalse", buf.String())
should.Equal("truefalsefalse", buf.String())
}
func Test_write_val_bool(t *testing.T) {
@ -37,7 +39,9 @@ func Test_write_val_bool(t *testing.T) {
buf := &bytes.Buffer{}
stream := NewStream(ConfigDefault, buf, 4096)
stream.WriteVal(true)
should.Equal(stream.Buffered(), 4)
stream.Flush()
should.Equal(stream.Buffered(), 0)
should.Nil(stream.Error)
should.Equal("true", buf.String())
}

View File

@ -6,9 +6,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/json-iterator/go/require"
"strconv"
"testing"
"github.com/json-iterator/go/require"
)
func Test_read_big_float(t *testing.T) {
@ -111,6 +112,10 @@ func Test_write_float32(t *testing.T) {
stream.Flush()
should.Nil(stream.Error)
should.Equal("abcdefg1.123456", buf.String())
stream = NewStream(ConfigDefault, nil, 0)
stream.WriteFloat32(float32(0.0000001))
should.Equal("1e-07", string(stream.buf))
}
func Test_write_float64(t *testing.T) {
@ -144,6 +149,10 @@ func Test_write_float64(t *testing.T) {
stream.Flush()
should.Nil(stream.Error)
should.Equal("abcdefg1.123456", buf.String())
stream = NewStream(ConfigDefault, nil, 0)
stream.WriteFloat64(float64(0.0000001))
should.Equal("1e-07", string(stream.buf))
}
func Test_read_float64_cursor(t *testing.T) {

View File

@ -25,4 +25,6 @@ func Test_invalid(t *testing.T) {
should.Equal(float64(0), any.ToFloat64())
should.Equal("", any.ToString())
should.Equal(Invalid, any.Get(0.1).Get(1).ValueType())
}

View File

@ -2,9 +2,10 @@ package jsoniter
import (
"encoding/json"
"github.com/json-iterator/go/require"
"math/big"
"testing"
"github.com/json-iterator/go/require"
)
func Test_read_map(t *testing.T) {
@ -30,6 +31,14 @@ func Test_map_wrapper_any_get_all(t *testing.T) {
should := require.New(t)
any := Wrap(map[string][]int{"Field1": {1, 2}})
should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString())
should.Contains(any.Keys(), "Field1")
should.Equal(any.GetObject()["Field1"].ToInt(), 1)
// map write to
stream := NewStream(ConfigDefault, nil, 0)
any.WriteTo(stream)
// TODO cannot pass
//should.Equal(string(stream.buf), "")
}
func Test_write_val_map(t *testing.T) {

View File

@ -0,0 +1,71 @@
package jsoniter
import (
"testing"
"github.com/json-iterator/go/require"
)
// if must be valid is useless, just drop this test
func Test_must_be_valid(t *testing.T) {
should := require.New(t)
any := Get([]byte("123"))
should.Equal(any.MustBeValid().ToInt(), 123)
any = Wrap(int8(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(int16(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(int32(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(int64(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(uint(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(uint8(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(uint16(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(uint32(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(uint64(10))
should.Equal(any.MustBeValid().ToInt(), 10)
any = Wrap(float32(10))
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
any = Wrap(float64(10))
should.Equal(any.MustBeValid().ToFloat64(), float64(10))
any = Wrap(true)
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
any = Wrap(false)
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
any = Wrap(nil)
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
any = Wrap(struct{ age int }{age: 1})
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
any = Wrap(map[string]interface{}{"abc": 1})
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
any = Wrap("abc")
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
any = Wrap([]int{})
should.Equal(any.MustBeValid().ToFloat64(), float64(0))
any = Wrap([]int{1, 2})
should.Equal(any.MustBeValid().ToFloat64(), float64(1))
}

View File

@ -34,6 +34,7 @@ func Test_one_field(t *testing.T) {
should.Equal("a", field)
return true
}))
}
func Test_two_field(t *testing.T) {
@ -70,6 +71,11 @@ func Test_object_wrapper_any_get_all(t *testing.T) {
}
any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}})
should.Contains(any.Get('*', 0).ToString(), `"Field2":3`)
should.Contains(any.Keys(), "Field1")
should.Contains(any.Keys(), "Field2")
should.NotContains(any.Keys(), "Field3")
//should.Contains(any.GetObject()["Field1"].GetArray()[0], 1)
}
func Test_write_object(t *testing.T) {