summaryrefslogtreecommitdiff
path: root/compat/json/borrowed_encode_test.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-07 14:01:44 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-10 14:09:56 -0700
commit483bbdc970b26d774ace39edfde8420aba53b742 (patch)
treeee5fcacbba4d4eb7c44e4cb1f35fb950f11aee1d /compat/json/borrowed_encode_test.go
parent480ccfd05a13ac36516c536a71203280a31b4d28 (diff)
Sync borrowed code from Go 1.20
New tests mean encode.go and compat.go also need some bugfixes.
Diffstat (limited to 'compat/json/borrowed_encode_test.go')
-rw-r--r--compat/json/borrowed_encode_test.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/compat/json/borrowed_encode_test.go b/compat/json/borrowed_encode_test.go
index 3d5d675..999c694 100644
--- a/compat/json/borrowed_encode_test.go
+++ b/compat/json/borrowed_encode_test.go
@@ -14,6 +14,7 @@ import (
"math"
"reflect"
"regexp"
+ "runtime/debug"
"strconv"
"testing"
"unicode"
@@ -781,6 +782,42 @@ func TestIssue10281(t *testing.T) {
}
}
+//nolint:paralleltest // MODIFIED: added; can't be parallel because it fusses with the global GC.
+func TestMarshalErrorAndReuseEncodeState(t *testing.T) {
+ // Disable the GC temporarily to prevent encodeState's in Pool being cleaned away during the test.
+ percent := debug.SetGCPercent(-1)
+ defer debug.SetGCPercent(percent)
+
+ // Trigger an error in Marshal with cyclic data.
+ type Dummy struct {
+ Name string
+ Next *Dummy
+ }
+ dummy := Dummy{Name: "Dummy"}
+ dummy.Next = &dummy
+ if b, err := Marshal(dummy); err == nil {
+ t.Errorf("Marshal(dummy) = %#q; want error", b)
+ }
+
+ type Data struct {
+ A string
+ I int
+ }
+ data := Data{A: "a", I: 1}
+ b, err := Marshal(data)
+ if err != nil {
+ t.Errorf("Marshal(%v) = %v", data, err)
+ }
+
+ var data2 Data
+ if err := Unmarshal(b, &data2); err != nil {
+ t.Errorf("Unmarshal(%v) = %v", data2, err)
+ }
+ if data2 != data {
+ t.Errorf("expect: %v, but get: %v", data, data2)
+ }
+}
+
func TestHTMLEscape(t *testing.T) {
t.Parallel() // MODIFIED: added
var b, want bytes.Buffer