Пример #1
0
func TestUnmarshalEmpty(t *testing.T) {
	record := ff.Everything{}
	err := record.UnmarshalJSON([]byte(`{}`))
	if err != nil {
		t.Fatalf("UnmarshalJSON: %v", err)
	}
}
Пример #2
0
func TestUnmarshalNullPointer(t *testing.T) {
	record := ff.Everything{}
	err := record.UnmarshalJSON([]byte(`{"FooStruct": null,"Something":99}`))
	if err != nil {
		t.Fatalf("UnmarshalJSON: %v", err)
	}
	if record.FooStruct != nil {
		t.Fatalf("record.Something decoding problem, expected: nil got: %v", record.FooStruct)
	}
}
Пример #3
0
func TestUnmarshalToReusedObject(t *testing.T) {
	JSONParts := []string{
		`"Bool":true`,
		`"Int":1`,
		`"Int8": 2`,
		`"Int16": 3`,
		`"Int32": -4`,
		`"Int64": 57`,
		`"Uint": 100`,
		`"Uint8": 101`,
		`"Uint16": 102`,
		`"Uint32": 50`,
		`"Uint64": 103`,
		`"Uintptr": 104`,
		`"Float32": 3.14`,
		`"Float64": 3.15`,
		`"Array": [1,2,3]`,
		`"Map": {"bar": 2,"foo": 1}`,
		`"String": "snowman☃\uD801\uDC37"`,
		`"StringPointer": "pointed snowman☃\uD801\uDC37"`,
		`"Int64Pointer": 44`,
		`"FooStruct": {"Bar": 1}`,
		`"MapMap": {"a0": {"b0":"foo"}, "a1":{"a2":"bar"}}`,
		`"MapArraySlice": {"foo":[[1,2,3],[4,5,6],[7]], "bar": [[1,2,3,4],[5,6,7]]}`,
		`"Something": 99`,
	}

	JSONWhole := "{" + strings.Join(JSONParts, ",") + "}"
	var record ff.Everything
	if err := record.UnmarshalJSON([]byte(JSONWhole)); err != nil {
		t.Fatalf("UnmarshalJSON: %v", err)
	}

	for _, part := range JSONParts {
		reuseRecord := record
		if err := reuseRecord.UnmarshalJSON([]byte("{" + part + "}")); err != nil {
			t.Fatalf("UnmarshalJSON: %v", err)
		}
		var emptyRecord ff.Everything
		if err := emptyRecord.UnmarshalJSON([]byte("{" + part + "}")); err != nil {
			t.Fatalf("UnmarshalJSON: %v", err)
		}

		if !reflect.DeepEqual(reuseRecord, emptyRecord) {
			t.Errorf("%#v should be equal to %#v", reuseRecord, emptyRecord)
		}
	}
}
Пример #4
0
func TestUnmarshalFull(t *testing.T) {
	record := ff.Everything{}
	// TODO(pquerna): add unicode snowman
	// TODO(pquerna): handle Bar subtype
	err := record.UnmarshalJSON([]byte(everythingJson))
	if err != nil {
		t.Fatalf("UnmarshalJSON: %v", err)
	}

	expect := "snowman☃𐐷"
	if record.String != expect {
		t.Fatalf("record.String decoding problem, expected: %v got: %v", expect, record.String)
	}

	if record.Something != 99 {
		t.Fatalf("record.Something decoding problem, expected: 99 got: %v", record.Something)
	}
}