diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2011-12-15 10:02:47 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2011-12-15 10:02:47 -0800 |
| commit | f89b5746fb809bef08eec46760ce429f420435fa (patch) | |
| tree | 88d28a270c483faa79b0b74dc58d1d28cfe9a085 /src | |
| parent | 1b82e03a8fd1a2558979d8870d973244505b5494 (diff) | |
| download | go-f89b5746fb809bef08eec46760ce429f420435fa.tar.xz | |
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
Diffstat (limited to 'src')
| -rw-r--r-- | src/pkg/encoding/json/decode_test.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/pkg/encoding/json/decode_test.go b/src/pkg/encoding/json/decode_test.go index bf3953eb05..e569fa2f70 100644 --- a/src/pkg/encoding/json/decode_test.go +++ b/src/pkg/encoding/json/decode_test.go @@ -6,6 +6,7 @@ package json import ( "bytes" + "fmt" "reflect" "strings" "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 { if isSpace(c) { return -1 |
