Skip to content

Commit

Permalink
pkg/encoding: add tests that Unmarshal rejects trailing input
Browse files Browse the repository at this point in the history
json correctly rejects trailing input whether it is valid or invalid,
like encoding/json.Unmarshal does, but yaml does not yet,
as already pointed out in the TODO I added to internal/encoding/yaml.
The following CL will fix the yaml package to do the right thing.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I01b381259ca0251bdb6e46dab60ca943226fe793
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193241
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
  • Loading branch information
mvdan committed Apr 18, 2024
1 parent 16edd40 commit e063155
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/encoding/json/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ unmarshal: [string]: X={
#result: json.Unmarshal(X)
}
unmarshal: t1: "1"
unmarshal: {
trailingValid: #"{"a": 1}{"b": 2}"#
trailingInvalid: #"{"a": 1}}invalid json"#
}

marshalStream: [string]: X={
[...] // input
Expand Down Expand Up @@ -88,6 +92,10 @@ validate.t2.result: error in call to encoding/json.Validate: invalid value 10 (o
./in.cue:6:10
./in.cue:9:27
json.Validate:1:6
unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
./in.cue:39:11
unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON:
./in.cue:39:11

Result:
import "encoding/json"
Expand Down Expand Up @@ -147,6 +155,12 @@ unmarshal: {
"1"
#result: 1
}
trailingValid: {
#result: _|_ // unmarshal.trailingValid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON
}
trailingInvalid: {
#result: _|_ // unmarshal.trailingInvalid.#result: error in call to encoding/json.Unmarshal: json: invalid JSON
}
}
marshalStream: {
t1: {
Expand Down
9 changes: 9 additions & 0 deletions pkg/encoding/yaml/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ unmarshalStream: {
null2: yaml.UnmarshalStream('1\n---\n---\n2')
null2: yaml.UnmarshalStream("1\n---\n---\n2")
}

unmarshalTrailingInput: {
valid: yaml.Unmarshal("1\n---\n2")
invalid: yaml.Unmarshal("1\n---\n}invalid yaml")
}
-- out/yaml --
Errors:
t1: error in call to encoding/yaml.Validate: invalid value 4 (out of bound <3):
Expand Down Expand Up @@ -75,3 +80,7 @@ unmarshalStream: {
null1: [1, null, 2]
null2: [1, null, 2]
}
unmarshalTrailingInput: {
valid: 1
invalid: 1
}

0 comments on commit e063155

Please sign in to comment.