mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
json: some tests to demonstrate bad error messages
Not a fix yet (help wanted), but part of Issue 2331 R=rsc CC=golang-dev https://golang.org/cl/5490043
This commit is contained in:
parent
1b82e03a8f
commit
f89b5746fb
1 changed files with 33 additions and 0 deletions
|
|
@ -6,6 +6,7 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -242,6 +243,38 @@ func TestHTMLEscape(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WrongString is a struct that's misusing the ,string modifier.
|
||||||
|
type WrongString struct {
|
||||||
|
Message string `json:"result,string"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type wrongStringTest struct {
|
||||||
|
in, err string
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO(bradfitz): as part of Issue 2331, fix these tests' expected
|
||||||
|
// error values to be helpful, rather than the confusing messages they
|
||||||
|
// are now.
|
||||||
|
var wrongStringTests = []wrongStringTest{
|
||||||
|
{`{"result":"x"}`, "JSON decoder out of sync - data changing underfoot?"},
|
||||||
|
{`{"result":"foo"}`, "json: cannot unmarshal bool into Go value of type string"},
|
||||||
|
{`{"result":"123"}`, "json: cannot unmarshal number into Go value of type string"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// If people misuse the ,string modifier, the error message should be
|
||||||
|
// helpful, telling the user that they're doing it wrong.
|
||||||
|
func TestErrorMessageFromMisusedString(t *testing.T) {
|
||||||
|
for n, tt := range wrongStringTests {
|
||||||
|
r := strings.NewReader(tt.in)
|
||||||
|
var s WrongString
|
||||||
|
err := NewDecoder(r).Decode(&s)
|
||||||
|
got := fmt.Sprintf("%v", err)
|
||||||
|
if got != tt.err {
|
||||||
|
t.Errorf("%d. got err = %q, want %q", n, got, tt.err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func noSpace(c rune) rune {
|
func noSpace(c rune) rune {
|
||||||
if isSpace(c) {
|
if isSpace(c) {
|
||||||
return -1
|
return -1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue