143 lines
3.6 KiB
Go
143 lines
3.6 KiB
Go
package schema
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseGlossaryYAML_Valid(t *testing.T) {
|
|
raw, err := os.ReadFile("testdata/glossary_valid.yaml")
|
|
if err != nil {
|
|
t.Fatalf("failed to read test fixture: %v", err)
|
|
}
|
|
|
|
glossary, err := ParseGlossaryYAML(raw)
|
|
if err != nil {
|
|
t.Fatalf("ParseGlossaryYAML failed: %v", err)
|
|
}
|
|
|
|
if len(glossary.Entries) != 2 {
|
|
t.Fatalf("expected 2 entries, got %d", len(glossary.Entries))
|
|
}
|
|
|
|
e1 := glossary.Entries[0]
|
|
if e1.Name != "Jesters" {
|
|
t.Errorf("expected entries[0].name = Jesters, got %q", e1.Name)
|
|
}
|
|
if e1.Category != "faction" {
|
|
t.Errorf("expected entries[0].category = faction, got %q", e1.Category)
|
|
}
|
|
if e1.Summary != "A faction name." {
|
|
t.Errorf("expected entries[0].summary = A faction name., got %q", e1.Summary)
|
|
}
|
|
if len(e1.Aliases) != 0 {
|
|
t.Errorf("expected entries[0].aliases = [], got %v", e1.Aliases)
|
|
}
|
|
|
|
e2 := glossary.Entries[1]
|
|
if e2.Name != "Popov" {
|
|
t.Errorf("expected entries[1].name = Popov, got %q", e2.Name)
|
|
}
|
|
if len(e2.Aliases) != 1 || e2.Aliases[0] != "Hrank" {
|
|
t.Errorf("expected entries[1].aliases = [Hrank], got %v", e2.Aliases)
|
|
}
|
|
}
|
|
|
|
func TestParseGlossaryYAML_MalformedYAML(t *testing.T) {
|
|
raw, err := os.ReadFile("testdata/glossary_malformed.yaml")
|
|
if err != nil {
|
|
t.Fatalf("failed to read test fixture: %v", err)
|
|
}
|
|
|
|
_, err = ParseGlossaryYAML(raw)
|
|
if err == nil {
|
|
t.Fatal("expected error for malformed YAML, got nil")
|
|
}
|
|
|
|
var parseErr *ParseError
|
|
if parseErr, ok := err.(*ParseError); !ok {
|
|
t.Errorf("expected *ParseError, got %T", err)
|
|
} else if parseErr.Message == "" {
|
|
t.Error("expected non-empty parse error message")
|
|
}
|
|
}
|
|
|
|
func TestParseGlossaryYAML_MissingRequiredFields(t *testing.T) {
|
|
raw, err := os.ReadFile("testdata/glossary_missing_fields.yaml")
|
|
if err != nil {
|
|
t.Fatalf("failed to read test fixture: %v", err)
|
|
}
|
|
|
|
_, err = ParseGlossaryYAML(raw)
|
|
if err == nil {
|
|
t.Fatal("expected error for missing required fields, got nil")
|
|
}
|
|
|
|
var valErr *ValidationError
|
|
if valErr, ok := err.(*ValidationError); !ok {
|
|
t.Errorf("expected *ValidationError, got %T", err)
|
|
} else if valErr.Field == "" {
|
|
t.Error("expected non-empty validation error field")
|
|
}
|
|
}
|
|
|
|
func TestParseGlossaryYAML_EmptyGlossary(t *testing.T) {
|
|
raw := []byte(`glossary: []`)
|
|
|
|
_, err := ParseGlossaryYAML(raw)
|
|
if err == nil {
|
|
t.Fatal("expected error for empty glossary, got nil")
|
|
}
|
|
|
|
var valErr *ValidationError
|
|
if valErr, ok := err.(*ValidationError); !ok {
|
|
t.Errorf("expected *ValidationError, got %T", err)
|
|
} else if valErr.Message != "must contain at least one entry" {
|
|
t.Errorf("expected 'must contain at least one entry', got %q", valErr.Message)
|
|
}
|
|
}
|
|
|
|
func TestParseGlossaryYAML_EmptyName(t *testing.T) {
|
|
raw := []byte(`
|
|
glossary:
|
|
- name: ""
|
|
category: faction
|
|
summary: A faction.
|
|
`)
|
|
|
|
_, err := ParseGlossaryYAML(raw)
|
|
if err == nil {
|
|
t.Fatal("expected error for empty name, got nil")
|
|
}
|
|
|
|
var valErr *ValidationError
|
|
if valErr, ok := err.(*ValidationError); !ok {
|
|
t.Errorf("expected *ValidationError, got %T", err)
|
|
} else if valErr.Message != "must not be empty" {
|
|
t.Errorf("expected 'must not be empty', got %q", valErr.Message)
|
|
}
|
|
}
|
|
|
|
func TestParseGlossaryYAML_EmptyAlias(t *testing.T) {
|
|
raw := []byte(`
|
|
glossary:
|
|
- name: Test
|
|
aliases:
|
|
- ""
|
|
category: faction
|
|
summary: A test entry.
|
|
`)
|
|
|
|
_, err := ParseGlossaryYAML(raw)
|
|
if err == nil {
|
|
t.Fatal("expected error for empty alias, got nil")
|
|
}
|
|
|
|
var valErr *ValidationError
|
|
if valErr, ok := err.(*ValidationError); !ok {
|
|
t.Errorf("expected *ValidationError, got %T", err)
|
|
} else if valErr.Message != "must not be empty" {
|
|
t.Errorf("expected 'must not be empty', got %q", valErr.Message)
|
|
}
|
|
}
|