• Daniel Martí's avatar
    encoding/json: fix the broken "overwriting of data" tests · 95c3c430
    Daniel Martí authored
    Because TestUnmarshal actually allocates a new value to decode into
    using ptr's pointer type, any existing data is thrown away. This was
    harmless in alomst all of the test cases, minus the "overwriting of
    data" ones added in 2015 in CL 12209.
    
    I spotted that nothing covered decoding a JSON array with few elements
    into a slice which already had many elements. I initially assumed that
    the code was buggy or that some code could be removed, when in fact
    there simply wasn't any code covering the edge case.
    
    Move those two tests to TestPrefilled, which already served a very
    similar purpose. Remove the map case, as TestPrefilled already has
    plenty of prefilled map cases. Moreover, we no longer reset an entire
    map when decoding, as per the godoc:
    
    	To unmarshal a JSON object into a map, Unmarshal first
    	establishes a map to use. If the map is nil, Unmarshal allocates
    	a new map. Otherwise Unmarshal reuses the existing map, keeping
    	existing entries.
    
    Finally, to ensure that ptr is used correctly in the future, make
    TestUnmarshal error if it's anything other than a pointer to a zero
    value. That is, the only correct use should be new(type). Don't rename
    the ptr field, as that would be extremely noisy and cause unwanted merge
    conflicts.
    
    Change-Id: I41e3ecfeae42d877ac5443a6bd622ac3d6c8120c
    Reviewed-on: https://go-review.googlesource.com/c/go/+/185738
    Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
    TryBot-Result: Gobot Gobot <gobot@golang.org>
    Reviewed-by: default avatarEmmanuel Odeke <emm.odeke@gmail.com>
    95c3c430
decode_test.go 57.3 KB