68 lines
2.2 KiB
Go
68 lines
2.2 KiB
Go
package shape
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
|
)
|
|
|
|
func TestValidatorApprovesWellFormedSpellPayload(t *testing.T) {
|
|
result, err := New(Options{}).Validate(context.Background(), requestWithValue(validSpellList()))
|
|
if err != nil {
|
|
t.Fatalf("Validate() error = %v, want nil", err)
|
|
}
|
|
if !result.Approved {
|
|
t.Fatalf("Validate() = %#v, want approved", result)
|
|
}
|
|
}
|
|
|
|
func TestValidatorRejectsMissingSpellList(t *testing.T) {
|
|
result, err := New(Options{}).Validate(context.Background(), requestWithValue(dnd.SpellList{}))
|
|
if err != nil {
|
|
t.Fatalf("Validate() error = %v, want nil", err)
|
|
}
|
|
if result.Approved {
|
|
t.Fatalf("Approved = true, want false")
|
|
}
|
|
if result.ReasonCode != ReasonCode {
|
|
t.Fatalf("ReasonCode = %q, want %q", result.ReasonCode, ReasonCode)
|
|
}
|
|
}
|
|
|
|
func TestValidatorRejectsMissingRequiredSpellFields(t *testing.T) {
|
|
value := validSpellList()
|
|
value.SpellCasts[0].Spell = ""
|
|
result, err := New(Options{}).Validate(context.Background(), requestWithValue(value))
|
|
if err != nil {
|
|
t.Fatalf("Validate() error = %v, want nil", err)
|
|
}
|
|
if result.Approved {
|
|
t.Fatalf("Approved = true, want false")
|
|
}
|
|
if result.ReasonCode != ReasonCode {
|
|
t.Fatalf("ReasonCode = %q, want %q", result.ReasonCode, ReasonCode)
|
|
}
|
|
}
|
|
|
|
func TestSpecAndRegister(t *testing.T) {
|
|
registry := pipeline.NewValidatorRegistry()
|
|
if err := Register(registry); err != nil {
|
|
t.Fatalf("Register() error = %v, want nil", err)
|
|
}
|
|
if _, err := DecodeOptions(map[string]any{"unexpected": true}); err == nil {
|
|
t.Fatal("DecodeOptions() error = nil, want unknown option error")
|
|
}
|
|
}
|
|
|
|
func requestWithValue(value dnd.SpellList) contracts.TypedValidationRequest[dnd.SpellList] {
|
|
return contracts.TypedValidationRequest[dnd.SpellList]{Value: value}
|
|
}
|
|
|
|
func validSpellList() dnd.SpellList {
|
|
return dnd.SpellList{SpellCasts: []dnd.SpellCast{{Caster: "Aria", Spell: "Cure Wounds", Effect: "heals", NarrativeDescription: "Aria heals Borin.", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}}}
|
|
}
|