Move scene shape facts into the scene family
This commit is contained in:
69
internal/modules/dnd/scenedescriptions/checks.go
Normal file
69
internal/modules/dnd/scenedescriptions/checks.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Package scenedescriptions owns reusable scene-description facts.
|
||||
package scenedescriptions
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
type Field string
|
||||
|
||||
const (
|
||||
ScenesMissing Field = "scenes_missing"
|
||||
ScenesEmpty Field = "scenes_empty"
|
||||
ID Field = "id"
|
||||
Kind Field = "kind"
|
||||
Title Field = "title"
|
||||
Summary Field = "summary"
|
||||
SourceRef Field = "source_ref"
|
||||
)
|
||||
|
||||
// Issue identifies a malformed field; Index is -1 for the list itself.
|
||||
type Issue struct {
|
||||
Index int
|
||||
Field Field
|
||||
}
|
||||
|
||||
// Check reports shape facts without choosing a validator policy or guidance.
|
||||
func Check(value dnd.SceneDescriptionList) []Issue {
|
||||
var issues []Issue
|
||||
if value.Scenes == nil {
|
||||
issues = append(issues, Issue{Index: -1, Field: ScenesMissing})
|
||||
} else if len(value.Scenes) == 0 {
|
||||
issues = append(issues, Issue{Index: -1, Field: ScenesEmpty})
|
||||
}
|
||||
for index, scene := range value.Scenes {
|
||||
if !nonblankTrimmed(scene.ID) {
|
||||
issues = append(issues, Issue{Index: index, Field: ID})
|
||||
}
|
||||
if !ValidKind(scene.Kind) {
|
||||
issues = append(issues, Issue{Index: index, Field: Kind})
|
||||
}
|
||||
if !nonblankTrimmed(scene.Title) {
|
||||
issues = append(issues, Issue{Index: index, Field: Title})
|
||||
}
|
||||
if !nonblankTrimmed(scene.Summary) {
|
||||
issues = append(issues, Issue{Index: index, Field: Summary})
|
||||
}
|
||||
if !nonblankTrimmed(scene.SourceRef.SourceID) || scene.SourceRef.StartUnitID <= 0 || scene.SourceRef.EndUnitID <= 0 {
|
||||
issues = append(issues, Issue{Index: index, Field: SourceRef})
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
|
||||
func nonblankTrimmed(value string) bool {
|
||||
return strings.TrimSpace(value) != "" && value == strings.TrimSpace(value)
|
||||
}
|
||||
|
||||
// ValidKind is shared with the durable codec, whose other checks intentionally
|
||||
// differ from the selectable shape validator.
|
||||
func ValidKind(value dnd.SceneKind) bool {
|
||||
switch value {
|
||||
case dnd.SceneKindCombat, dnd.SceneKindNarrative, dnd.SceneKindRecap, dnd.SceneKindMeta:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
38
internal/modules/dnd/scenedescriptions/checks_test.go
Normal file
38
internal/modules/dnd/scenedescriptions/checks_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package scenedescriptions
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func TestCheckReportsSceneFieldFacts(t *testing.T) {
|
||||
valid := dnd.SceneDescription{ID: "id", Kind: dnd.SceneKindNarrative, Title: "Title", Summary: "Summary", SourceRef: source.SourceRef{SourceID: "source", StartUnitID: 2, EndUnitID: 1}}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
value dnd.SceneDescriptionList
|
||||
want []Issue
|
||||
}{
|
||||
{"missing", dnd.SceneDescriptionList{}, []Issue{{-1, ScenesMissing}}},
|
||||
{"empty", dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{}}, []Issue{{-1, ScenesEmpty}}},
|
||||
{"valid reversed range is a separate check", dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{valid}}, nil},
|
||||
{"multiple valid scenes", dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{valid, valid}}, nil},
|
||||
{"all invalid fields", dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{{ID: " id ", Kind: "other", Title: " ", Summary: " untrimmed ", SourceRef: source.SourceRef{SourceID: " source ", StartUnitID: 0, EndUnitID: 1}}}}, []Issue{{0, ID}, {0, Kind}, {0, Title}, {0, Summary}, {0, SourceRef}}},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := Check(test.value); !reflect.DeepEqual(got, test.want) {
|
||||
t.Fatalf("Check() = %#v, want %#v", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, kind := range []dnd.SceneKind{dnd.SceneKindCombat, dnd.SceneKindNarrative, dnd.SceneKindRecap, dnd.SceneKindMeta} {
|
||||
if !ValidKind(kind) {
|
||||
t.Fatalf("ValidKind(%q) = false", kind)
|
||||
}
|
||||
}
|
||||
if ValidKind(" narrative ") || ValidKind("") {
|
||||
t.Fatal("accepted unsupported kind")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user