buffer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var browserify = require('../');
  2. var test = require('tap').test;
  3. var vm = require('vm');
  4. if (!ArrayBuffer.isView) ArrayBuffer.isView = function () { return false; };
  5. test('utf8 buffer to base64', function (t) {
  6. t.plan(1);
  7. var b = browserify();
  8. b.require('buffer');
  9. b.bundle(function (err, src) {
  10. if (err) return t.fail(err);
  11. var c = context();
  12. vm.runInNewContext(src, c);
  13. t.equal(
  14. new c.require('buffer').Buffer("Ձאab", "utf8").toString("base64"),
  15. new Buffer("Ձאab", "utf8").toString("base64")
  16. );
  17. });
  18. });
  19. test('utf8 buffer to hex', function (t) {
  20. t.plan(1);
  21. var b = browserify();
  22. b.require('buffer');
  23. b.bundle(function (err, src) {
  24. var c = context();
  25. vm.runInNewContext(src, c);
  26. t.equal(
  27. new c.require('buffer').Buffer("Ձאab", "utf8").toString("hex"),
  28. new Buffer("Ձאab", "utf8").toString("hex")
  29. );
  30. });
  31. });
  32. test('ascii buffer to base64', function (t) {
  33. t.plan(1);
  34. var b = browserify();
  35. b.require('buffer');
  36. b.bundle(function (err, src) {
  37. var c = context();
  38. vm.runInNewContext(src, c);
  39. t.equal(
  40. new c.require('buffer').Buffer("123456!@#$%^", "ascii").toString("base64"),
  41. new Buffer("123456!@#$%^", "ascii").toString("base64")
  42. );
  43. });
  44. });
  45. test('ascii buffer to hex', function (t) {
  46. t.plan(1);
  47. var b = browserify();
  48. b.require('buffer');
  49. b.bundle(function (err, src) {
  50. var c = context();
  51. vm.runInNewContext(src, c);
  52. t.equal(
  53. new c.require('buffer').Buffer("123456!@#$%^", "ascii").toString("hex"),
  54. new Buffer("123456!@#$%^", "ascii").toString("hex")
  55. );
  56. });
  57. });
  58. test('base64 buffer to utf8', function (t) {
  59. t.plan(1);
  60. var b = browserify();
  61. b.require('buffer');
  62. b.bundle(function (err, src) {
  63. var c = context();
  64. vm.runInNewContext(src, c);
  65. t.equal(
  66. new c.require('buffer').Buffer("1YHXkGFi", "base64").toString("utf8"),
  67. new Buffer("1YHXkGFi", "base64").toString("utf8")
  68. );
  69. });
  70. });
  71. test('hex buffer to utf8', function (t) {
  72. t.plan(1);
  73. var b = browserify();
  74. b.require('buffer');
  75. b.bundle(function (err, src) {
  76. var c = context();
  77. vm.runInNewContext(src, c);
  78. var B = c.require('buffer');
  79. t.equal(
  80. new B.Buffer("d581d7906162", "hex").toString("utf8"),
  81. new Buffer("d581d7906162", "hex").toString("utf8")
  82. );
  83. });
  84. });
  85. test('base64 buffer to ascii', function (t) {
  86. t.plan(1);
  87. var b = browserify();
  88. b.require('buffer');
  89. b.bundle(function (err, src) {
  90. var c = context();
  91. vm.runInNewContext(src, c);
  92. t.equal(
  93. new c.require('buffer').Buffer("MTIzNDU2IUAjJCVe", "base64").toString("ascii"),
  94. new Buffer("MTIzNDU2IUAjJCVe", "base64").toString("ascii")
  95. );
  96. });
  97. });
  98. test('hex buffer to ascii', function (t) {
  99. t.plan(1);
  100. var b = browserify();
  101. b.require('buffer');
  102. b.bundle(function (err, src) {
  103. var c = context();
  104. vm.runInNewContext(src, c);
  105. t.equal(
  106. new c.require('buffer').Buffer("31323334353621402324255e", "hex").toString("ascii"),
  107. new Buffer("31323334353621402324255e", "hex").toString("ascii")
  108. );
  109. });
  110. });
  111. test('indexing a buffer', function (t) {
  112. t.plan(5);
  113. var b = browserify();
  114. b.require('buffer');
  115. b.bundle(function (err, src) {
  116. var c = context();
  117. vm.runInNewContext(src, c);
  118. var buf = c.require('buffer').Buffer('abc');
  119. t.equal(buf[0], 97);
  120. t.equal(buf[1], 98);
  121. t.equal(buf[2], 99);
  122. t.equal(buf[3], undefined);
  123. t.equal(buf.toString('utf8'), 'abc');
  124. });
  125. });
  126. function context () {
  127. return {
  128. ArrayBuffer: ArrayBuffer,
  129. Uint8Array: Uint8Array,
  130. DataView: DataView
  131. };
  132. }