encoder_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package ean
  2. import (
  3. "image/color"
  4. "testing"
  5. )
  6. func testHelper(t *testing.T, testCode, testResult, kind string, checkMetadata bool) {
  7. code, err := Encode(testCode)
  8. if err != nil {
  9. t.Error(err)
  10. }
  11. if checkMetadata && (code.Metadata().Dimensions != 1 || code.Content() != testCode || code.Metadata().CodeKind != kind) {
  12. t.Error("Metadata missmatch")
  13. }
  14. if len(testResult) != code.Bounds().Max.X {
  15. t.Fail()
  16. }
  17. for i, r := range testResult {
  18. if (code.At(i, 0) == color.Black) != (r == '1') {
  19. t.Fail()
  20. }
  21. }
  22. }
  23. func Test_EncodeEAN(t *testing.T) {
  24. testHelper(t, "5901234123457", "10100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101", "EAN 13", true)
  25. testHelper(t, "55123457", "1010110001011000100110010010011010101000010101110010011101000100101", "EAN 8", true)
  26. testHelper(t, "5512345", "1010110001011000100110010010011010101000010101110010011101000100101", "EAN 8", false)
  27. _, err := Encode("55123458") //<-- Invalid checksum
  28. if err == nil {
  29. t.Error("Invalid checksum not detected")
  30. }
  31. _, err = Encode("invalid")
  32. if err == nil {
  33. t.Error("\"invalid\" should not be encodable")
  34. }
  35. _, err = Encode("invalid")
  36. if err == nil {
  37. t.Error("\"invalid\" should not be encodable")
  38. }
  39. bits := encodeEAN13("invalid error")
  40. if bits != nil {
  41. t.Error("\"invalid error\" should not be encodable")
  42. }
  43. }