// SPDX-License-Identifier: MIT package template import ( "encoding/json" "fmt" "math" "os" "testing" ) // TestPlainScalarsAgainstReference compares how plain scalars are typed with // the JavaScript yaml library the reference uses. testdata/scalars.json is // generated by gen/scalars.mjs. func TestPlainScalarsAgainstReference(t *testing.T) { data, err := os.ReadFile("testdata/scalars.json") if err != nil { t.Fatal(err) } var want map[string]string if err := json.Unmarshal(data, &want); err != nil { t.Fatal(err) } for src, w := range want { res := readYAMLDocument("f.yml", []byte("k: "+src+"\n")) if w == "ERR" { continue } if len(res.errs) > 0 { t.Errorf("%q: error %v, reference %s", src, res.errs[0], w) continue } var got string switch v := res.root.vals[0].lit.(type) { case *Null: got = "null" case *Number: if math.IsNaN(v.Value) { got = "num:NaN" } else { got = "num:" + JSNumberString(v.Value) } case *Boolean: got = fmt.Sprintf("bool:%v", v.Value) case *String: got = "str:" + v.Value } if got != w { t.Errorf("%q: got %s, reference %s", src, got, w) } } }