Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/security_bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Jul 3, 2023
2 parents 721ac55 + b40bbd5 commit 2f9f1d9
Show file tree
Hide file tree
Showing 21 changed files with 1,029 additions and 652 deletions.
5 changes: 5 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,8 @@ func Get(src []byte, path ...interface{}) (ast.Node, error) {
func GetFromString(src string, path ...interface{}) (ast.Node, error) {
return ast.NewSearcher(src).GetByPath(path...)
}

// Valid reports whether data is a valid JSON encoding.
func Valid(data []byte) bool {
return ConfigDefault.Valid(data)
}
51 changes: 51 additions & 0 deletions api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2021 ByteDance Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package sonic

import (
`testing`

`github.com/stretchr/testify/require`
)

func TestValid(t *testing.T) {
require.False(t, Valid(nil))

testCase := []struct {
data string
expected bool
}{
{``, false},
{`s`, false},
{`{`, false},
{`[`, false},
{`[1,2`, false},
{`{"so":nic"}`, false},

{`null`, true},
{`""`, true},
{`1`, true},
{`"sonic"`, true},
{`{}`, true},
{`[]`, true},
{`[1,2]`, true},
{`{"so":"nic"}`, true},
}
for _, tc := range testCase {
require.Equal(t, tc.expected, Valid([]byte(tc.data)), tc.data)
}
}
16 changes: 12 additions & 4 deletions ast/api_amd64_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build amd64 && go1.15 && !go1.21
// +build amd64,go1.15,!go1.21

/*
Expand All @@ -15,14 +16,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ast

import (
`encoding/json`
`testing`

`github.com/bytedance/sonic/encoder`
`github.com/stretchr/testify/assert`
`github.com/stretchr/testify/require`
)

func TestSortNodeTwitter(t *testing.T) {
Expand All @@ -38,13 +40,19 @@ func TestSortNodeTwitter(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var expObj interface{}
require.NoError(t, json.Unmarshal(exp, &expObj))

if err := root.SortKeys(true); err != nil {
t.Fatal(err)
}
act, err := root.MarshalJSON()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(exp), len(act))
assert.Equal(t, string(exp), string(act))
var actObj interface{}
require.NoError(t, json.Unmarshal(act, &actObj))
require.Equal(t, expObj, actObj)
require.Equal(t, len(exp), len(act))
require.Equal(t, string(exp), string(act))
}

0 comments on commit 2f9f1d9

Please sign in to comment.