EAN-UPC.test.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var assert = require('assert');
  2. var JsBarcode = require('../../bin/JsBarcode.js');
  3. var Canvas = require("canvas");
  4. var help = require("./help/help");
  5. var clone = help.clone;
  6. var options = {height: 100, displayValue: true, fontSize: 20, textMargin: 2, width: 2};
  7. describe('UPC-A', function() {
  8. it('should be able to include the encoder(s)', function () {
  9. UPC = JsBarcode.getModule("UPC");
  10. });
  11. it('should be able to encode normal text', function () {
  12. var enc = new UPC("123456789999", clone(options));
  13. assert.equal("10100110010010011011110101000110110001010111101010100010010010001110100111010011101001110100101"
  14. , help.fixBin(enc.encode()));
  15. });
  16. it('should warn with invalid text', function () {
  17. var enc = new UPC("12345", clone(options));
  18. assert.equal(false, enc.valid());
  19. });
  20. it('should auto include the checksum if missing', function () {
  21. var enc = new UPC("12345678999", clone(options));
  22. assert.equal("123456789999", help.fixText(enc.encode()));
  23. });
  24. it('should work with text option', function () {
  25. var enc = new UPC("12345678999", help.merge(options, {text: "THISISTEXT"}));
  26. assert.equal("THISISTEXT", help.fixText(enc.encode()));
  27. });
  28. it('should work with flat option', function () {
  29. var enc = new UPC("123456789999", help.merge(options, {flat: true}));
  30. assert.equal("10100110010010011011110101000110110001010111101010100010010010001110100111010011101001110100101"
  31. , enc.encode().data);
  32. assert.equal("123456789999", enc.encode().text);
  33. });
  34. });
  35. const UPCE_BINARY = "101011001100100110011101011100101110110011001010101";
  36. describe('UPC-E', function() {
  37. it('should be able to include the encoder(s)', function () {
  38. UPCE = JsBarcode.getModule("UPCE");
  39. });
  40. it('should be able to encode 8-digit codes', function () {
  41. var enc = new UPCE("01245714", clone(options));
  42. assert.equal(UPCE_BINARY, help.fixBin(enc.encode()));
  43. });
  44. it('should be able to encode 6-digit codes by assuming a 0 number system', function () {
  45. var enc = new UPCE("124571", clone(options));
  46. assert.equal(UPCE_BINARY, help.fixBin(enc.encode()));
  47. });
  48. it('should warn with invalid text', function () {
  49. var enc = new UPCE("01245715", clone(options));
  50. assert.equal(false, enc.valid());
  51. });
  52. it('should work with text option', function () {
  53. var enc = new UPCE("124571", help.merge(options, {text: "SOMETEXT"}));
  54. assert.equal("SOMETEXT", help.fixText(enc.encode()));
  55. });
  56. it('should work with flat option', function () {
  57. var enc = new UPCE("01245714", help.merge(options, {flat: true}));
  58. assert.equal(UPCE_BINARY, enc.encode().data);
  59. assert.equal("01245714", enc.encode().text);
  60. });
  61. });
  62. describe('EAN', function() {
  63. it('should be able to include the encoder(s)', function () {
  64. EAN = JsBarcode.getModule("EAN13");
  65. });
  66. it('should be able to encode normal text', function () {
  67. var enc = new EAN("5901234123457", clone(options));
  68. assert.equal(true, enc.valid());
  69. assert.equal("10100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101"
  70. , help.fixBin(enc.encode()));
  71. assert.equal("5901234123457", help.fixText(enc.encode()));
  72. });
  73. it('should be able to encode normal text with flat option', function () {
  74. var enc = new EAN("5901234123457", help.merge(options, {flat: true}));
  75. assert.equal(true, enc.valid());
  76. assert.equal("10100010110100111011001100100110111101001110101010110011011011001000010101110010011101000100101"
  77. , enc.encode().data);
  78. assert.equal("5901234123457", help.fixText(enc.encode()));
  79. });
  80. it('should warn with invalid text', function () {
  81. var enc = new EAN("12345", {});
  82. assert.equal(false, enc.valid());
  83. var enc = new EAN("5901234123456 ", {});
  84. assert.equal(false, enc.valid());
  85. });
  86. it('should auto include the checksum if missing', function () {
  87. var enc = new EAN("590123412345", clone(options));
  88. assert.equal("5901234123457", help.fixText(enc.encode()));
  89. });
  90. it('should work with text option', function () {
  91. var enc = new EAN("12345678999", help.merge(options, {text: "THISISTEXT"}));
  92. assert.equal("THISISTEXT", help.fixText(enc.encode()));
  93. });
  94. });
  95. describe('EAN-8', function() {
  96. it('should be able to include the encoder(s)', function () {
  97. EAN8 = JsBarcode.getModule("EAN8");
  98. });
  99. it('should be able to encode normal text', function () {
  100. var enc = new EAN8("96385074", {});
  101. assert.equal(true, enc.valid());
  102. assert.equal("1010001011010111101111010110111010101001110111001010001001011100101"
  103. , help.fixBin(enc.encode()));
  104. assert.equal("96385074", help.fixText(enc.encode()));
  105. });
  106. it('should be able to encode normal text with flat option', function () {
  107. var enc = new EAN8("96385074", help.merge(options, {flat: true}));
  108. assert.equal(true, enc.valid());
  109. assert.equal("1010001011010111101111010110111010101001110111001010001001011100101"
  110. , enc.encode().data);
  111. assert.equal("96385074", help.fixText(enc.encode()));
  112. });
  113. it('should auto include the checksum if missing', function () {
  114. var enc = new EAN8("9638507", {});
  115. assert.equal(true, enc.valid());
  116. assert.equal("96385074", help.fixText(enc.encode()));
  117. assert.equal("1010001011010111101111010110111010101001110111001010001001011100101"
  118. , help.fixBin(enc.encode()));
  119. });
  120. it('should warn with invalid text', function () {
  121. var enc = new EAN8("12345", {});
  122. assert.equal(false, enc.valid());
  123. var enc = new EAN8("96385073", {});
  124. assert.equal(false, enc.valid());
  125. });
  126. it('should work with text option', function () {
  127. var enc = new EAN8("96385074", help.merge(options, {text: "THISISTEXT", flat: true}));
  128. assert.equal("THISISTEXT", help.fixText(enc.encode()));
  129. });
  130. });
  131. describe('EAN-5', function() {
  132. it('should be able to include the encoder(s)', function () {
  133. EAN5 = JsBarcode.getModule("EAN5");
  134. });
  135. it('should be able to encode normal text', function () {
  136. var enc = new EAN5("54495", {});
  137. assert.equal(true, enc.valid());
  138. assert.equal("10110110001010100011010011101010001011010111001"
  139. , enc.encode().data);
  140. var enc = new EAN5("12345", {});
  141. assert.equal(true, enc.valid());
  142. assert.equal("10110110011010010011010100001010100011010110001"
  143. , enc.encode().data);
  144. });
  145. it('should warn with invalid text', function () {
  146. var enc = new EAN5("1234", {});
  147. assert.equal(false, enc.valid());
  148. var enc = new EAN5("123a5", {});
  149. assert.equal(false, enc.valid());
  150. });
  151. it('should work with text option', function () {
  152. var enc = new EAN5("12345", help.merge(options, {text: "THISISTEXT"}));
  153. assert.equal("THISISTEXT", help.fixText(enc.encode()));
  154. });
  155. });
  156. describe('EAN-2', function() {
  157. it('should be able to include the encoder(s)', function () {
  158. EAN2 = JsBarcode.getModule("EAN2");
  159. });
  160. it('should be able to encode normal text', function () {
  161. var enc = new EAN2("53", {});
  162. assert.equal(true, enc.valid());
  163. assert.equal("10110110001010100001"
  164. , enc.encode().data);
  165. var enc = new EAN2("12", {});
  166. assert.equal(true, enc.valid());
  167. assert.equal("10110011001010010011"
  168. , enc.encode().data);
  169. });
  170. it('should warn with invalid text', function () {
  171. var enc = new EAN2("1", {});
  172. assert.equal(false, enc.valid());
  173. var enc = new EAN2("a2", {});
  174. assert.equal(false, enc.valid());
  175. });
  176. it('should work with text option', function () {
  177. var enc = new EAN2("12", help.merge(options, {text: "THISISTEXT"}));
  178. assert.equal("THISISTEXT", help.fixText(enc.encode()));
  179. });
  180. });