summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorKurt Jung <kurt.w.jung@gmail.com>2015-10-20 12:26:10 -0400
committerKurt Jung <kurt.w.jung@gmail.com>2015-10-20 12:26:10 -0400
commite2d935b7215daa9476f1ca385a15227f5f5a7011 (patch)
tree56b837702a1d3b036e081506513d79913389875f /internal
parent26baf7d305543d6afe39d38f0b2c30054c9fa4e3 (diff)
parentf45e5e9196bb313883f36e0ccc72f052f46aa37e (diff)
Merge pull request #49 from jung-kurt/compare-reference-pdf
Compare example PDFs with reference copies
Diffstat (limited to 'internal')
-rw-r--r--internal/example/example.go28
1 files changed, 26 insertions, 2 deletions
diff --git a/internal/example/example.go b/internal/example/example.go
index d6a114c..56ffb23 100644
--- a/internal/example/example.go
+++ b/internal/example/example.go
@@ -22,12 +22,17 @@ import (
"os"
"path/filepath"
"strings"
+ "time"
+
+ "github.com/jung-kurt/gofpdf"
)
var gofpdfDir string
func init() {
setRoot()
+ gofpdf.SetDefaultCatalogSort(true)
+ gofpdf.SetDefaultCreationDate(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
}
// Assign the relative path to the gofpdfDir directory based on current working
@@ -36,8 +41,7 @@ func setRoot() {
wdStr, err := os.Getwd()
if err == nil {
gofpdfDir = ""
- sepStr := string(os.PathSeparator)
- list := strings.Split(wdStr, sepStr)
+ list := strings.Split(filepath.ToSlash(wdStr), "/")
for j := len(list) - 1; j >= 0 && list[j] != "gofpdf"; j-- {
gofpdfDir = filepath.Join(gofpdfDir, "..")
}
@@ -87,12 +91,32 @@ func Filename(baseStr string) string {
return PdfFile(baseStr + ".pdf")
}
+// referenceCompare compares the specified file with the file's reference copy
+// located in the 'reference' subdirectory. All bytes of the two files are
+// compared except for the value of the /CreationDate field in the PDF. This
+// function succeeds if both files are equivalent except for their
+// /CreationDate values or if the reference file does not exist.
+func referenceCompare(fileStr string) (err error) {
+ var refFileStr, refDirStr, dirStr, baseFileStr string
+ dirStr, baseFileStr = filepath.Split(fileStr)
+ refDirStr = filepath.Join(dirStr, "reference")
+ err = os.MkdirAll(refDirStr, 0755)
+ if err == nil {
+ refFileStr = filepath.Join(refDirStr, baseFileStr)
+ err = gofpdf.ComparePDFFiles(fileStr, refFileStr)
+ }
+ return
+}
+
// Summary generates a predictable report for use by test examples. If the
// specified error is nil, the filename delimiters are normalized and the
// filename printed to standard output with a success message. If the specified
// error is not nil, its String() value is printed to standard output.
func Summary(err error, fileStr string) {
if err == nil {
+ err = referenceCompare(fileStr)
+ }
+ if err == nil {
fileStr = filepath.ToSlash(fileStr)
fmt.Printf("Successfully generated %s\n", fileStr)
} else {