Prepare item and enemy event sort keys once
This commit is contained in:
@@ -93,6 +93,7 @@ type normalizedRecord struct {
|
||||
event dnd.EnemyEvent
|
||||
identity string
|
||||
inputIndex int
|
||||
orderKey enemyeventmodel.CanonicalOrderKey
|
||||
}
|
||||
|
||||
type nameCanonicalization struct {
|
||||
@@ -108,7 +109,7 @@ func normalizeList(input dnd.EnemyEventList, order shared.SourceRefOrder, regist
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
for index, inputEvent := range input.Events {
|
||||
event, nameChange, refsChanged := normalizeEvent(inputEvent, order, registry)
|
||||
records[index] = normalizedRecord{event: event, identity: enemyeventmodel.CanonicalIdentity(event), inputIndex: index}
|
||||
records[index] = normalizedRecord{event: event, identity: enemyeventmodel.CanonicalIdentity(event), inputIndex: index, orderKey: enemyeventmodel.PrepareCanonicalOrder(order, event)}
|
||||
if nameChange != nil {
|
||||
findings = append(findings, diagnostics.Finding{
|
||||
Scope: eventScope(index),
|
||||
@@ -128,7 +129,8 @@ func normalizeList(input dnd.EnemyEventList, order shared.SourceRefOrder, regist
|
||||
}
|
||||
|
||||
sort.SliceStable(records, func(left, right int) bool {
|
||||
return enemyeventmodel.Less(order, records[left].event, records[right].event)
|
||||
l, r := records[left], records[right]
|
||||
return enemyeventmodel.CanonicalLessPrepared(order, l.event, r.event, l.orderKey, r.orderKey)
|
||||
})
|
||||
for position, record := range records {
|
||||
if position == record.inputIndex {
|
||||
|
||||
@@ -81,6 +81,28 @@ func TestNormalizeUsesExplicitKindOrderAndPreservesDistinctObservations(t *testi
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeOrdersTiedEnemyEvidenceWithoutMergingIt(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "session", Units: []source.SourceUnit{{ID: 50}, {ID: 10}, {ID: 90}}}
|
||||
ref := func(id int) source.SourceRef {
|
||||
return source.SourceRef{SourceID: doc.ID, StartUnitID: id, EndUnitID: id}
|
||||
}
|
||||
event := func(refs ...source.SourceRef) dnd.EnemyEvent {
|
||||
return dnd.EnemyEvent{Name: "Ashfang", Kind: dnd.EnemyEventKindEngaged, SourceRefs: refs}
|
||||
}
|
||||
earlier, later, invalid := event(ref(50), ref(10)), event(ref(50), ref(90)), event(ref(999))
|
||||
input := dnd.EnemyEventList{Events: []dnd.EnemyEvent{later, invalid, earlier, later}}
|
||||
result, err := runNormalize(newNormalizer(t, npcReferences(t)), context.Background(), normalizeRequest(doc, input, contracts.ReferenceSet{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []dnd.EnemyEvent{earlier, later, invalid}; !reflect.DeepEqual(result.Value.Events, want) {
|
||||
t.Fatalf("events = %#v, want %#v", result.Value.Events, want)
|
||||
}
|
||||
if !hasDiagnostic(result.Diagnostics, ReasonCodeEventsReordered) || !hasDiagnostic(result.Diagnostics, ReasonCodeDuplicateCollapsed) {
|
||||
t.Fatalf("diagnostics = %#v", result.Diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizePreservesDistinctEvidenceWhenEncodedKeysCoincide(t *testing.T) {
|
||||
invalidSourceID := string([]byte{'s', 0xff})
|
||||
replacementSourceID := "s\uFFFD"
|
||||
|
||||
Reference in New Issue
Block a user