diff --git a/internal/postprocess/punctuation.go b/internal/postprocess/punctuation.go index 14cf068..097a41d 100644 --- a/internal/postprocess/punctuation.go +++ b/internal/postprocess/punctuation.go @@ -44,7 +44,7 @@ func FilterPunctuation(es *actions.EditScript, ms *engine.Mapping) *actions.Edit Node: a.Node, }) - dstNode := ms.Dst()[a.Node] + dstNode := ms.Src()[a.Node] if dstNode != nil { filtered.Add(actions.Action{ Type: actions.Insert, diff --git a/internal/postprocess/punctuation_test.go b/internal/postprocess/punctuation_test.go new file mode 100644 index 0000000..e79bc5b --- /dev/null +++ b/internal/postprocess/punctuation_test.go @@ -0,0 +1,66 @@ +package postprocess + +import ( + "testing" + + "github.com/HarshK97/diffmantic/internal/actions" + "github.com/HarshK97/diffmantic/internal/engine" + "github.com/HarshK97/diffmantic/internal/testutil" +) + +func TestFilterPunctuation_StrictPunctuationMoveSplit(t *testing.T) { + // Create T1 (source) and T2 (destination) strict punctuation nodes (e.g. double quote or colon) + srcPunct := testutil.Leaf("string_delimiter", "\"") + dstPunct := testutil.Leaf("string_delimiter", "\"") + dstParent := testutil.Leaf("string_literal", "") + + ms := engine.NewMapping() + ms.Add(srcPunct, dstPunct) + + es := actions.NewEditScript() + es.Add(actions.Action{ + Type: actions.Move, + Node: srcPunct, + Parent: dstParent, + Position: 0, + }) + + filtered := FilterPunctuation(es, ms) + if filtered == nil { + t.Fatal("expected non-nil edit script") + } + + acts := filtered.Actions() + if len(acts) != 2 { + t.Fatalf("expected 2 actions (Delete + Insert), got %d", len(acts)) + } + + if acts[0].Type != actions.Delete || acts[0].Node != srcPunct { + t.Errorf("expected first action to be Delete of srcPunct, got %+v", acts[0]) + } + + if acts[1].Type != actions.Insert || acts[1].Node != dstPunct { + t.Errorf("expected second action to be Insert of dstPunct, got %+v", acts[1]) + } +} + +func TestFilterPunctuation_SkipSemicolonAndComma(t *testing.T) { + semiNode := testutil.Leaf("semicolon", ";") + commaNode := testutil.Leaf("comma", ",") + + ms := engine.NewMapping() + es := actions.NewEditScript() + es.Add(actions.Action{Type: actions.Insert, Node: semiNode}) + es.Add(actions.Action{Type: actions.Delete, Node: commaNode}) + + filtered := FilterPunctuation(es, ms) + if filtered.Size() != 0 { + t.Errorf("expected 0 actions after filtering commas and semicolons, got %d", filtered.Size()) + } +} + +func TestFilterPunctuation_NilScript(t *testing.T) { + if FilterPunctuation(nil, engine.NewMapping()) != nil { + t.Error("expected nil for nil edit script") + } +}