From 6f41eff7c35c34502bb72813c95fbd304d789174 Mon Sep 17 00:00:00 2001 From: joeyak Date: Mon, 1 Jul 2024 23:15:56 -0400 Subject: [PATCH] Added a test program and made some changes to returned errors --- README.md | 10 ++ cmd/test-printer/main.go | 293 +++++++++++++++++++++++++++++++++++++++ printer.go | 58 ++++---- 3 files changed, 335 insertions(+), 26 deletions(-) create mode 100644 cmd/test-printer/main.go diff --git a/README.md b/README.md index 579f91d..c911a81 100644 --- a/README.md +++ b/README.md @@ -38,3 +38,13 @@ func main() { printer.Cut() } ``` + +## Testing + +What? Did I hear you ask for testing? You think we make useless mocks that only tests our assumptions about the hoin printer instead of REAL **HONEST** ***GOOD*** boots on the ground testing. + +Run `go run ./cmd/test-printer/` to print out our test program. + +Really, how are we supposed to tests without a firmware dump? Total incongruity. + +Also the test program assumes some things will work line printing and the such, cause how can we test functions without that. It'd be obvious if nothing prints. The goal is to test all the extra functions like horizontal tabbing, justifications, images, etc. diff --git a/cmd/test-printer/main.go b/cmd/test-printer/main.go new file mode 100644 index 0000000..47fcbd4 --- /dev/null +++ b/cmd/test-printer/main.go @@ -0,0 +1,293 @@ +package main + +import ( + "fmt" + "os" + "reflect" + "runtime" + "strings" + "time" + + "github.com/joeyak/hoin-printer" +) + +func runTest(testName string, testFunc func(hoin.Printer) error) error { + printer, err := hoin.NewIpPrinter(hoin.DefaultPrinterIP) + if err != nil { + return fmt.Errorf("failed test %s: %w", testName, err) + } + defer printer.Close() + + printer.Initialize() + printer.Println("=== ", testName, " ===") + defer printer.LF() + + err = testFunc(printer) + if err != nil { + return fmt.Errorf("failed test %s: %w", testName, err) + } + + return nil +} + +func cleanup() { + printer, err := hoin.NewIpPrinter(hoin.DefaultPrinterIP) + if err != nil { + fmt.Printf("could not create new printer to feed lines: %s\n", err) + os.Exit(1) + } + defer printer.Close() + + printer.Println("##### ", time.Now().Format(time.DateOnly+" "+time.Kitchen), " #####") + printer.FeedLines(10) +} + +func main() { + tests := []func(hoin.Printer) error{ + testBeep, + testHT, + testLineSpacing, + testBold, + testRotate90, + testReversePrinter, + testFonts, + testJustify, + } + + var errors []error + + for i, test := range tests { + testName := strings.TrimPrefix(runtime.FuncForPC(reflect.ValueOf(test).Pointer()).Name(), "main.") + fmt.Printf("Running test [%d/%d] %s - ", i+1, len(tests), testName) + + err := runTest(testName, test) + if err != nil { + fmt.Println("fail") + errors = append(errors, err) + } + + fmt.Println("pass") + } + + cleanup() + + if len(errors) > 0 { + fmt.Printf("%d errors occured\n", len(errors)) + for _, err := range errors { + fmt.Println(err) + } + os.Exit(1) + } + +} + +func testBeep(printer hoin.Printer) error { + return printer.Beep(1, 1) +} + +func testHT(printer hoin.Printer) error { + err := printer.Print("-") + if err != nil { + return fmt.Errorf("could not print HT prefix: %w", err) + } + + err = printer.SetHT(10) + if err != nil { + return fmt.Errorf("could not set HT positions: %w", err) + } + defer printer.SetHT() + + err = printer.HT() + if err != nil { + return fmt.Errorf("could not print HT prefix: %w", err) + } + + err = printer.Println("- 10 character tab") + if err != nil { + return fmt.Errorf("could not print HT suffix: %w", err) + } + + err = printer.Println("~", strings.Repeat("-", 9), "~") + if err != nil { + return fmt.Errorf("could not print ruler line: %w", err) + } + + return nil +} + +func testLineSpacing(printer hoin.Printer) error { + defer printer.ResetLineSpacing() + + for _, spacing := range []int{0, 255} { + err := printer.SetLineSpacing(spacing) + if err != nil { + return fmt.Errorf("could not set line spacing to %d: %w", spacing, err) + } + + err = printer.Printf("Spacing %d start\n", spacing) + if err != nil { + return fmt.Errorf("could not print line spacing %d start", spacing) + } + err = printer.Printf("Spacing %d end\n", spacing) + if err != nil { + return fmt.Errorf("could not print line spacing %d end", spacing) + } + } + + err := printer.ResetLineSpacing() + if err != nil { + return err + } + + err = printer.Println("Reset spacing start") + if err != nil { + return fmt.Errorf("could not print line spacing reset start: %w", err) + } + + err = printer.Println("Reset spacing end") + if err != nil { + return fmt.Errorf("could not print line spacing reset end: %w", err) + } + + return nil +} + +func testBold(printer hoin.Printer) error { + defer printer.SetBold(false) + + err := printer.Print("Normal ") + if err != nil { + return fmt.Errorf("could not print start control text: %w", err) + } + + err = printer.SetBold(true) + if err != nil { + return err + } + + err = printer.Print("Bold") + if err != nil { + return fmt.Errorf("could not print bold text: %w", err) + } + + err = printer.SetBold(false) + if err != nil { + return err + } + + err = printer.Println(" Normal") + if err != nil { + return fmt.Errorf("could not print end control text: %w", err) + } + + return nil +} + +func testRotate90(printer hoin.Printer) error { + defer printer.SetRotate90(false) + + err := printer.Println("Control Text") + if err != nil { + return fmt.Errorf("could not print control text: %w", err) + } + + err = printer.SetRotate90(true) + if err != nil { + return err + } + + err = printer.Println("Rotated Text") + if err != nil { + return fmt.Errorf("could not print rotated text: %w", err) + } + + err = printer.SetRotate90(false) + if err != nil { + return err + } + + return nil +} + +func testReversePrinter(printer hoin.Printer) error { + defer printer.SetReversePrinting(false) + + err := printer.Println("Control Text") + if err != nil { + return fmt.Errorf("could not print control text: %w", err) + } + + err = printer.SetReversePrinting(true) + if err != nil { + return err + } + + err = printer.Println("Reversed Text") + if err != nil { + return fmt.Errorf("could not print reversed text: %w", err) + } + + return nil +} + +func testFonts(printer hoin.Printer) error { + defer printer.SetFont(hoin.FontA) + + err := printer.SetFont(hoin.FontA) + if err != nil { + return err + } + + err = printer.Println("Font A") + if err != nil { + return fmt.Errorf("could not print Font A: %w", err) + } + + err = printer.SetFont(hoin.FontB) + if err != nil { + return err + } + + err = printer.Println("Font B") + if err != nil { + return fmt.Errorf("could not print Font B: %w", err) + } + + return nil +} + +func testJustify(printer hoin.Printer) error { + defer printer.Justify(hoin.LeftJustify) + + err := printer.Justify(hoin.LeftJustify) + if err != nil { + return err + } + + err = printer.Println("Left Justify") + if err != nil { + return fmt.Errorf("could not print Left Justify: %w", err) + } + + err = printer.Justify(hoin.CenterJustify) + if err != nil { + return err + } + + err = printer.Println("Center Justify") + if err != nil { + return fmt.Errorf("could not print Center Justify: %w", err) + } + + err = printer.Justify(hoin.RightJustify) + if err != nil { + return err + } + + err = printer.Println("Right Justify") + if err != nil { + return fmt.Errorf("could not print Right Justify: %w", err) + } + + return nil +} diff --git a/printer.go b/printer.go index f0a022e..6fa57d6 100644 --- a/printer.go +++ b/printer.go @@ -10,6 +10,9 @@ import ( ) const ( + // Default ip and port for hoin printers + DefaultPrinterIP = "192.168.1.23:9100" + HT = 0x09 LF = 0x0A CR = 0x0D @@ -383,16 +386,16 @@ func (p Printer) SetReversePrinting(b bool) error { // n=0 selects font A // n=1 selects font B func (p Printer) SetFont(f Font) error { - errMsg := "could not set font: %w" + errMsg := "could not set font to %v: %w" err := checkEnum(f, FontA, FontB) if err != nil { - return fmt.Errorf(errMsg, err) + return fmt.Errorf(errMsg, f, err) } _, err = p.Write([]byte{ESC, 'M', byte(f)}) if err != nil { - return fmt.Errorf(errMsg, err) + return fmt.Errorf(errMsg, f, err) } return nil @@ -400,16 +403,16 @@ func (p Printer) SetFont(f Font) error { // Justify sets the alignment to n func (p Printer) Justify(j Justification) error { - errMsg := "could not justify: %w" + errMsg := "could not set justify to %v: %w" err := checkEnum(j, CenterJustify, LeftJustify, RightJustify) if err != nil { - return fmt.Errorf(errMsg, err) + return fmt.Errorf(errMsg, j, err) } _, err = p.Write([]byte{ESC, 'a', byte(j)}) if err != nil { - return fmt.Errorf(errMsg, err) + return fmt.Errorf(errMsg, j, err) } return nil } @@ -621,30 +624,33 @@ func checkBarcodeData(data, accepted string) error { // PrintBarCode prints the bar code passed in with data. // // The size ranges are as follows in (Type: min, max): -// BcUPCA: 11, 12 -// BcUPCE: 6, 7 -// BcJAN13: 12, 13 -// BcJAN8: 7, 8 -// BcCODE39: 0, 14 -// BcITF: 0, 22 -// BcCODABAR: 2, 19 -// BcCODE93: 1, 17 -// BcCODE123: 0, 65 +// +// BcUPCA: 11, 12 +// BcUPCE: 6, 7 +// BcJAN13: 12, 13 +// BcJAN8: 7, 8 +// BcCODE39: 0, 14 +// BcITF: 0, 22 +// BcCODABAR: 2, 19 +// BcCODE93: 1, 17 +// BcCODE123: 0, 65 // // For the accepted data values: -// BcUPCA, BcUPCE, BcJAN13, BcJAN8, BcITF all only accept [0123456789] -// BcCODE39, BcCODE93, BcCODE123 can accept [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.*$/+% ] -// BcCODABAR: -// The first and last character of the CODABAR code bar has to be one of [ABCD] -// and the rest of the characters in between can be one of [0123456789-$:/.+] +// +// BcUPCA, BcUPCE, BcJAN13, BcJAN8, BcITF all only accept [0123456789] +// BcCODE39, BcCODE93, BcCODE123 can accept [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-.*$/+% ] +// BcCODABAR: +// The first and last character of the CODABAR code bar has to be one of [ABCD] +// and the rest of the characters in between can be one of [0123456789-$:/.+] // // Note on the CODE123 length: -// ...the docs say it's between 2 and 255 but the printer -// does not have that limit. On one hand it can go down to 0 character, but also I could -// not find the limit for max characters. At 15 characters it went off the page with a -// HOP-E802 printer and at 34 characters it starts printing the HRI weird. At 66 0s -// repeating it seems to break and stop printing, and the same at 65 As repeating. -// Long story short...I think they didn't finish programming the checks on CODE123 +// +// ...the docs say it's between 2 and 255 but the printer +// does not have that limit. On one hand it can go down to 0 character, but also I could +// not find the limit for max characters. At 15 characters it went off the page with a +// HOP-E802 printer and at 34 characters it starts printing the HRI weird. At 66 0s +// repeating it seems to break and stop printing, and the same at 65 As repeating. +// Long story short...I think they didn't finish programming the checks on CODE123 func (p Printer) PrintBarCode(barcodeType BarCode, data string) error { errMsg := "could not print bar code: %w"