From 18c33c52f49afda63dabe7952b0ed98de6aead3a Mon Sep 17 00:00:00 2001 From: Kurt Jung Date: Wed, 26 Aug 2015 20:44:07 -0400 Subject: Factor various test example helper routines into their own internal package --- internal/example/example.go | 104 +++++++++++++++++++++++++++++++++++++++ internal/example/example_test.go | 31 ++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 internal/example/example.go create mode 100644 internal/example/example_test.go (limited to 'internal') diff --git a/internal/example/example.go b/internal/example/example.go new file mode 100644 index 0000000..8486994 --- /dev/null +++ b/internal/example/example.go @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung) + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +/* +Package example provides some helper routines for the test packages of +gofpdf and its various contributed packages located beneath the contrib +directory. +*/ +package example + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +var gofpdfDir string + +func init() { + setRoot() +} + +// Assign the relative path to the gofpdfDir directory based on current working +// directory +func setRoot() { + wdStr, err := os.Getwd() + if err == nil { + gofpdfDir = "" + sepStr := string(os.PathSeparator) + list := strings.Split(wdStr, sepStr) + for j := len(list) - 1; j >= 0 && list[j] != "gofpdf"; j-- { + gofpdfDir = filepath.Join(gofpdfDir, "..") + } + } else { + panic(err) + } +} + +// ImageFile returns a qualified filename in which the path to the image +// directory is prepended to the specified filename. +func ImageFile(fileStr string) string { + return filepath.Join(gofpdfDir, "image", fileStr) +} + +// FontDir returns the path to the font directory. +func FontDir() string { + return filepath.Join(gofpdfDir, "font") +} + +// FontFile returns a qualified filename in which the path to the font +// directory is prepended to the specified filename. +func FontFile(fileStr string) string { + return filepath.Join(FontDir(), fileStr) +} + +// TextFile returns a qualified filename in which the path to the text +// directory is prepended to the specified filename. +func TextFile(fileStr string) string { + return filepath.Join(gofpdfDir, "text", fileStr) +} + +// PdfDir returns the path to the PDF output directory. +func PdfDir() string { + return filepath.Join(gofpdfDir, "pdf") +} + +// PdfFile returns a qualified filename in which the path to the PDF output +// directory is prepended to the specified filename. +func PdfFile(fileStr string) string { + return filepath.Join(PdfDir(), fileStr) +} + +// Filename returns a qualified filename in which the example PDF directory +// path is prepended and the suffix ".pdf" is appended to the specified +// filename. +func Filename(baseStr string) string { + return PdfFile(baseStr + ".pdf") +} + +// 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 { + fileStr = filepath.ToSlash(fileStr) + fmt.Printf("Successfully generated %s\n", fileStr) + } else { + fmt.Println(err) + } +} diff --git a/internal/example/example_test.go b/internal/example/example_test.go new file mode 100644 index 0000000..54f4689 --- /dev/null +++ b/internal/example/example_test.go @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015 Kurt Jung (Gmail: kurt.w.jung) + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +package example_test + +import ( + "fmt" + "path/filepath" + + "github.com/jung-kurt/gofpdf/internal/example" +) + +// Test the Filename() function. +func ExampleExample_Filename() { + fmt.Println(filepath.ToSlash(example.Filename("foo"))) + // Output: + // ../../pdf/foo.pdf +} -- cgit v1.2.1-24-ge1ad