mirror of
https://github.com/golang/go
synced 2024-11-13 18:10:24 -07:00
A quick fix to ensure that json.Marshal returns errors correctly, as well as some error-related tests
R=rsc https://golang.org/cl/157151
This commit is contained in:
parent
5f484ec118
commit
9da8d88499
@ -392,12 +392,12 @@ func writeValue(w io.Writer, val reflect.Value) (err os.Error) {
|
|||||||
*reflect.InterfaceValue,
|
*reflect.InterfaceValue,
|
||||||
*reflect.PtrValue,
|
*reflect.PtrValue,
|
||||||
*reflect.UnsafePointerValue:
|
*reflect.UnsafePointerValue:
|
||||||
return &MarshalError{val.Type()}
|
err = &MarshalError{val.Type()}
|
||||||
default:
|
default:
|
||||||
value := val.(reflect.Value);
|
value := val.(reflect.Value);
|
||||||
fmt.Fprint(w, value.Interface());
|
fmt.Fprint(w, value.Interface());
|
||||||
}
|
}
|
||||||
return nil;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
func Marshal(w io.Writer, val interface{}) os.Error {
|
func Marshal(w io.Writer, val interface{}) os.Error {
|
||||||
|
@ -188,18 +188,47 @@ var marshalTests = []marshalTest{
|
|||||||
marshalTest{map[string][]int{"3": []int{1, 2, 3}}, `{"3":[1,2,3]}`},
|
marshalTest{map[string][]int{"3": []int{1, 2, 3}}, `{"3":[1,2,3]}`},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJsonMarshal(t *testing.T) {
|
func TestMarshal(t *testing.T) {
|
||||||
for _, tt := range marshalTests {
|
for _, tt := range marshalTests {
|
||||||
var buf bytes.Buffer;
|
var buf bytes.Buffer;
|
||||||
|
|
||||||
err := Marshal(&buf, tt.val);
|
err := Marshal(&buf, tt.val);
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error converting %s to JSON: \n", err.String())
|
t.Fatalf("Marshal(%T): %s", tt.val, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s := buf.String();
|
s := buf.String();
|
||||||
if s != tt.out {
|
if s != tt.out {
|
||||||
t.Errorf("Error converting to JSON. Expected: %q Actual %q\n", tt.out, s)
|
t.Errorf("Marshal(%T) = %q, want %q\n", tt.val, tt.out, s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type marshalErrorTest struct {
|
||||||
|
val interface{};
|
||||||
|
error string;
|
||||||
|
}
|
||||||
|
|
||||||
|
type MTE string
|
||||||
|
|
||||||
|
var marshalErrorTests = []marshalErrorTest{
|
||||||
|
marshalErrorTest{map[chan int]string{make(chan int): "one"}, "json cannot encode value of type map[chan int] string"},
|
||||||
|
marshalErrorTest{map[string]*MTE{"hi": nil}, "json cannot encode value of type *json.MTE"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarshalError(t *testing.T) {
|
||||||
|
for _, tt := range marshalErrorTests {
|
||||||
|
var buf bytes.Buffer;
|
||||||
|
|
||||||
|
err := Marshal(&buf, tt.val);
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("Marshal(%T): no error, want error %s", tt.val, tt.error)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err.String() != tt.error {
|
||||||
|
t.Fatalf("Marshal(%T) = error %s, want error %s", tt.val, err, tt.error)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user