package escpos_test import ( "bytes" "testing" "uberbringer/internal/pkg/escpos" ) // Glyph groups the receipt blocks draw with. const ( singleBox = "┌┐└┘─│├┤┬┴┼" doubleBox = "╔╗╚╝═║╠╣╦╩╬" mixedBox = "╒╓╕╖╘╙╛╜╞╟╡╢╤╥╧╨╪╫" shades = "░▒▓█" halfBlocks = "▀▄" sideHalves = "▌▐" markers = "·°■" bulletOp = "∙" check = "√" neverPrinted = "►◄→←" // these map to control codes 0x10-0x1F bulletGeneral = "•" ) func TestCanEncode(t *testing.T) { type want struct { cp escpos.CodePage glyphs string encodes bool } tests := []want{ {escpos.CodePage437, singleBox + doubleBox + mixedBox + shades + halfBlocks + sideHalves + markers + bulletOp + check, true}, {escpos.CodePage437, neverPrinted + bulletGeneral, false}, {escpos.CodePage850, singleBox + doubleBox + shades + halfBlocks + markers, true}, {escpos.CodePage850, mixedBox + sideHalves + bulletOp + check + neverPrinted + bulletGeneral, false}, {escpos.CodePage858, singleBox + doubleBox + shades + halfBlocks + markers + "€", true}, {escpos.CodePage858, mixedBox + sideHalves + bulletOp + check + neverPrinted + bulletGeneral, false}, // Windows-1252 has no box drawing or shades, but has the bullet // at 0x95 and the typographic quotes and dashes. {escpos.CodePage1252, singleBox + doubleBox + mixedBox + shades + halfBlocks + sideHalves + bulletOp + check + "■" + neverPrinted, false}, {escpos.CodePage1252, "·°" + bulletGeneral + "“”‘’–—…€™×", true}, // Every code page: printable ASCII yes, controls and newlines no. {escpos.CodePage437, "A z~ !", true}, {escpos.CodePage437, "\n\t\x00\x1b\x7f\u0085\uFFFD", false}, {escpos.CodePage1252, "\n\t\x00\x1b\x7f\u0085\uFFFD", false}, {escpos.CodePage437, "“”‘’–—…€™×🎉漢", false}, } for _, tt := range tests { for _, r := range tt.glyphs { if got := tt.cp.CanEncode(r); got != tt.encodes { t.Errorf("%s CanEncode(%q) = %v, want %v", tt.cp, r, got, tt.encodes) } } } } // TestCanEncodeMatchesEncode checks that CanEncode is true exactly when // Encode prints the rune as itself rather than as '?' or not at all. func TestCanEncodeMatchesEncode(t *testing.T) { for _, cp := range []escpos.CodePage{escpos.CodePage437, escpos.CodePage850, escpos.CodePage858, escpos.CodePage1252} { for r := rune(0); r < 0x2700; r++ { if r == '?' { continue } out := cp.Encode(string(r)) printsItself := len(out) == 1 && out[0] != '?' && out[0] >= 0x20 if got := cp.CanEncode(r); got != printsItself { t.Errorf("%s CanEncode(%U) = %v, but Encode gives % X", cp, r, got, out) } } } } func TestDecodeRoundTrip(t *testing.T) { tests := []struct { cp escpos.CodePage roundTrip int // bytes 0x20-0xFF that decode to a rune encoding back to them }{ {escpos.CodePage437, 223}, {escpos.CodePage850, 223}, {escpos.CodePage858, 223}, {escpos.CodePage1252, 218}, // 0x81, 0x8D, 0x8F, 0x90 and 0x9D are unassigned } for _, tt := range tests { n := 0 for c := 0x20; c <= 0xFF; c++ { b := []byte{byte(c)} s := tt.cp.Decode(b) r := []rune(s) if len(r) != 1 { t.Fatalf("%s Decode(% X) = %q, want one rune", tt.cp, b, s) } if !tt.cp.CanEncode(r[0]) { continue } if got := tt.cp.Encode(s); !bytes.Equal(got, b) { t.Errorf("%s: % X decodes to %q, which encodes to % X", tt.cp, b, s, got) } n++ } if n != tt.roundTrip { t.Errorf("%s: %d bytes round-trip, want %d", tt.cp, n, tt.roundTrip) } } text := "┌─ Grüße £5 ─┐ ░▒▓ ·√°" if got := escpos.CodePage437.Decode(escpos.CodePage437.Encode(text)); got != text { t.Errorf("round trip = %q, want %q", got, text) } }