Minimize D&D spell extraction contracts
This commit is contained in:
@@ -201,11 +201,9 @@ func spellList(names ...string) dnd.SpellList {
|
||||
|
||||
func validCast(name string) dnd.SpellCast {
|
||||
return dnd.SpellCast{
|
||||
Caster: "Aria",
|
||||
Spell: name,
|
||||
Effect: "heals an ally",
|
||||
NarrativeDescription: "Aria restores Borin.",
|
||||
SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}},
|
||||
Caster: "Aria",
|
||||
Spell: name,
|
||||
SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,12 +48,6 @@ func Validate(value dnd.SpellList) error {
|
||||
if strings.TrimSpace(spell.Spell) == "" {
|
||||
return fmt.Errorf("spell_casts[%d].spell must not be empty", index)
|
||||
}
|
||||
if strings.TrimSpace(spell.Effect) == "" {
|
||||
return fmt.Errorf("spell_casts[%d].effect must not be empty", index)
|
||||
}
|
||||
if strings.TrimSpace(spell.NarrativeDescription) == "" {
|
||||
return fmt.Errorf("spell_casts[%d].narrative_description must not be empty", index)
|
||||
}
|
||||
if len(spell.SourceRefs) == 0 {
|
||||
return fmt.Errorf("spell_casts[%d].source_refs must not be empty", index)
|
||||
}
|
||||
|
||||
@@ -34,17 +34,23 @@ func TestValidatorRejectsMissingSpellList(t *testing.T) {
|
||||
}
|
||||
|
||||
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)
|
||||
tests := []struct {
|
||||
name string
|
||||
mutate func(*dnd.SpellCast)
|
||||
}{
|
||||
{name: "blank caster", mutate: func(cast *dnd.SpellCast) { cast.Caster = " " }},
|
||||
{name: "blank spell", mutate: func(cast *dnd.SpellCast) { cast.Spell = "" }},
|
||||
{name: "empty evidence", mutate: func(cast *dnd.SpellCast) { cast.SourceRefs = nil }},
|
||||
}
|
||||
if result.Approved {
|
||||
t.Fatalf("Approved = true, want false")
|
||||
}
|
||||
if result.ReasonCode != ReasonCode {
|
||||
t.Fatalf("ReasonCode = %q, want %q", result.ReasonCode, ReasonCode)
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
value := validSpellList()
|
||||
test.mutate(&value.SpellCasts[0])
|
||||
result, err := New(Options{}).Validate(context.Background(), requestWithValue(value))
|
||||
if err != nil || result.Approved || result.ReasonCode != ReasonCode {
|
||||
t.Fatalf("Validate() = %#v, %v; want shape rejection", result, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,5 +75,5 @@ func requestWithValue(value dnd.SpellList) contracts.TypedValidationRequest[dnd.
|
||||
}
|
||||
|
||||
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}}}}}
|
||||
return dnd.SpellList{SpellCasts: []dnd.SpellCast{{Caster: "Aria", Spell: "Cure Wounds", SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: 1, EndUnitID: 1}}}}}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ func TestValidatorSpecCheckpointAndRegistration(t *testing.T) {
|
||||
|
||||
func requestWithValue(doc *source.SourceDocument, ref source.SourceRef) contracts.TypedValidationRequest[dnd.SpellList] {
|
||||
return contracts.TypedValidationRequest[dnd.SpellList]{Source: doc, Value: dnd.SpellList{SpellCasts: []dnd.SpellCast{{
|
||||
Caster: "Aria", Spell: "Cure Wounds", Effect: "heals", NarrativeDescription: "Aria casts Cure Wounds.", SourceRefs: []source.SourceRef{ref},
|
||||
Caster: "Aria", Spell: "Cure Wounds", SourceRefs: []source.SourceRef{ref},
|
||||
}}}}
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestValidatorWarnsWhenSpellDoesNotAppearInCitedText(t *testing.T) {
|
||||
|
||||
func TestValidatorMatchesCaseInsensitiveUnicodeMultiwordSpellAcrossCitations(t *testing.T) {
|
||||
value := dnd.SpellList{SpellCasts: []dnd.SpellCast{{
|
||||
Caster: "Aria", Spell: "Tasha's Hideous Laughter", Effect: "effect", NarrativeDescription: "description",
|
||||
Caster: "Aria", Spell: "Tasha's Hideous Laughter",
|
||||
SourceRefs: []source.SourceRef{
|
||||
{SourceID: "session", StartUnitID: 2, EndUnitID: 2},
|
||||
{SourceID: "session", StartUnitID: 1, EndUnitID: 2},
|
||||
@@ -108,7 +108,7 @@ func TestValidatorSpecCheckpointAndRegistration(t *testing.T) {
|
||||
|
||||
func requestWithSpell(doc *source.SourceDocument, name string, unitID int) contracts.TypedValidationRequest[dnd.SpellList] {
|
||||
return contracts.TypedValidationRequest[dnd.SpellList]{Source: doc, Value: dnd.SpellList{SpellCasts: []dnd.SpellCast{{
|
||||
Caster: "Aria", Spell: name, Effect: "effect", NarrativeDescription: "description",
|
||||
Caster: "Aria", Spell: name,
|
||||
SourceRefs: []source.SourceRef{{SourceID: "session", StartUnitID: unitID, EndUnitID: unitID}},
|
||||
}}}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user