This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
package wpdf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCellWithAllOptions(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
|
||||
b.Cell("test",
|
||||
NewPDFCellOpt().
|
||||
Width(40).
|
||||
Height(8).
|
||||
Border(BorderFull).
|
||||
LnPos(BreakToNextLine).
|
||||
Align(AlignHorzCenter).
|
||||
FillBackground(true).
|
||||
Font(FontTimes, Bold, 10).
|
||||
LnAfter(2).
|
||||
TextColor(255, 0, 0).
|
||||
BorderColor(0, 255, 0).
|
||||
FillColor(0, 0, 255).
|
||||
Alpha(0.5, BlendMultiply))
|
||||
|
||||
out, err := b.Build()
|
||||
if err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
if !bytes.HasPrefix(out, []byte("%PDF-")) {
|
||||
t.Error("output not a PDF")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellRestoresFont(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.SetFont(FontHelvetica, Normal, 12)
|
||||
|
||||
b.Cell("text", NewPDFCellOpt().Width(50).Font(FontTimes, Bold, 20))
|
||||
|
||||
if b.fontName != FontHelvetica {
|
||||
t.Errorf("fontName not restored: %v", b.fontName)
|
||||
}
|
||||
if b.fontStyle != Normal {
|
||||
t.Errorf("fontStyle not restored: %v", b.fontStyle)
|
||||
}
|
||||
if b.fontSize != 12 {
|
||||
t.Errorf("fontSize not restored: %v", b.fontSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellRestoresColors(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.SetTextColor(10, 20, 30)
|
||||
b.SetDrawColor(40, 50, 60)
|
||||
b.SetFillColor(70, 80, 90)
|
||||
|
||||
b.Cell("text",
|
||||
NewPDFCellOpt().Width(50).
|
||||
TextColor(1, 2, 3).
|
||||
BorderColor(4, 5, 6).
|
||||
FillColor(7, 8, 9))
|
||||
|
||||
r, g, bl := b.GetTextColor()
|
||||
if r != 10 || g != 20 || bl != 30 {
|
||||
t.Errorf("TextColor not restored: (%d,%d,%d)", r, g, bl)
|
||||
}
|
||||
|
||||
r, g, bl = b.GetDrawColor()
|
||||
if r != 40 || g != 50 || bl != 60 {
|
||||
t.Errorf("DrawColor not restored: (%d,%d,%d)", r, g, bl)
|
||||
}
|
||||
|
||||
r, g, bl = b.GetFillColor()
|
||||
if r != 70 || g != 80 || bl != 90 {
|
||||
t.Errorf("FillColor not restored: (%d,%d,%d)", r, g, bl)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellAutoWidth(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
|
||||
startX := b.GetX()
|
||||
b.Cell("Hello", NewPDFCellOpt().AutoWidth().AutoWidthPaddingX(2).LnPos(BreakToRight))
|
||||
|
||||
endX := b.GetX()
|
||||
if endX <= startX {
|
||||
t.Errorf("AutoWidth: X did not advance (start=%v end=%v)", startX, endX)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellWithExplicitX(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
|
||||
b.Cell("text", NewPDFCellOpt().Width(50).X(70).LnPos(BreakToRight))
|
||||
// X should be 70 + 50 = 120 after cell with BreakToRight
|
||||
if got := b.GetX(); got < 120-0.01 || got > 120+0.01 {
|
||||
t.Errorf("X after cell at X=70 with width=50 = %v, want ~120", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellWithDebugTrueBreakToRight(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Debug(true)
|
||||
b.Cell("text", NewPDFCellOpt().Width(20).LnPos(BreakToRight))
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellWithDebugTrueBreakToBelow(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Debug(true)
|
||||
b.Cell("text", NewPDFCellOpt().Width(20).LnPos(BreakToBelow))
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiCellAllOptions(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.MultiCell("hello\nworld",
|
||||
NewPDFMultiCellOpt().
|
||||
Width(50).
|
||||
Height(6).
|
||||
Border(BorderFull).
|
||||
Align(AlignLeft).
|
||||
FillBackground(true).
|
||||
Font(FontTimes, Italic, 10).
|
||||
LnAfter(1).
|
||||
X(20).
|
||||
TextColor(1, 2, 3).
|
||||
BorderColor(4, 5, 6).
|
||||
FillColor(7, 8, 9).
|
||||
Alpha(0.5, BlendNormal))
|
||||
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiCellRestoresFont(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.SetFont(FontHelvetica, Normal, 12)
|
||||
|
||||
b.MultiCell("text", NewPDFMultiCellOpt().Width(50).Font(FontTimes, Bold, 20))
|
||||
|
||||
if b.fontName != FontHelvetica {
|
||||
t.Errorf("fontName not restored: %v", b.fontName)
|
||||
}
|
||||
if b.fontStyle != Normal {
|
||||
t.Errorf("fontStyle not restored: %v", b.fontStyle)
|
||||
}
|
||||
if b.fontSize != 12 {
|
||||
t.Errorf("fontSize not restored: %v", b.fontSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiCellWithDebug(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Debug(true)
|
||||
b.MultiCell("hello world", NewPDFMultiCellOpt().Width(50))
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRectAllOptions(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Rect(20, 10, RectFillOutline,
|
||||
NewPDFRectOpt().
|
||||
X(15).Y(20).
|
||||
LineWidth(0.5).
|
||||
DrawColor(10, 20, 30).
|
||||
FillColor(100, 110, 120).
|
||||
Alpha(0.5, BlendNormal).
|
||||
Rounded(2))
|
||||
|
||||
// Drawing color and line width should be restored after Rect.
|
||||
r, g, bl := b.GetDrawColor()
|
||||
if r != 0 || g != 0 || bl != 0 {
|
||||
t.Errorf("draw color not restored: (%d,%d,%d)", r, g, bl)
|
||||
}
|
||||
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRectStyles(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Rect(10, 10, RectFill, NewPDFRectOpt().X(10).Y(10).FillColor(255, 0, 0))
|
||||
b.Rect(10, 10, RectOutline, NewPDFRectOpt().X(30).Y(10).DrawColor(0, 255, 0))
|
||||
b.Rect(10, 10, RectFillOutline, NewPDFRectOpt().X(50).Y(10).FillColor(0, 0, 255).DrawColor(255, 255, 0))
|
||||
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRectIndividualRadii(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Rect(20, 20, RectOutline,
|
||||
NewPDFRectOpt().
|
||||
X(10).Y(10).
|
||||
RadiusTL(2).RadiusTR(3).RadiusBR(4).RadiusBL(5))
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLineAllOptions(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Line(10, 10, 100, 100,
|
||||
NewPDFLineOpt().
|
||||
LineWidth(0.5).
|
||||
DrawColor(255, 0, 0).
|
||||
Alpha(0.7, BlendDarken).
|
||||
CapRound())
|
||||
|
||||
// Line width should be restored after Line.
|
||||
if got := b.GetLineWidth(); got == 0.5 {
|
||||
t.Errorf("LineWidth was not restored: %v", got)
|
||||
}
|
||||
|
||||
r, g, bl := b.GetDrawColor()
|
||||
if r == 255 && g == 0 && bl == 0 {
|
||||
t.Error("DrawColor not restored")
|
||||
}
|
||||
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLineCapStyles(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
b.Line(10, 10, 100, 10, NewPDFLineOpt().LineWidth(2).CapButt())
|
||||
b.Line(10, 20, 100, 20, NewPDFLineOpt().LineWidth(2).CapRound())
|
||||
b.Line(10, 30, 100, 30, NewPDFLineOpt().LineWidth(2).CapSquare())
|
||||
|
||||
if _, err := b.Build(); err != nil {
|
||||
t.Fatalf("Build error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCellWithExtraLn(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
yBefore := b.GetY()
|
||||
b.Cell("text", NewPDFCellOpt().Width(50).LnAfter(5))
|
||||
yAfter := b.GetY()
|
||||
if yAfter <= yBefore {
|
||||
t.Errorf("Y did not advance after Cell with LnAfter")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiCellWithExtraLn(t *testing.T) {
|
||||
b := newBuilderWithPage(t)
|
||||
yBefore := b.GetY()
|
||||
b.MultiCell("multiline\ntext", NewPDFMultiCellOpt().Width(50).LnAfter(5))
|
||||
yAfter := b.GetY()
|
||||
if yAfter <= yBefore {
|
||||
t.Errorf("Y did not advance after MultiCell with LnAfter")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user