|
| 1 | +package tableformat |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/jeduden/mdsmith/internal/lint" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +// TestCheckColumnCountCompliant_NoAlloc pins that checkColumnCount |
| 11 | +// returns nil, with zero allocations, when every row's cell count |
| 12 | +// matches the header. The old make([]lint.Diagnostic, 0, |
| 13 | +// len(t.rows)-1) allocated a slice on every call and then discarded |
| 14 | +// it via `if len(diags) == 0 { return nil }` on this common |
| 15 | +// compliant-table path — see docs/development/high-performance-go.md's |
| 16 | +// "Return nil, not []T{}" pattern. |
| 17 | +func TestCheckColumnCountCompliant_NoAlloc(t *testing.T) { |
| 18 | + f, err := lint.NewFile("t.md", []byte("| a | b |\n|---|---|\n| 1 | 2 |\n")) |
| 19 | + require.NoError(t, err) |
| 20 | + tables := findStructureTables(f.Lines, structureSkipFunc(f)) |
| 21 | + require.Len(t, tables, 1) |
| 22 | + tbl := tables[0] |
| 23 | + |
| 24 | + if got := checkColumnCount(f, tbl, "MDS025", "table-format"); got != nil { |
| 25 | + t.Fatalf("checkColumnCount on a compliant table = %#v, want nil", got) |
| 26 | + } |
| 27 | + |
| 28 | + if testing.Short() { |
| 29 | + t.Skip("alloc gate skipped in -short mode") |
| 30 | + } |
| 31 | + if raceEnabled { |
| 32 | + t.Skip("alloc gate skipped under -race") |
| 33 | + } |
| 34 | + allocs := testing.AllocsPerRun(200, func() { |
| 35 | + _ = checkColumnCount(f, tbl, "MDS025", "table-format") |
| 36 | + }) |
| 37 | + t.Logf("checkColumnCount(compliant) allocs/op = %.0f", allocs) |
| 38 | + if allocs > 0 { |
| 39 | + t.Fatalf("checkColumnCount(compliant) allocs/op = %.0f, want 0: allocate diags "+ |
| 40 | + "lazily via append instead of an eager make() that gets discarded", allocs) |
| 41 | + } |
| 42 | +} |
0 commit comments