Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust tests for merging into stdlib #102

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions arshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ type (
// cannot be called on an unexported field.
netipAddr `json:"name"`

// structMethodText has a MarshalText method
// that cancels out netipAddr.MarshalText.
structMethodText
// Bogus MarshalText and AppendText methods are declared on
// structUnexportedEmbeddedMethodTag to prevent it from
// implementing those method interfaces.
}
structUnexportedEmbeddedStruct struct {
structOmitZeroAll
Expand Down Expand Up @@ -573,6 +573,9 @@ type (
}
)

func (structUnexportedEmbeddedMethodTag) MarshalText() {}
func (structUnexportedEmbeddedMethodTag) AppendText() {}

func (p *allMethods) MarshalJSONV2(enc *jsontext.Encoder, opts Options) error {
if got, want := "MarshalJSONV2", p.method; got != want {
return fmt.Errorf("called wrong method: got %v, want %v", got, want)
Expand Down Expand Up @@ -9223,7 +9226,9 @@ func TestCoderBufferGrowth(t *testing.T) {
}
}

bb := &bytesBuffer{new(bytes.Buffer)}
// bb is identical to bytes.Buffer,
// but a different type to avoid any optimizations for bytes.Buffer.
bb := struct{ *bytes.Buffer }{new(bytes.Buffer)}

var writeSizes []int
if err := MarshalWrite(WriterFunc(func(b []byte) (int, error) {
Expand Down
25 changes: 17 additions & 8 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package json
package json_test

import (
"bytes"
Expand All @@ -18,6 +18,7 @@ import (

jsonv1 "encoding/json"

jsonv2 "github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/internal/jsontest"
"github.com/go-json-experiment/json/jsontext"
)
Expand All @@ -28,6 +29,14 @@ var benchV1 = os.Getenv("BENCHMARK_V1") != ""
// but a different type to avoid any optimizations for bytes.Buffer.
type bytesBuffer struct{ *bytes.Buffer }

func addr[T any](v T) *T {
return &v
}

func len64[Bytes ~[]byte | ~string](in Bytes) int64 {
return int64(len(in))
}

var arshalTestdata = []struct {
name string
raw []byte
Expand Down Expand Up @@ -206,10 +215,10 @@ func (*jsonArshalerV1) UnmarshalJSON(b []byte) error {

type jsonArshalerV2 struct{ _ [4]int }

func (jsonArshalerV2) MarshalJSONV2(enc *jsontext.Encoder, opts Options) error {
func (jsonArshalerV2) MarshalJSONV2(enc *jsontext.Encoder, opts jsonv2.Options) error {
return enc.WriteToken(jsontext.String("method"))
}
func (*jsonArshalerV2) UnmarshalJSONV2(dec *jsontext.Decoder, opts Options) error {
func (*jsonArshalerV2) UnmarshalJSONV2(dec *jsontext.Decoder, opts jsonv2.Options) error {
b, err := dec.ReadValue()
if string(b) != `"method"` {
return fmt.Errorf("UnmarshalJSONV2: got %q, want %q", b, `"method"`)
Expand All @@ -226,7 +235,7 @@ func runUnmarshal(tb testing.TB) {
var val any
run := func(tb testing.TB) {
val = tt.new()
if err := Unmarshal(tt.raw, val); err != nil {
if err := jsonv2.Unmarshal(tt.raw, val); err != nil {
tb.Fatalf("Unmarshal error: %v", err)
}
}
Expand Down Expand Up @@ -266,7 +275,7 @@ func runMarshal(tb testing.TB) {
var raw []byte
run := func(tb testing.TB) {
var err error
raw, err = Marshal(tt.val)
raw, err = jsonv2.Marshal(tt.val)
if err != nil {
tb.Fatalf("Marshal error: %v", err)
}
Expand Down Expand Up @@ -347,7 +356,7 @@ func runAllTestdata(tb testing.TB) {

func mustUnmarshalValue(t testing.TB, data []byte, newValue func() any) (value any) {
value = newValue()
if err := Unmarshal(data, value); err != nil {
if err := jsonv2.Unmarshal(data, value); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
return value
Expand All @@ -370,11 +379,11 @@ func runArshal(t testing.TB, arshalName string, newValue func() any, data []byte

switch arshalName {
case "Marshal":
if _, err := Marshal(value); err != nil {
if _, err := jsonv2.Marshal(value); err != nil {
t.Fatalf("Marshal error: %v", err)
}
case "Unmarshal":
if err := Unmarshal(data, newValue()); err != nil {
if err := jsonv2.Unmarshal(data, newValue()); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
}
Expand Down
16 changes: 3 additions & 13 deletions fold_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"reflect"
"testing"
"unicode"

jsonv1 "encoding/json"
)

var equalFoldTestdata = []struct {
Expand Down Expand Up @@ -103,18 +101,10 @@ func TestFoldRune(t *testing.T) {
// TestBenchmarkUnmarshalUnknown unmarshals an unknown field into a struct with
// varying number of fields. Since the unknown field does not directly match
// any known field by name, it must fall back on case-insensitive matching.
func TestBenchmarkUnmarshalUnknown(t *testing.T) { runUnmarshalUnknown(t) }
func BenchmarkUnmarshalUnknown(b *testing.B) { runUnmarshalUnknown(b) }

func runUnmarshalUnknown(tb testing.TB) {
func TestBenchmarkUnmarshalUnknown(t *testing.T) {
in := []byte(`{"NameUnknown":null}`)
for _, n := range []int{1, 2, 5, 10, 20, 50, 100} {
unmarshal := Unmarshal
if benchV1 {
unmarshal = func(in []byte, out any, opts ...Options) error {
return jsonv1.Unmarshal(in, out)
}
}

var fields []reflect.StructField
for i := range n {
Expand All @@ -126,9 +116,9 @@ func runUnmarshalUnknown(tb testing.TB) {
}
out := reflect.New(reflect.StructOf(fields)).Interface()

runTestOrBench(tb, fmt.Sprintf("N%d", n), len64(in), func(tb testing.TB) {
t.Run(fmt.Sprintf("N%d", n), func(t *testing.T) {
if err := unmarshal(in, out); err != nil {
tb.Fatalf("Unmarshal error: %v", err)
t.Fatalf("Unmarshal error: %v", err)
}
})
}
Expand Down
Loading