You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.3 KiB

  1. (function(){
  2. "use strict";
  3. var base64_arraybuffer = require('../lib/base64-arraybuffer.js');
  4. /*
  5. ======== A Handy Little Nodeunit Reference ========
  6. https://github.com/caolan/nodeunit
  7. Test methods:
  8. test.expect(numAssertions)
  9. test.done()
  10. Test assertions:
  11. test.ok(value, [message])
  12. test.equal(actual, expected, [message])
  13. test.notEqual(actual, expected, [message])
  14. test.deepEqual(actual, expected, [message])
  15. test.notDeepEqual(actual, expected, [message])
  16. test.strictEqual(actual, expected, [message])
  17. test.notStrictEqual(actual, expected, [message])
  18. test.throws(block, [error], [message])
  19. test.doesNotThrow(block, [error], [message])
  20. test.ifError(value)
  21. */
  22. function stringArrayBuffer(str) {
  23. var buffer = new ArrayBuffer(str.length);
  24. var bytes = new Uint8Array(buffer);
  25. str.split('').forEach(function(str, i) {
  26. bytes[i] = str.charCodeAt(0);
  27. });
  28. return buffer;
  29. }
  30. function testArrayBuffers(buffer1, buffer2) {
  31. var len1 = buffer1.byteLength,
  32. len2 = buffer2.byteLength;
  33. if (len1 !== len2) {
  34. console.log(buffer1, buffer2);
  35. return false;
  36. }
  37. for (var i = 0; i < len1; i++) {
  38. if (buffer1[i] !== buffer1[i]) {
  39. console.log(i, buffer1, buffer2);
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. exports['base64tests'] = {
  46. 'encode': function(test) {
  47. test.expect(4);
  48. test.equal(base64_arraybuffer.encode(stringArrayBuffer("Hello world")), "SGVsbG8gd29ybGQ=", 'encode "Hello world"');
  49. test.equal(base64_arraybuffer.encode(stringArrayBuffer("Man")), 'TWFu', 'encode "Man"');
  50. test.equal(base64_arraybuffer.encode(stringArrayBuffer("Ma")), "TWE=", 'encode "Ma"');
  51. test.equal(base64_arraybuffer.encode(stringArrayBuffer("Hello worlds!")), "SGVsbG8gd29ybGRzIQ==", 'encode "Hello worlds!"');
  52. test.done();
  53. },
  54. 'decode': function(test) {
  55. test.expect(3);
  56. test.ok(testArrayBuffers(base64_arraybuffer.decode("TWFu"), stringArrayBuffer("Man")), 'decode "Man"');
  57. test.ok(testArrayBuffers(base64_arraybuffer.decode("SGVsbG8gd29ybGQ="), stringArrayBuffer("Hello world")), 'decode "Hello world"');
  58. test.ok(testArrayBuffers(base64_arraybuffer.decode("SGVsbG8gd29ybGRzIQ=="), stringArrayBuffer("Hello worlds!")), 'decode "Hello worlds!"');
  59. test.done();
  60. }
  61. };
  62. })();