Fix JSON Unicode decoding and YAML float validation
This commit is contained in:
@@ -142,6 +142,7 @@ func TestDecodeProfile(t *testing.T) {
|
||||
"a: !custom value", "a: !!binary Zm9v", "a: !!set {x: null}", "a: !!omap [{x: 1}]",
|
||||
"a: &cycle [*cycle]", "a: *unknown", "a: .inf", "a: -.Inf", "a: .NaN",
|
||||
"a: !!float true", "a: !!int false", "a: [broken", "a: &x {b: 1}\nc: {<<: *x}",
|
||||
`a: !!float ""`, "a: !!float e3", "a: !!float +", "a: !!float .",
|
||||
} {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
if _, err := Decode([]byte(input)); err == nil || !strings.HasPrefix(err.Error(), "decode:") {
|
||||
@@ -177,12 +178,42 @@ copy: *items
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONDecoding(t *testing.T) {
|
||||
value, err := Decode([]byte(`{"schema_version":"maximumdirect.campaign.v1","id":"weather","name":"Weather \ud83c\udf27","observations":[{"amount":0.123456789012345678901,"scale":1e500,"available":true,"missing":null}]}`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]any{
|
||||
"schema_version": "maximumdirect.campaign.v1", "id": "weather", "name": "Weather 🌧",
|
||||
"observations": []any{map[string]any{
|
||||
"amount": json.Number("0.123456789012345678901"), "scale": json.Number("1e500"),
|
||||
"available": true, "missing": nil,
|
||||
}},
|
||||
}
|
||||
if !reflect.DeepEqual(value, want) {
|
||||
t.Fatalf("JSON decoding changed data: got %#v, want %#v", value, want)
|
||||
}
|
||||
if err := loadSet(t).Validate(value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, input := range []string{
|
||||
`{"a":1,"\u0061":2}`,
|
||||
`{"observations":[{"value":1,"value":2}]}`,
|
||||
`{"\ud83c\udf27":1,"🌧":2}`,
|
||||
} {
|
||||
if _, err := Decode([]byte(input)); err == nil || !strings.Contains(err.Error(), "duplicate mapping key") {
|
||||
t.Fatalf("expected duplicate-key failure for %s, got %v", input, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNumericPrecision(t *testing.T) {
|
||||
for input, want := range map[string]string{
|
||||
"1234567890123456789012345678901234567890": "1234567890123456789012345678901234567890",
|
||||
"0.123456789012345678901234567890": "0.123456789012345678901234567890",
|
||||
".5": "0.5", "5.": "5.0", "+.5": "0.5", "-0.5": "-0.5", "1e500": "1e500",
|
||||
"0x10": "16", "0o10": "8", "0b10": "2", "010": "8", "1_000": "1000",
|
||||
`!!float "0"`: "0", "!!float .5": "0.5", "!!float 5.": "5.0", "!!float 1e500": "1e500",
|
||||
} {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
value, err := Decode([]byte("measurement: " + input))
|
||||
|
||||
Reference in New Issue
Block a user