456 lines
11 KiB
Go
456 lines
11 KiB
Go
package wpdf
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestPDFCellOptBuilders(t *testing.T) {
|
|
opt := NewPDFCellOpt().
|
|
Width(100).
|
|
Height(20).
|
|
Border(BorderFull).
|
|
LnPos(BreakToRight).
|
|
Align(AlignHorzCenter).
|
|
FillBackground(true).
|
|
Link(5).
|
|
LinkStr("https://example.com").
|
|
Font(FontTimes, Bold, 14).
|
|
LnAfter(2).
|
|
X(15).
|
|
AutoWidth().
|
|
AutoWidthPaddingX(3).
|
|
TextColor(1, 2, 3).
|
|
BorderColor(4, 5, 6).
|
|
FillColor(7, 8, 9).
|
|
Alpha(0.5, BlendMultiply).
|
|
Debug(true)
|
|
|
|
if opt.width == nil || *opt.width != 100 {
|
|
t.Error("width not set")
|
|
}
|
|
if opt.height == nil || *opt.height != 20 {
|
|
t.Error("height not set")
|
|
}
|
|
if opt.border == nil || *opt.border != BorderFull {
|
|
t.Error("border not set")
|
|
}
|
|
if opt.ln == nil || *opt.ln != BreakToRight {
|
|
t.Error("ln not set")
|
|
}
|
|
if opt.align == nil || *opt.align != AlignHorzCenter {
|
|
t.Error("align not set")
|
|
}
|
|
if opt.fill == nil || *opt.fill != true {
|
|
t.Error("fill not set")
|
|
}
|
|
if opt.link == nil || *opt.link != 5 {
|
|
t.Error("link not set")
|
|
}
|
|
if opt.linkStr == nil || *opt.linkStr != "https://example.com" {
|
|
t.Error("linkStr not set")
|
|
}
|
|
if opt.fontNameOverride == nil || *opt.fontNameOverride != FontTimes {
|
|
t.Error("fontNameOverride not set")
|
|
}
|
|
if opt.fontStyleOverride == nil || *opt.fontStyleOverride != Bold {
|
|
t.Error("fontStyleOverride not set")
|
|
}
|
|
if opt.fontSizeOverride == nil || *opt.fontSizeOverride != 14 {
|
|
t.Error("fontSizeOverride not set")
|
|
}
|
|
if opt.extraLn == nil || *opt.extraLn != 2 {
|
|
t.Error("extraLn not set")
|
|
}
|
|
if opt.x == nil || *opt.x != 15 {
|
|
t.Error("x not set")
|
|
}
|
|
if opt.autoWidth == nil || *opt.autoWidth != true {
|
|
t.Error("autoWidth not set")
|
|
}
|
|
if opt.autoWidthPaddingX == nil || *opt.autoWidthPaddingX != 3 {
|
|
t.Error("autoWidthPaddingX not set")
|
|
}
|
|
if opt.textColor == nil || *opt.textColor != (PDFColor{R: 1, G: 2, B: 3}) {
|
|
t.Error("textColor not set")
|
|
}
|
|
if opt.borderColor == nil || *opt.borderColor != (PDFColor{R: 4, G: 5, B: 6}) {
|
|
t.Error("borderColor not set")
|
|
}
|
|
if opt.fillColor == nil || *opt.fillColor != (PDFColor{R: 7, G: 8, B: 9}) {
|
|
t.Error("fillColor not set")
|
|
}
|
|
if opt.alphaOverride == nil || opt.alphaOverride.V1 != 0.5 || opt.alphaOverride.V2 != BlendMultiply {
|
|
t.Error("alphaOverride not set")
|
|
}
|
|
if opt.debug == nil || *opt.debug != true {
|
|
t.Error("debug not set")
|
|
}
|
|
}
|
|
|
|
func TestPDFCellOptHexColors(t *testing.T) {
|
|
opt := NewPDFCellOpt().
|
|
TextColorHex(0xFF0000).
|
|
BorderColorHex(0x00FF00).
|
|
FillColorHex(0x0000FF)
|
|
|
|
if *opt.textColor != (PDFColor{R: 0xFF, G: 0, B: 0}) {
|
|
t.Errorf("textColorHex got %+v", opt.textColor)
|
|
}
|
|
if *opt.borderColor != (PDFColor{R: 0, G: 0xFF, B: 0}) {
|
|
t.Errorf("borderColorHex got %+v", opt.borderColor)
|
|
}
|
|
if *opt.fillColor != (PDFColor{R: 0, G: 0, B: 0xFF}) {
|
|
t.Errorf("fillColorHex got %+v", opt.fillColor)
|
|
}
|
|
}
|
|
|
|
func TestPDFCellOptBoldItalic(t *testing.T) {
|
|
opt := NewPDFCellOpt().Bold()
|
|
if opt.fontStyleOverride == nil || *opt.fontStyleOverride != Bold {
|
|
t.Error("Bold() should set fontStyleOverride to Bold")
|
|
}
|
|
|
|
opt2 := NewPDFCellOpt().Italic()
|
|
if opt2.fontStyleOverride == nil || *opt2.fontStyleOverride != Italic {
|
|
t.Error("Italic() should set fontStyleOverride to Italic")
|
|
}
|
|
}
|
|
|
|
func TestPDFCellOptIndividualFontSetters(t *testing.T) {
|
|
opt := NewPDFCellOpt().FontName(FontCourier).FontStyle(Italic).FontSize(11)
|
|
if *opt.fontNameOverride != FontCourier {
|
|
t.Error("FontName")
|
|
}
|
|
if *opt.fontStyleOverride != Italic {
|
|
t.Error("FontStyle")
|
|
}
|
|
if *opt.fontSizeOverride != 11 {
|
|
t.Error("FontSize")
|
|
}
|
|
}
|
|
|
|
func TestPDFCellOptCopy(t *testing.T) {
|
|
orig := NewPDFCellOpt().Width(50).Height(10)
|
|
cp := orig.Copy()
|
|
|
|
if cp == orig {
|
|
t.Error("Copy() returned same pointer")
|
|
}
|
|
if *cp.width != 50 || *cp.height != 10 {
|
|
t.Error("copied opt does not match original")
|
|
}
|
|
|
|
cp.Width(99)
|
|
if *orig.width != 50 {
|
|
t.Errorf("modifying copy affected original: orig.width=%v", *orig.width)
|
|
}
|
|
}
|
|
|
|
func TestPDFCellOptToMulti(t *testing.T) {
|
|
cell := NewPDFCellOpt().
|
|
Width(100).
|
|
Height(20).
|
|
Border(BorderTop).
|
|
Align(AlignRight).
|
|
FillBackground(true).
|
|
Font(FontTimes, Bold, 14).
|
|
LnAfter(3).
|
|
X(5).
|
|
TextColor(1, 2, 3).
|
|
BorderColor(4, 5, 6).
|
|
FillColor(7, 8, 9)
|
|
|
|
multi := cell.ToMulti()
|
|
|
|
if multi == nil {
|
|
t.Fatal("ToMulti returned nil")
|
|
}
|
|
if *multi.width != 100 {
|
|
t.Error("width not transferred")
|
|
}
|
|
if *multi.height != 20 {
|
|
t.Error("height not transferred")
|
|
}
|
|
if *multi.border != BorderTop {
|
|
t.Error("border not transferred")
|
|
}
|
|
if *multi.align != AlignRight {
|
|
t.Error("align not transferred")
|
|
}
|
|
if !*multi.fill {
|
|
t.Error("fill not transferred")
|
|
}
|
|
if *multi.fontNameOverride != FontTimes {
|
|
t.Error("fontName not transferred")
|
|
}
|
|
if *multi.fontStyleOverride != Bold {
|
|
t.Error("fontStyle not transferred")
|
|
}
|
|
if *multi.fontSizeOverride != 14 {
|
|
t.Error("fontSize not transferred")
|
|
}
|
|
if *multi.extraLn != 3 {
|
|
t.Error("extraLn not transferred")
|
|
}
|
|
if *multi.x != 5 {
|
|
t.Error("x not transferred")
|
|
}
|
|
if *multi.textColor != (PDFColor{R: 1, G: 2, B: 3}) {
|
|
t.Error("textColor not transferred")
|
|
}
|
|
if *multi.borderColor != (PDFColor{R: 4, G: 5, B: 6}) {
|
|
t.Error("borderColor not transferred")
|
|
}
|
|
if *multi.fillColor != (PDFColor{R: 7, G: 8, B: 9}) {
|
|
t.Error("fillColor not transferred")
|
|
}
|
|
}
|
|
|
|
func TestPDFMultiCellOptBuilders(t *testing.T) {
|
|
opt := NewPDFMultiCellOpt().
|
|
Width(80).
|
|
Height(10).
|
|
Border(BorderFull).
|
|
Align(AlignLeft).
|
|
FillBackground(true).
|
|
Font(FontTimes, Italic, 11).
|
|
LnAfter(1).
|
|
X(5).
|
|
TextColor(10, 20, 30).
|
|
BorderColor(40, 50, 60).
|
|
FillColor(70, 80, 90).
|
|
Alpha(0.7, BlendScreen).
|
|
Debug(true)
|
|
|
|
if *opt.width != 80 || *opt.height != 10 || *opt.border != BorderFull {
|
|
t.Error("base opts not set")
|
|
}
|
|
if *opt.align != AlignLeft || !*opt.fill {
|
|
t.Error("align/fill not set")
|
|
}
|
|
if *opt.fontNameOverride != FontTimes || *opt.fontStyleOverride != Italic || *opt.fontSizeOverride != 11 {
|
|
t.Error("font not set")
|
|
}
|
|
if *opt.extraLn != 1 || *opt.x != 5 {
|
|
t.Error("layout opts not set")
|
|
}
|
|
if *opt.textColor != (PDFColor{R: 10, G: 20, B: 30}) {
|
|
t.Error("textColor not set")
|
|
}
|
|
if *opt.borderColor != (PDFColor{R: 40, G: 50, B: 60}) {
|
|
t.Error("borderColor not set")
|
|
}
|
|
if *opt.fillColor != (PDFColor{R: 70, G: 80, B: 90}) {
|
|
t.Error("fillColor not set")
|
|
}
|
|
if opt.alphaOverride.V1 != 0.7 || opt.alphaOverride.V2 != BlendScreen {
|
|
t.Error("alpha not set")
|
|
}
|
|
if !*opt.debug {
|
|
t.Error("debug not set")
|
|
}
|
|
}
|
|
|
|
func TestPDFMultiCellOptHexColors(t *testing.T) {
|
|
opt := NewPDFMultiCellOpt().
|
|
TextColorHex(0xAABBCC).
|
|
BorderColorHex(0x112233).
|
|
FillColorHex(0x445566)
|
|
|
|
if *opt.textColor != (PDFColor{R: 0xAA, G: 0xBB, B: 0xCC}) {
|
|
t.Error("textColorHex")
|
|
}
|
|
if *opt.borderColor != (PDFColor{R: 0x11, G: 0x22, B: 0x33}) {
|
|
t.Error("borderColorHex")
|
|
}
|
|
if *opt.fillColor != (PDFColor{R: 0x44, G: 0x55, B: 0x66}) {
|
|
t.Error("fillColorHex")
|
|
}
|
|
}
|
|
|
|
func TestPDFMultiCellOptBoldItalic(t *testing.T) {
|
|
if *NewPDFMultiCellOpt().Bold().fontStyleOverride != Bold {
|
|
t.Error("MultiCell Bold")
|
|
}
|
|
if *NewPDFMultiCellOpt().Italic().fontStyleOverride != Italic {
|
|
t.Error("MultiCell Italic")
|
|
}
|
|
}
|
|
|
|
func TestPDFMultiCellOptIndividualFontSetters(t *testing.T) {
|
|
opt := NewPDFMultiCellOpt().FontName(FontCourier).FontStyle(Bold).FontSize(11)
|
|
if *opt.fontNameOverride != FontCourier {
|
|
t.Error("FontName")
|
|
}
|
|
if *opt.fontStyleOverride != Bold {
|
|
t.Error("FontStyle")
|
|
}
|
|
if *opt.fontSizeOverride != 11 {
|
|
t.Error("FontSize")
|
|
}
|
|
}
|
|
|
|
func TestPDFMultiCellOptCopy(t *testing.T) {
|
|
orig := NewPDFMultiCellOpt().Width(50)
|
|
cp := orig.Copy()
|
|
if cp == orig {
|
|
t.Error("Copy() returned same pointer")
|
|
}
|
|
cp.Width(99)
|
|
if *orig.width != 50 {
|
|
t.Error("modifying copy affected original")
|
|
}
|
|
}
|
|
|
|
func TestPDFRectOptBuilders(t *testing.T) {
|
|
opt := NewPDFRectOpt().
|
|
X(5).Y(6).
|
|
LineWidth(0.3).
|
|
DrawColor(10, 20, 30).
|
|
FillColor(40, 50, 60).
|
|
Alpha(0.4, BlendOverlay).
|
|
Rounded(2).
|
|
Debug(true)
|
|
|
|
if *opt.x != 5 || *opt.y != 6 {
|
|
t.Error("x/y not set")
|
|
}
|
|
if *opt.lineWidth != 0.3 {
|
|
t.Error("lineWidth not set")
|
|
}
|
|
if *opt.drawColor != (PDFColor{R: 10, G: 20, B: 30}) {
|
|
t.Error("drawColor not set")
|
|
}
|
|
if *opt.fillColor != (PDFColor{R: 40, G: 50, B: 60}) {
|
|
t.Error("fillColor not set")
|
|
}
|
|
if opt.alpha.V1 != 0.4 || opt.alpha.V2 != BlendOverlay {
|
|
t.Error("alpha not set")
|
|
}
|
|
if *opt.radiusTL != 2 || *opt.radiusTR != 2 || *opt.radiusBL != 2 || *opt.radiusBR != 2 {
|
|
t.Error("Rounded did not set all four corners")
|
|
}
|
|
if !*opt.debug {
|
|
t.Error("debug not set")
|
|
}
|
|
}
|
|
|
|
func TestPDFRectOptIndividualRadii(t *testing.T) {
|
|
opt := NewPDFRectOpt().RadiusTL(1).RadiusTR(2).RadiusBR(3).RadiusBL(4)
|
|
|
|
if *opt.radiusTL != 1 || *opt.radiusTR != 2 || *opt.radiusBR != 3 || *opt.radiusBL != 4 {
|
|
t.Errorf("individual radii not set: TL=%v TR=%v BR=%v BL=%v",
|
|
*opt.radiusTL, *opt.radiusTR, *opt.radiusBR, *opt.radiusBL)
|
|
}
|
|
}
|
|
|
|
func TestPDFRectOptHexColors(t *testing.T) {
|
|
opt := NewPDFRectOpt().DrawColorHex(0xABCDEF).FillColorHex(0x123456)
|
|
if *opt.drawColor != (PDFColor{R: 0xAB, G: 0xCD, B: 0xEF}) {
|
|
t.Error("drawColorHex")
|
|
}
|
|
if *opt.fillColor != (PDFColor{R: 0x12, G: 0x34, B: 0x56}) {
|
|
t.Error("fillColorHex")
|
|
}
|
|
}
|
|
|
|
func TestPDFLineOptBuilders(t *testing.T) {
|
|
opt := NewPDFLineOpt().
|
|
LineWidth(0.5).
|
|
DrawColor(10, 20, 30).
|
|
Alpha(0.3, BlendDarken).
|
|
CapButt().
|
|
Debug(true)
|
|
|
|
if *opt.lineWidth != 0.5 {
|
|
t.Error("lineWidth not set")
|
|
}
|
|
if *opt.drawColor != (PDFColor{R: 10, G: 20, B: 30}) {
|
|
t.Error("drawColor not set")
|
|
}
|
|
if opt.alpha.V1 != 0.3 || opt.alpha.V2 != BlendDarken {
|
|
t.Error("alpha not set")
|
|
}
|
|
if *opt.capStyle != CapButt {
|
|
t.Error("capStyle CapButt not set")
|
|
}
|
|
if !*opt.debug {
|
|
t.Error("debug not set")
|
|
}
|
|
}
|
|
|
|
func TestPDFLineOptCapStyles(t *testing.T) {
|
|
if *NewPDFLineOpt().CapButt().capStyle != CapButt {
|
|
t.Error("CapButt")
|
|
}
|
|
if *NewPDFLineOpt().CapRound().capStyle != CapRound {
|
|
t.Error("CapRound")
|
|
}
|
|
if *NewPDFLineOpt().CapSquare().capStyle != CapSquare {
|
|
t.Error("CapSquare")
|
|
}
|
|
}
|
|
|
|
func TestPDFLineOptHexColor(t *testing.T) {
|
|
opt := NewPDFLineOpt().DrawColorHex(0xFEDCBA)
|
|
if *opt.drawColor != (PDFColor{R: 0xFE, G: 0xDC, B: 0xBA}) {
|
|
t.Error("drawColorHex")
|
|
}
|
|
}
|
|
|
|
func TestPDFImageRegisterOptBuilders(t *testing.T) {
|
|
opt := NewPDFImageRegisterOpt().
|
|
ImageType("PNG").
|
|
ReadDpi(true).
|
|
AllowNegativePosition(true).
|
|
Name("custom_name")
|
|
|
|
if *opt.imageType != "PNG" {
|
|
t.Error("imageType not set")
|
|
}
|
|
if !*opt.readDpi {
|
|
t.Error("readDpi not set")
|
|
}
|
|
if !*opt.allowNegativePosition {
|
|
t.Error("allowNegativePosition not set")
|
|
}
|
|
if *opt.name != "custom_name" {
|
|
t.Error("name not set")
|
|
}
|
|
}
|
|
|
|
func TestPDFImageOptBuilders(t *testing.T) {
|
|
opt := NewPDFImageOpt().
|
|
X(1).Y(2).Width(30).Height(40).
|
|
Flow(false).
|
|
Link(7).LinkStr("foo").
|
|
ImageType("PNG").
|
|
ReadDpi(true).
|
|
AllowNegativePosition(true).
|
|
Crop(0.1, 0.2, 0.3, 0.4).
|
|
Alpha(0.6, BlendMultiply).
|
|
Debug(true)
|
|
|
|
if *opt.x != 1 || *opt.y != 2 || *opt.width != 30 || *opt.height != 40 {
|
|
t.Error("position/size not set")
|
|
}
|
|
if *opt.flow != false {
|
|
t.Error("flow not set")
|
|
}
|
|
if *opt.link != 7 || *opt.linkStr != "foo" {
|
|
t.Error("link not set")
|
|
}
|
|
if *opt.imageType != "PNG" || !*opt.readDpi || !*opt.allowNegativePosition {
|
|
t.Error("image options not set")
|
|
}
|
|
if opt.crop == nil || opt.crop.CropX != 0.1 || opt.crop.CropY != 0.2 || opt.crop.CropWidth != 0.3 || opt.crop.CropHeight != 0.4 {
|
|
t.Errorf("crop not set: %+v", opt.crop)
|
|
}
|
|
if opt.alphaOverride.V1 != 0.6 || opt.alphaOverride.V2 != BlendMultiply {
|
|
t.Error("alpha not set")
|
|
}
|
|
if !*opt.debug {
|
|
t.Error("debug not set")
|
|
}
|
|
}
|