Skip to content

Commit

Permalink
Create new unit tests:
Browse files Browse the repository at this point in the history
SetFont()
HorizontalScaling() / SetHorizontalScaling()
LineWidth() / SetLineWidth()
X() / SetX()
Y() / SetY()

Add invalid number test case for ToPoints()
  • Loading branch information
balacode committed Mar 28, 2018
1 parent dca98cb commit c0bc30a
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 11 deletions.
25 changes: 15 additions & 10 deletions public_test.go
@@ -1,6 +1,6 @@
// -----------------------------------------------------------------------------
// (c) balarabe@protonmail.com License: MIT
// :v: 2018-03-28 03:12:33 C7F387 [public_test.go]
// :v: 2018-03-28 03:16:48 896DE3 [public_test.go]
// -----------------------------------------------------------------------------

package pdf_test
Expand Down Expand Up @@ -79,28 +79,33 @@ func TestPublicAPI(t *testing.T) {
// FontName() string
// SetFontName(name string) *PDF

//TODO: SetFont(name string, points float64) *PDF
utest.SetFont, // SetFont(name string, points float64) *PDF

//TODO: utest.FontSize,
// FontSize() float64
// SetFontSize(points float64) *PDF

//TODO: HorizontalScaling() uint16
//TODO: SetHorizontalScaling(percent uint16) *PDF
utest.HorizontalScaling,
// HorizontalScaling() uint16
// SetHorizontalScaling(percent uint16) *PDF

//TODO: LineWidth() float64
//TODO: SetLineWidth(points float64) *PDF
utest.LineWidth,
// LineWidth() float64
// SetLineWidth(points float64) *PDF

//TODO: SetX(x float64) *PDF
//TODO: SetY(y float64) *PDF
utest.X,
// X() float64
// SetX(x float64) *PDF

//TODO: utest.SetXY, // SetXY(x, y float64) *PDF

utest.Y,
// Y() float64
// SetY(y float64) *PDF

utest.Units,
// Units() string
// SetUnits(unitName string) *PDF
//TODO: X() float64
//TODO: Y() float64

// ---------------------------------------------------------------------
// # Methods (ob *PDF)
Expand Down
130 changes: 130 additions & 0 deletions utest/horizontal_scaling.go
@@ -0,0 +1,130 @@
// -----------------------------------------------------------------------------
// (c) balarabe@protonmail.com License: MIT
// :v: 2018-03-28 03:14:14 829F50 [utest/horizontal_scaling.go]
// -----------------------------------------------------------------------------

package utest

import "fmt" // standard
import "testing" // standard

import "github.com/balacode/one-file-pdf"

// HorizontalScaling is the unit test for PDF.HorizontalScaling()
func HorizontalScaling(t *testing.T) {
fmt.Println("utest.HorizontalScaling")
//
// Horizontal Scaling of new PDF must be 100
func() {
var doc pdf.PDF
TEqual(t, doc.HorizontalScaling(), 100)
}()
func() {
var doc = pdf.NewPDF("A4")
TEqual(t, doc.HorizontalScaling(), 100)
}()
//
// SetHorizontalScaling() has effect on the property?
func() {
var doc pdf.PDF
doc.SetHorizontalScaling(149)
TEqual(t, doc.HorizontalScaling(), 149)
}()
func() {
var doc = pdf.NewPDF("A4")
doc.SetHorizontalScaling(149)
TEqual(t, doc.HorizontalScaling(), 149)
}()

// -------------------------------------------------------------------------
// Test PDF generation

func() {
var doc = pdf.NewPDF("A4-L")
doc.SetCompression(false).
SetUnits("cm").
SetFont("Times-Bold", 20).
SetXY(1, 1).
DrawText("Horizontal Scaling Property")

for i, hscaling := range []int{50, 100, 150, 200, 250} {
var y = 2.5 + float64(i)*2.5
doc.SetXY(1, y).
SetFont("Helvetica", 10).
SetHorizontalScaling(100).
DrawText(fmt.Sprintf("Horizontal Scaling = %d", hscaling)).
SetXY(1, y+0.7).
SetHorizontalScaling(uint16(hscaling)).
SetFontSize(20).
DrawText("Five hexing wizard bots jump quickly")
}
var expect = `
%PDF-1.4
1 0 obj<</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj<</Type/Pages/Count 1/MediaBox[0 0 841 595]/Kids[3 0 R]>>
endobj
3 0 obj<</Type/Page/Parent 2 0 R/Contents 4 0 R\
/Resources<</Font <</FNT1 5 0 R/FNT2 6 0 R>>>>>>
endobj
4 0 obj <</Length 899>>stream
BT /FNT1 20 Tf ET
0.000 0.000 0.000 rg
0.000 0.000 0.000 RG
BT 28 566 Td (Horizontal Scaling Property) Tj ET
BT /FNT2 10 Tf ET
BT 28 524 Td (Horizontal Scaling = 50) Tj ET
BT /FNT2 20 Tf ET
BT 50 Tz ET
BT 28 504 Td (Five hexing wizard bots jump quickly) Tj ET
BT /FNT2 10 Tf ET
BT 100 Tz ET
BT 28 453 Td (Horizontal Scaling = 100) Tj ET
BT /FNT2 20 Tf ET
BT 28 433 Td (Five hexing wizard bots jump quickly) Tj ET
BT /FNT2 10 Tf ET
BT 28 382 Td (Horizontal Scaling = 150) Tj ET
BT /FNT2 20 Tf ET
BT 150 Tz ET
BT 28 362 Td (Five hexing wizard bots jump quickly) Tj ET
BT /FNT2 10 Tf ET
BT 100 Tz ET
BT 28 311 Td (Horizontal Scaling = 200) Tj ET
BT /FNT2 20 Tf ET
BT 200 Tz ET
BT 28 291 Td (Five hexing wizard bots jump quickly) Tj ET
BT /FNT2 10 Tf ET
BT 100 Tz ET
BT 28 240 Td (Horizontal Scaling = 250) Tj ET
BT /FNT2 20 Tf ET
BT 250 Tz ET
BT 28 221 Td (Five hexing wizard bots jump quickly) Tj ET
endstream
5 0 obj<</Type/Font/Subtype/Type1/Name/F1/BaseFont/Times-Bold\
/Encoding/WinAnsiEncoding>>
endobj
6 0 obj<</Type/Font/Subtype/Type1/Name/F2/BaseFont/Helvetica\
/Encoding/WinAnsiEncoding>>
endobj
xref
0 7
0000000000 65535 f
0000000009 00000 n
0000000053 00000 n
0000000125 00000 n
0000000228 00000 n
0000001168 00000 n
0000001264 00000 n
trailer
<</Size 7/Root 1 0 R>>
startxref
1359
%%EOF
`
pdfCompare(t, doc.Bytes(), expect, pdfStreamsInText)
doc.SaveFile("``test_horizontal_scaling.pdf")
}()

} // HorizontalScaling

//end
61 changes: 61 additions & 0 deletions utest/line_width.go
@@ -0,0 +1,61 @@
// -----------------------------------------------------------------------------
// (c) balarabe@protonmail.com License: MIT
// :v: 2018-03-28 03:14:14 08292D [utest/line_width.go]
// -----------------------------------------------------------------------------

package utest

import "fmt" // standard
import "testing" // standard

import "github.com/balacode/one-file-pdf"

// LineWidth is the unit test for PDF.LineWidth()
func LineWidth(t *testing.T) {
fmt.Println("utest.LineWidth")
//
// LineWidth of new PDF must be 1 point
func() {
var doc pdf.PDF
TEqual(t, doc.LineWidth(), 1)
}()
func() {
var doc = pdf.NewPDF("A4")
TEqual(t, doc.LineWidth(), 1)
}()
//
// SetLineWidth() has effect on the property?
func() {
var doc pdf.PDF
doc.SetLineWidth(42)
TEqual(t, doc.LineWidth(), 42)
}()
func() {
var doc = pdf.NewPDF("A4")
doc.SetLineWidth(7)
TEqual(t, doc.LineWidth(), 7)
}()

// -------------------------------------------------------------------------
// Test PDF generation

return // TODO: implement test case

func() {
var doc = pdf.NewPDF("A4")
doc.SetCompression(false).
SetUnits("cm").
SetFont("Times-Bold", 20).
SetXY(1, 1).
DrawText("LineWidth Property")
var expect = `
%PDF-1.4
%%EOF
`
pdfCompare(t, doc.Bytes(), expect, pdfStreamsInText)
doc.SaveFile("``test_line_width.pdf")
}()

} // LineWidth

//end

0 comments on commit c0bc30a

Please sign in to comment.