Skip to content

Commit

Permalink
Improve unit test for PDF.getPaperSize()
Browse files Browse the repository at this point in the history
* Add all paper sizes (in mm) to test
* Permute input strings to test case-insensitivity and trimming, etc.
* Make use of 'util' package for PermuteStrings()
  • Loading branch information
balacode committed May 5, 2018
1 parent af45096 commit d0f07ba
Showing 1 changed file with 79 additions and 13 deletions.
92 changes: 79 additions & 13 deletions private_test.go
@@ -1,6 +1,6 @@
// -----------------------------------------------------------------------------
// (c) balarabe@protonmail.com License: MIT
// :v: 2018-04-29 23:45:39 D9CFB1 [private_test.go]
// :v: 2018-05-05 12:28:42 6F6E4F [private_test.go]
// -----------------------------------------------------------------------------

package pdf
Expand All @@ -19,32 +19,98 @@ import (
"runtime"
"strings"
"testing"

"github.com/balacode/one-file-pdf/utest/util"
)

// go test --run Test_getPapreSize_
func Test_getPapreSize_(t *testing.T) {
//
var test = func(input, name string, widthPt, heightPt float64, err error) {
// subtest: tests a specific paper size against width and height in points
var subtest = func(paperSize, permuted string, w, h float64, err error) {
var doc PDF
var got, gotErr = doc.getPaperSize(name)
//
doc.SetUnits("mm")
var got, gotErr = doc.getPaperSize(permuted)
if gotErr != err {
t.Errorf("'error' mismatch: expected: %v returned %v", err, gotErr)
t.Fail()
}
if got.name != name {
mismatch(t, input+" 'name'", name, got.name)
if got.name != paperSize {
mismatch(t, permuted+" 'name'", paperSize, got.name)
}
if floatStr(got.widthPt) != floatStr(w) {
mismatch(t, permuted+" 'widthPt'", w, got.widthPt)
}
if floatStr(got.widthPt) != floatStr(widthPt) {
mismatch(t, input+" 'widthPt'", widthPt, got.widthPt)
if floatStr(got.heightPt) != floatStr(h) {
mismatch(t, permuted+" 'heightPt'", h, got.heightPt)
}
if floatStr(got.heightPt) != floatStr(heightPt) {
mismatch(t, input+" 'heightPt'", heightPt, got.heightPt)
}
// test: tests the given paper size in portrait and landscape orientations
// w and h are the paper width and height in mm
// - permutes the paper size by including spaces
// - converts units from mm to points
var test = func(paperSize string, w, h float64, err error) {
const PTperMM = 2.83464566929134
var spaces = []string{"", " ", " ", " ", "\r", "\n", "\t"}
for _, orient := range []string{"", "-l", "-L"} {
var permuted = util.PermuteStrings(
spaces,
[]string{
strings.ToLower(paperSize),
strings.ToUpper(paperSize),
},
spaces,
[]string{orient},
spaces,
)
for _, s := range permuted {
var size = paperSize + strings.ToUpper(orient)
if orient == "-l" || orient == "-L" {
subtest(size, s, h*PTperMM, w*PTperMM, err)
} else {
subtest(size, s, w*PTperMM, h*PTperMM, err)
}
}
}
}
test("A4", "A4", 595.276, 841.89, nil)
test("A4-L", "A4-L", 841.89, 595.276, nil)
// TODO: add more test cases
test("A4", 210, 297, nil)
test("A0", 841, 1189, nil)
test("A1", 594, 841, nil)
test("A2", 420, 594, nil)
test("A3", 297, 420, nil)
test("A4", 210, 297, nil)
test("A5", 148, 210, nil)
test("A6", 105, 148, nil)
test("A7", 74, 105, nil)
test("A8", 52, 74, nil)
test("A9", 37, 52, nil)
test("A10", 26, 37, nil)
test("B0", 1000, 1414, nil)
test("B1", 707, 1000, nil)
test("B2", 500, 707, nil)
test("B3", 353, 500, nil)
test("B4", 250, 353, nil)
test("B5", 176, 250, nil)
test("B6", 125, 176, nil)
test("B7", 88, 125, nil)
test("B8", 62, 88, nil)
test("B9", 44, 62, nil)
test("B10", 31, 44, nil)
test("C0", 917, 1297, nil)
test("C1", 648, 917, nil)
test("C2", 458, 648, nil)
test("C3", 324, 458, nil)
test("C4", 229, 324, nil)
test("C5", 162, 229, nil)
test("C6", 114, 162, nil)
test("C7", 81, 114, nil)
test("C8", 57, 81, nil)
test("C9", 40, 57, nil)
test("C10", 28, 40, nil)
test("LEDGER", 432, 279, nil)
test("LEGAL", 216, 356, nil)
test("LETTER", 216, 279, nil)
test("TABLOID", 279, 432, nil)
} // Test_getPapreSize_

// -----------------------------------------------------------------------------
Expand Down

0 comments on commit d0f07ba

Please sign in to comment.