diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/example/example.go | 28 |
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 { |