Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions diff/myers/myers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1293,3 +1293,29 @@ func generateBenchmarkInput(size int, changeRate float64) (string, string) {

return strings.Join(aLines, "\n"), strings.Join(bLines, "\n")
}

func TestLinearSpacePanics(t *testing.T) {
// 1. x/y clamping panic
t.Run("xy clamping panic", func(t *testing.T) {
a := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"}
b := []string{"X", "Y", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "Z"}

_, err := myers.DiffStrings(a, b,
myers.WithLinearSpace(true),
myers.WithSmallInputThreshold(0),
)
assert.NoError(t, err)
})

// 2. prefix/suffix limit panic
t.Run("prefix suffix limit panic", func(t *testing.T) {
a := []string{"X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"}
b := []string{"X", "X", "X", "X", "X", "X", "X", "X", "X", "X"}

_, err := myers.DiffStrings(a, b,
myers.WithLinearSpace(true),
myers.WithSmallInputThreshold(0),
)
assert.NoError(t, err)
})
}
Comment on lines +1297 to +1321
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better maintainability and to follow common Go testing patterns, consider refactoring this test to be table-driven. This makes it easier to add more panic-related test cases in the future.

func TestLinearSpacePanics(t *testing.T) {
	tests := []struct {
		name string
		a    []string
		b    []string
	}{
		{
			name: "xy clamping panic",
			a:    []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"},
			b:    []string{"X", "Y", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "Z"},
		},
		{
			name: "prefix suffix limit panic",
			a:    []string{"X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
			b:    []string{"X", "X", "X", "X", "X", "X", "X", "X", "X", "X"},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			_, err := myers.DiffStrings(tt.a, tt.b,
				myers.WithLinearSpace(true),
				myers.WithSmallInputThreshold(0),
			)
			assert.NoError(t, err)
		})
	}
}