diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index a8ee3b5..f38ccb6 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -6,7 +6,7 @@ on: - workflow_dispatch jobs: - test: + vet: runs-on: ubuntu-latest steps: @@ -18,6 +18,3 @@ jobs: - name: vet run: go vet ./... - - - name: test - run: go test ./... diff --git a/printer_test.go b/printer_test.go deleted file mode 100644 index e40bbae..0000000 --- a/printer_test.go +++ /dev/null @@ -1,423 +0,0 @@ -package hoin_test - -import ( - "bytes" - "fmt" - "testing" - - "github.com/joeyak/hoin-printer" - "github.com/stretchr/testify/assert" -) - -func convertIntsToBytes(a []int) []byte { - var data []byte - for _, b := range a { - data = append(data, byte(b)) - } - return data -} - -func newPrinter() (*bytes.Buffer, hoin.Printer) { - buffer := &bytes.Buffer{} - return buffer, hoin.NewPrinter(buffer) -} - -func TestHT(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.HT() - - assert.NoError(t, err) - assert.Equal(t, "\x09", buffer.String()) -} - -func TestLF(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.LF() - - assert.NoError(t, err) - assert.Equal(t, "\x0A", buffer.String()) -} - -func TestCR(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.CR() - - assert.NoError(t, err) - assert.Equal(t, "\x0D", buffer.String()) -} - -func TestInitialize(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.Initialize() - - assert.NoError(t, err) - assert.Equal(t, []byte{hoin.ESC, '@'}, buffer.Bytes()) -} - -func FuzzWrite(f *testing.F) { - f.Add([]byte("Test")) - f.Fuzz(func(t *testing.T, b []byte) { - buffer, printer := newPrinter() - - n, err := printer.Write(b) - - assert.NoError(t, err) - assert.Equal(t, len(b), n) - assert.Equal(t, string(b), buffer.String()) - }) -} - -func FuzzRead(f *testing.F) { - f.Add([]byte("Test"), 0) - f.Fuzz(func(t *testing.T, b []byte, n int) { - buffer, printer := newPrinter() - buffer.Write(b) - - data := make([]byte, len(b)+n) - n, err := printer.Read(data) - - assert.NoError(t, err) - assert.Equal(t, len(b), n) - assert.Equal(t, b, data[:n]) - }) -} - -func FuzzPrintln(f *testing.F) { - f.Add("Test") - f.Fuzz(func(t *testing.T, s string) { - buffer, printer := newPrinter() - - err := printer.Println(s) - - assert.NoError(t, err) - assert.Equal(t, s+"\x0A", buffer.String()) - }) -} - -func FuzzPrintf(f *testing.F) { - f.Add("Test") - f.Fuzz(func(t *testing.T, s string) { - a := []interface{}{1, 2} - format := fmt.Sprintf("%s %%d %%d", s) - expected := fmt.Sprintf(format, a...) - - buffer, printer := newPrinter() - - err := printer.Printf(format, a...) - - assert.NoError(t, err) - assert.Equal(t, expected, buffer.String()) - }) -} - -func FuzzPrint(f *testing.F) { - f.Add("Test") - f.Fuzz(func(t *testing.T, s string) { - buffer, printer := newPrinter() - - err := printer.Print(s) - - assert.NoError(t, err) - assert.Equal(t, s, buffer.String()) - }) -} - -func TestCut(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.Cut() - - assert.NoError(t, err) - assert.Equal(t, "\x1DV\x00", buffer.String()) -} - -func TestCutFeed(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.CutFeed(5) - - assert.NoError(t, err) - assert.Equal(t, "\x1DV\x42\x05", buffer.String()) -} - -func TestResetLineSpacing(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.ResetLineSpacing() - - assert.NoError(t, err) - assert.Equal(t, "\x1B2", buffer.String()) -} - -func TestSetLineSpacing(t *testing.T) { - testCases := []struct { - units int - err bool - }{ - {-1, true}, - {0, false}, - {1, false}, - {100, false}, - {255, false}, - {256, true}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("%d:%t", tc.units, tc.err), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.SetLineSpacing(tc.units) - - if tc.err { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, '3', byte(tc.units)}, buffer.Bytes()) - } - }) - } -} - -func TestFeed(t *testing.T) { - testCases := []struct { - units int - err bool - }{ - {-1, true}, - {0, false}, - {1, false}, - {100, false}, - {255, false}, - {256, true}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("%d:%t", tc.units, tc.err), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.Feed(tc.units) - - if tc.err { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'J', byte(tc.units)}, buffer.Bytes()[buffer.Len()-3:]) - } - }) - } -} - -func TestFeedLines(t *testing.T) { - testCases := []struct { - units int - err bool - }{ - {-1, true}, - {0, false}, - {1, false}, - {100, false}, - {255, false}, - {256, true}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("%d:%t", tc.units, tc.err), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.FeedLines(tc.units) - - if tc.err { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'd', byte(tc.units)}, buffer.Bytes()[buffer.Len()-3:]) - } - }) - } -} - -func TestSetHTPositions(t *testing.T) { - testCases := [][]int{ - {}, - {1}, - {4}, - {4, 4, 4}, - } - - for _, b := range testCases { - t.Run(fmt.Sprint(b), func(t *testing.T) { - expected := convertIntsToBytes(append(append([]int{0x1B, 'D'}, b...), 0)) - buffer, printer := newPrinter() - - err := printer.SetHT(b...) - - assert.NoError(t, err) - assert.EqualValues(t, expected, buffer.Bytes()) - }) - } -} - -func TestSetHTPositionsError(t *testing.T) { - testCases := [][]int{ - {-1}, - {256}, - make([]int, 256), - } - - for _, b := range testCases { - t.Run(fmt.Sprint(b), func(t *testing.T) { - _, printer := newPrinter() - - err := printer.SetHT(b...) - - assert.Error(t, err) - }) - } -} - -func TestSetBold(t *testing.T) { - testCases := []struct { - input bool - output byte - }{ - {false, 0}, - {true, 1}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprint(tc.input), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.SetBold(tc.input) - - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'E', tc.output}, buffer.Bytes()) - }) - } -} - -func TestSetFont(t *testing.T) { - for _, i := range []int{0, 1} { - t.Run(fmt.Sprint(i), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.SetFont(i) - - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'M', byte(i)}, buffer.Bytes()) - }) - } -} - -func TestSetFontErr(t *testing.T) { - for _, i := range []int{-1, 2, 3} { - t.Run(fmt.Sprint(i), func(t *testing.T) { - _, printer := newPrinter() - - err := printer.SetFont(i) - - assert.Error(t, err) - }) - } -} - -func TestSetRotate90(t *testing.T) { - testCases := []struct { - input bool - output byte - }{ - {false, 0}, - {true, 1}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprint(tc.input), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.SetRotate90(tc.input) - - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'V', tc.output}, buffer.Bytes()) - }) - } -} - -func TestSetReversePrinting(t *testing.T) { - testCases := []struct { - input bool - output byte - }{ - {false, 0}, - {true, 1}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprint(tc.input), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.SetReversePrinting(tc.input) - - assert.NoError(t, err) - assert.Equal(t, []byte{0x1D, 'B', tc.output}, buffer.Bytes()) - }) - } -} - -func TestBeep(t *testing.T) { - testCases := []struct { - n, t int - err bool - }{ - {0, 0, true}, - {1, 0, true}, - {0, 1, true}, - {1, 1, false}, - {9, 9, false}, - {10, 10, true}, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("%d-%d-%t", tc.n, tc.t, tc.err), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.Beep(tc.n, tc.t) - - if tc.err { - assert.Error(t, err) - } else { - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'B', byte(tc.n), byte(tc.t)}, buffer.Bytes()[buffer.Len()-4:]) - } - }) - } -} - -func TestJustify(t *testing.T) { - for _, j := range []hoin.Justification{hoin.LeftJustify, hoin.CenterJustify, hoin.RightJustify} { - t.Run(fmt.Sprint(j), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.Justify(j) - - assert.NoError(t, err) - assert.Equal(t, []byte{0x1B, 'a', byte(j)}, buffer.Bytes()) - }) - } -} - -func TestSetHRIPosition(t *testing.T) { - for _, hp := range []hoin.HRIPosition{hoin.HRINone, hoin.HRIAbove, hoin.HRIBelow, hoin.HRIBoth} { - t.Run(fmt.Sprint(hp), func(t *testing.T) { - buffer, printer := newPrinter() - - err := printer.SetHRIPosition(hp) - - assert.NoError(t, err) - assert.Equal(t, []byte{0x1D, 'H', byte(hp)}, buffer.Bytes()) - }) - } -} diff --git a/transmit_test.go b/transmit_test.go deleted file mode 100644 index 19430af..0000000 --- a/transmit_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package hoin_test - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestTransmitPrinterStatus(t *testing.T) { - _, printer := newPrinter() - - _, err := printer.TransmitPrinterStatus() - - assert.NoError(t, err) -} - -func TestTransmitOfflineStatus(t *testing.T) { - _, printer := newPrinter() - - _, err := printer.TransmitOfflineStatus() - - assert.NoError(t, err) -} - -func TestTransmitErrorStatus(t *testing.T) { - _, printer := newPrinter() - - _, err := printer.TransmitErrorStatus() - - assert.NoError(t, err) -} - -func TestTransmitPaperSensorStatus(t *testing.T) { - _, printer := newPrinter() - - _, err := printer.TransmitPaperSensorStatus() - - assert.NoError(t, err) -}