Skip to content

Commit

Permalink
internal/encoding/yaml: add test cases for YAML 1.1 and 1.2 octals
Browse files Browse the repository at this point in the history
YAML 1.1 has octal numbers with the "0" prefix,
whereas YAML 1.2 has octal numbers with the "0o" prefix.
Both are used in the wild, so we must be able to handle both.

Right now, a non-octal number beginning with "0" results in an error,
whereas most YAML decoders, including go-yaml's Unmarshal,
fall back to decoding the value as a string.
We will implement a fix like that in a follow-up commit;
this commit simply adds test cases to show the current behavior.

Also add cases where YAML 1.1 and 1.2 octal integer literals
are prefixed with an explicit !!float tag, where the user definitely
does not mean the input to be an octal integer.
We don't want any change in our handling of octal integers to change
these cases, even if one of the cases is broken per the added TODO.

For #2578.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I46f54f43d7f47c83ec907e421a37b049ad9d3137
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195046
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed May 23, 2024
1 parent 9394167 commit c41955b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions internal/encoding/yaml/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,21 @@ var unmarshalTests = []struct {
"decimal: +685_230",
"decimal: +685_230",
}, {
"octal: 02472256",
"octal: 0o2472256",
"octal_yaml11: 02472256",
"octal_yaml11: 0o2472256",
}, {
"octal_yaml12: 0o2472256",
"octal_yaml12: 0o2472256",
}, {
"not_octal_yaml12: 0o123456789",
`not_octal_yaml12: "0o123456789"`,
}, {
// TODO(mvdan): 01234 is not a valid numeric literal in CUE.
"float_octal_yaml11: !!float 01234",
"float_octal_yaml11: number & 01234",
}, {
"float_octal_yaml12: !!float 0o1234",
"float_octal_yaml12: number & 0o1234",
}, {
"hexa: 0x_0A_74_AE",
"hexa: 0x_0A_74_AE",
Expand Down Expand Up @@ -916,6 +929,11 @@ var unmarshalErrorTests = []struct {
{"{{.}}", `test.yaml:1: invalid map key: !!map`},
{"b: *a\na: &a {c: 1}", `test.yaml: unknown anchor 'a' referenced`},
{"%TAG !%79! tag:yaml.org,2002:\n---\nv: !%79!int '1'", "test.yaml: did not find expected whitespace"},
// TODO(mvdan): just like not_octal_yaml12, we should interpret the value as a string.
{
"not_octal_yaml11: 0123456789",
`test.yaml:1: cannot decode "0123456789" as !!float: illegal integer number "0123456789"`,
},
}

func TestUnmarshalErrors(t *testing.T) {
Expand Down

0 comments on commit c41955b

Please sign in to comment.