Verify current catalog examples through native ConfigKit loading
This commit is contained in:
@@ -23,8 +23,8 @@ func loadSet(t *testing.T) *Set {
|
||||
func TestDocuments(t *testing.T) {
|
||||
set := loadSet(t)
|
||||
directories := []string{"tests/valid", "tests/invalid"}
|
||||
for _, record := range set.Catalog.Schemas {
|
||||
directories = append(directories, filepath.Dir(record.Path)+"/examples")
|
||||
for _, schema := range set.Current() {
|
||||
directories = append(directories, filepath.Dir(schema.Record.Path)+"/examples")
|
||||
}
|
||||
for _, directory := range directories {
|
||||
err := filepath.WalkDir(filepath.Join("../..", directory), func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
@@ -39,7 +39,7 @@ func TestDocuments(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := Decode(data)
|
||||
value, err := LoadDomain(path, data)
|
||||
var before []byte
|
||||
if err == nil {
|
||||
before, err = json.Marshal(value)
|
||||
@@ -49,6 +49,9 @@ func TestDocuments(t *testing.T) {
|
||||
}
|
||||
if directory == "tests/invalid" {
|
||||
phase, _, _ := strings.Cut(entry.Name(), "-")
|
||||
if phase == "decode" {
|
||||
phase = "load"
|
||||
}
|
||||
if err == nil || !strings.HasPrefix(err.Error(), phase+":") {
|
||||
t.Fatalf("want %s failure, got %v", phase, err)
|
||||
}
|
||||
@@ -100,7 +103,7 @@ func TestDefinedFieldShapes(t *testing.T) {
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
value, err := Decode([]byte("schema_version: maximumdirect.glossary.v1\n" + test.body))
|
||||
value, err := LoadDomain("fixture.yml", []byte("schema_version: maximumdirect.glossary.v1\n"+test.body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -119,7 +122,7 @@ func TestDefinedFieldShapes(t *testing.T) {
|
||||
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: null}}}", false},
|
||||
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 0}]}}}", false},
|
||||
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.5}]}}}", false},
|
||||
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.0000000000000000000001}]}}}", false},
|
||||
{"schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira, classes: [{name: scout, level: 1.0000000000000000000001}]}}}", true},
|
||||
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {}", true},
|
||||
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {glossaries: []}", true},
|
||||
{"schema_version: maximumdirect.rpg.campaign.v1\nid: example\nreferences: {party: null}", false},
|
||||
@@ -129,7 +132,7 @@ func TestDefinedFieldShapes(t *testing.T) {
|
||||
{"schema_version: maximumdirect.rpg.campaign.v1\nname: Example", false},
|
||||
} {
|
||||
t.Run(test.body, func(t *testing.T) {
|
||||
value, err := Decode([]byte(test.body))
|
||||
value, err := LoadDomain("fixture.yml", []byte(test.body))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -140,99 +143,6 @@ func TestDefinedFieldShapes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeProfile(t *testing.T) {
|
||||
for _, input := range []string{
|
||||
"a: 1\na: 2", "a: {b: 1, b: 2}", `{"a":1,"a":2}`,
|
||||
"---\na: 1\n---\na: 2", "1: value", "true: value", "? [a, b]\n: value",
|
||||
"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:") {
|
||||
t.Fatalf("expected decoding failure, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
value, err := Decode([]byte(`
|
||||
date: 2026-11-28
|
||||
timestamp: 2026-11-28T12:30:00Z
|
||||
affirmative: yes
|
||||
switch: on
|
||||
enabled: true
|
||||
missing: ~
|
||||
quoted: "true"
|
||||
quoted_number: "1e500"
|
||||
tagged_string: !!str 1e500
|
||||
"<<": literal extension
|
||||
original: &items [one, two]
|
||||
copy: *items
|
||||
`))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]any{
|
||||
"date": "2026-11-28", "timestamp": "2026-11-28T12:30:00Z",
|
||||
"affirmative": "yes", "switch": "on", "enabled": true, "missing": nil, "quoted": "true",
|
||||
"quoted_number": "1e500", "tagged_string": "1e500",
|
||||
"<<": "literal extension", "original": []any{"one", "two"}, "copy": []any{"one", "two"},
|
||||
}
|
||||
if !reflect.DeepEqual(value, want) {
|
||||
t.Fatalf("got %#v, want %#v", value, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJSONDecoding(t *testing.T) {
|
||||
value, err := Decode([]byte(`{"schema_version":"maximumdirect.rpg.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.rpg.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 := validateFixture(t, loadSet(t), 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))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
number, ok := value.(map[string]any)["measurement"].(json.Number)
|
||||
if !ok || number.String() != want {
|
||||
t.Fatalf("want exact JSON number %q, got %#v", want, value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestYAMLJSONEquivalence(t *testing.T) {
|
||||
yamlBytes, err := os.ReadFile("../../glossary/v0.1.0/examples/weather.yml")
|
||||
if err != nil {
|
||||
@@ -242,11 +152,11 @@ func TestYAMLJSONEquivalence(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
yamlValue, err := Decode(yamlBytes)
|
||||
yamlValue, err := LoadDomain("weather.yml", yamlBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonValue, err := Decode(jsonBytes)
|
||||
jsonValue, err := LoadDomain("weather.json", jsonBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -257,7 +167,7 @@ func TestYAMLJSONEquivalence(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
roundTrip, err := Decode(data)
|
||||
roundTrip, err := LoadDomain("roundtrip.json", data)
|
||||
if err != nil || !reflect.DeepEqual(yamlValue, roundTrip) {
|
||||
t.Fatalf("JSON conversion changed data: %v", err)
|
||||
}
|
||||
@@ -267,13 +177,13 @@ func TestYAMLJSONEquivalence(t *testing.T) {
|
||||
// Public CLI selection is always explicit and tested separately.
|
||||
func validateFixture(t *testing.T, set *Set, value any) error {
|
||||
t.Helper()
|
||||
selection := "glossary@0.1.0"
|
||||
selection := "glossary"
|
||||
if doc, ok := value.(map[string]any); ok {
|
||||
switch doc["schema_version"] {
|
||||
case "maximumdirect.rpg.party.v1":
|
||||
selection = "rpg/party@0.1.0"
|
||||
selection = "rpg/party"
|
||||
case "maximumdirect.rpg.campaign.v1":
|
||||
selection = "rpg/campaign@0.1.0"
|
||||
selection = "rpg/campaign"
|
||||
}
|
||||
}
|
||||
schema, err := set.Select(selection)
|
||||
@@ -302,19 +212,19 @@ func TestDistributionAndOfflineResolution(t *testing.T) {
|
||||
}
|
||||
files[record.Path] = &fstest.MapFile{Data: data}
|
||||
}
|
||||
partyPath := "rpg/party/v0.1.0/schema.json"
|
||||
partyPath := "rpg/party/v0.2.0/schema.json"
|
||||
original := files[partyPath].Data
|
||||
replacement := `"name": {"$ref": "` + schemaBase + `glossary/v0.1.0/schema.json#/$defs/text"}`
|
||||
replacement := `"name": {"$ref": "` + schemaBase + `glossary/v0.2.0/schema.json#/$defs/text"}`
|
||||
files[partyPath] = &fstest.MapFile{Data: []byte(strings.Replace(string(original), `"name": {"$ref": "#/$defs/text"}`, replacement, 1))}
|
||||
set, err := Load(files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
selected, err := set.Select("rpg/party@0.1.0")
|
||||
selected, err := set.Select("rpg/party")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value, err := Decode([]byte("schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira}}}"))
|
||||
value, err := LoadDomain("fixture.yml", []byte("schema_version: maximumdirect.rpg.party.v1\ncharacters: {mira: {character: {name: Mira}}}"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user