github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/ast/api_amd64_test.go (about) 1 //go:build amd64 && go1.16 && !go1.22 2 // +build amd64,go1.16,!go1.22 3 4 /* 5 * Copyright 2022 ByteDance Inc. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package ast 21 22 import ( 23 "encoding/json" 24 "testing" 25 26 "github.com/goshafaq/sonic/encoder" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestSortNodeTwitter(t *testing.T) { 31 root, err := NewSearcher(_TwitterJson).GetByPath() 32 if err != nil { 33 t.Fatal(err) 34 } 35 obj, err := root.MapUseNumber() 36 if err != nil { 37 t.Fatal(err) 38 } 39 exp, err := encoder.Encode(obj, encoder.SortMapKeys) 40 if err != nil { 41 t.Fatal(err) 42 } 43 var expObj interface{} 44 require.NoError(t, json.Unmarshal(exp, &expObj)) 45 46 if err := root.SortKeys(true); err != nil { 47 t.Fatal(err) 48 } 49 act, err := root.MarshalJSON() 50 if err != nil { 51 t.Fatal(err) 52 } 53 var actObj interface{} 54 require.NoError(t, json.Unmarshal(act, &actObj)) 55 require.Equal(t, expObj, actObj) 56 require.Equal(t, len(exp), len(act)) 57 require.Equal(t, string(exp), string(act)) 58 }