Preserve YAML string types and explicit numeric evidence
This commit is contained in:
@@ -39,7 +39,7 @@ func TestNormalizeReport(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestYAMLReportValues(t *testing.T) {
|
||||
input := `large: 123456789012345678901234567890
|
||||
input := `large: !!int 123456789012345678901234567890
|
||||
hex: 0xff
|
||||
fraction: .125
|
||||
text: "false"
|
||||
@@ -94,8 +94,8 @@ func TestDecodeYAMLReportRejectsUnsupportedValues(t *testing.T) {
|
||||
|
||||
func TestYAMLReportLargeNumericScalars(t *testing.T) {
|
||||
for _, tc := range []struct{ input, want string }{
|
||||
{"1e9999", "1e9999"}, {"0x100000000000000000000", "1208925819614629174706176"},
|
||||
{"+01.2500e+9999", "1.2500e+9999"},
|
||||
{"!!float 1e9999", "1e9999"}, {"!!int 0x100000000000000000000", "1208925819614629174706176"},
|
||||
{"!!float +01.2500e+9999", "1.2500e+9999"},
|
||||
} {
|
||||
t.Run(tc.input, func(t *testing.T) {
|
||||
got, err := DecodeYAMLReport(strings.NewReader(tc.input))
|
||||
@@ -104,7 +104,7 @@ func TestYAMLReportLargeNumericScalars(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, input := range []string{`"1e9999"`, `!!str 1e9999`, `.`, `hello`} {
|
||||
for _, input := range []string{`1e9999`, `"1e9999"`, `!!str 1e9999`, `.`, `hello`} {
|
||||
got, err := DecodeYAMLReport(strings.NewReader(input))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -133,3 +133,37 @@ func TestReportYAMLNodePreservesNumericLookingStrings(t *testing.T) {
|
||||
t.Fatalf("%s: got %#v, want %#v", data, got, value)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeYAMLReportPreservesPlainNativeStrings(t *testing.T) {
|
||||
// Native ConfigKit safe output can contain an unquoted numeric-looking string
|
||||
// when the YAML resolver considers the token outside the numeric range.
|
||||
value, err := DecodeYAMLReport(strings.NewReader(`1e400: 1e400
|
||||
quoted: "1e400"
|
||||
explicit: !!str 1e400
|
||||
number: !!float 1e400
|
||||
`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]any{"1e400": "1e400", "quoted": "1e400", "explicit": "1e400", "number": json.Number("1e400")}
|
||||
if !reflect.DeepEqual(value, want) {
|
||||
t.Fatalf("decoded=%#v want=%#v", value, want)
|
||||
}
|
||||
for _, format := range []Format{FormatJSON, FormatYAML} {
|
||||
var output bytes.Buffer
|
||||
if err := RenderReport(&output, Report{Value: value}, format); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got any
|
||||
if format == FormatJSON {
|
||||
decoder := json.NewDecoder(&output)
|
||||
decoder.UseNumber()
|
||||
err = decoder.Decode(&got)
|
||||
} else {
|
||||
got, err = DecodeYAMLReport(&output)
|
||||
}
|
||||
if err != nil || !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("format %v: got=%#v err=%v", format, got, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user