From c41955b39207044bde0d68933cc0cf9af239bb58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 22 May 2024 10:30:34 +0100 Subject: [PATCH] internal/encoding/yaml: add test cases for YAML 1.1 and 1.2 octals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: I46f54f43d7f47c83ec907e421a37b049ad9d3137 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195046 TryBot-Result: CUEcueckoo Reviewed-by: Roger Peppe Unity-Result: CUE porcuepine --- internal/encoding/yaml/decode_test.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/encoding/yaml/decode_test.go b/internal/encoding/yaml/decode_test.go index e994f66f3d0..377bcc063ac 100644 --- a/internal/encoding/yaml/decode_test.go +++ b/internal/encoding/yaml/decode_test.go @@ -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", @@ -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) {