rows, got %d", trCount)
}
tdCount := strings.Count(html, "| cells, got %d", tdCount)
}
t.Logf("HTML:\n%s", html)
}
func TestConstructTable_EmptyCells(t *testing.T) {
html := ConstructTable(nil, nil, "", nil)
if html != "" {
t.Errorf("expected empty string for empty cells, got %q", html)
}
html = ConstructTable([]pdf.TSRCell{}, []pdf.TextBox{}, "", nil)
if html != "" {
t.Errorf("expected empty string for empty cells slice, got %q", html)
}
}
func TestConstructTable_NoMatchingBox(t *testing.T) {
// Cell has no overlapping text box → empty |
cells := []pdf.TSRCell{
{X0: 0, Y0: 0, X1: 100, Y1: 50, Text: "Has text", Label: "table row"},
{X0: 101, Y0: 0, X1: 200, Y1: 50, Label: "table row"},
}
boxes := []pdf.TextBox{}
html := ConstructTable(cells, boxes, "", nil)
if !strings.Contains(html, "Has text") {
t.Error("expected first cell text")
}
// Should still have 2 | cells
if strings.Count(html, " | cells, got %d. HTML:\n%s", strings.Count(html, " | 表1:测试标题") {
t.Errorf("expected caption, got:\n%s", html)
}
t.Logf("HTML:\n%s", html)
}
func TestConstructTable_SingleRow(t *testing.T) {
cells := []pdf.TSRCell{
{X0: 0, Y0: 0, X1: 50, Y1: 40, Text: "Col1", Label: "table row"},
{X0: 51, Y0: 0, X1: 100, Y1: 40, Text: "Col2", Label: "table row"},
}
html := ConstructTable(cells, nil, "", nil)
if strings.Count(html, " |
"))
}
if strings.Count(html, "| ") {
t.Error("output should contain HTML table")
}
// Key assertion: constructTable backfills tables[0].Rows.
rows := tables[0].Rows
if len(rows) != 2 {
t.Fatalf("expected 2 rows, got %d", len(rows))
}
if rows[0][0] != "标职务" {
t.Errorf("row 0 col 0 = %q, want %q", rows[0][0], "标职务")
}
if rows[0][1] != "飞机" {
t.Errorf("row 0 col 1 = %q, want %q", rows[0][1], "飞机")
}
if rows[1][0] != "公司级领导" {
t.Errorf("row 1 col 0 = %q, want %q", rows[1][0], "公司级领导")
}
if rows[1][1] != "经济舱位" {
t.Errorf("row 1 col 1 = %q, want %q", rows[1][1], "经济舱位")
}
}
func TestConstructTable_FromBoxesRC(t *testing.T) {
// Boxes with R (row) and C (col) annotations — like the output of
// annotateTableBoxes after layout cleanup.
boxes := []pdf.TextBox{
{X0: 50, X1: 150, Top: 100, Bottom: 130, Text: "姓名", R: 0, C: 0},
{X0: 155, X1: 255, Top: 100, Bottom: 130, Text: "年龄", R: 0, C: 1},
{X0: 50, X1: 150, Top: 135, Bottom: 165, Text: "张三", R: 1, C: 0},
{X0: 155, X1: 255, Top: 135, Bottom: 165, Text: "25", R: 1, C: 1},
}
// constructTable should build HTML directly from boxes by R/C grouping,
// ignoring cell text (matching Python's construct_table).
item := &pdf.TableItem{}
html := ConstructTable(nil, boxes, "", item)
if !strings.Contains(html, "姓名") || !strings.Contains(html, "张三") {
t.Errorf("HTML missing box text: %s", html)
}
// 2 rows, 2 cols
if strings.Count(html, " |