2025-12-20 14:37:56 +01:00
|
|
|
package pdfrender
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
|
|
func TestGenerateContinuationTemplateName(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
template string
|
|
|
|
|
pageNum int
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "First page returns original",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
pageNum: 1,
|
2025-12-21 22:07:46 +01:00
|
|
|
expected: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Second page gets continuation name",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
pageNum: 2,
|
2025-12-21 22:07:46 +01:00
|
|
|
expected: "page_1.2.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
2025-12-20 15:03:55 +01:00
|
|
|
name: "Third page uses same continuation template (.2)",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_2.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
pageNum: 3,
|
2025-12-21 22:07:46 +01:00
|
|
|
expected: "page_2.2.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
2025-12-20 15:03:55 +01:00
|
|
|
name: "All continuation pages use .2 template",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
pageNum: 10,
|
2025-12-21 22:07:46 +01:00
|
|
|
expected: "page_1.2.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
result := GenerateContinuationTemplateName(tt.template, tt.pageNum)
|
|
|
|
|
if result != tt.expected {
|
|
|
|
|
t.Errorf("Expected '%s', got '%s'", tt.expected, result)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestExtractBaseTemplateName(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
template string
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "Base template returns itself",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_1.html",
|
|
|
|
|
expected: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Continuation template returns base",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_1.2.html",
|
|
|
|
|
expected: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Multiple digit continuation",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_1.10.html",
|
|
|
|
|
expected: "page_1.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Different page type",
|
2025-12-21 22:07:46 +01:00
|
|
|
template: "page_2.3.html",
|
|
|
|
|
expected: "page_2.html",
|
2025-12-20 14:37:56 +01:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "Template without underscore",
|
|
|
|
|
template: "simple.html",
|
|
|
|
|
expected: "simple.html",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
result := ExtractBaseTemplateName(tt.template)
|
|
|
|
|
if result != tt.expected {
|
|
|
|
|
t.Errorf("Expected '%s', got '%s'", tt.expected, result)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestContinuationRoundTrip(t *testing.T) {
|
|
|
|
|
// Test that generating a continuation name and extracting the base works correctly
|
2025-12-21 22:07:46 +01:00
|
|
|
original := "page_1.html"
|
2025-12-20 14:37:56 +01:00
|
|
|
|
|
|
|
|
for pageNum := 1; pageNum <= 5; pageNum++ {
|
|
|
|
|
continuation := GenerateContinuationTemplateName(original, pageNum)
|
|
|
|
|
extracted := ExtractBaseTemplateName(continuation)
|
|
|
|
|
|
|
|
|
|
if extracted != original {
|
|
|
|
|
t.Errorf("Round trip failed for page %d: original '%s', continuation '%s', extracted '%s'",
|
|
|
|
|
pageNum, original, continuation, extracted)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|