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.

108 lines
2.7 KiB

  1. var hasBinary = require('./');
  2. var assert = require('better-assert');
  3. var fs = require('fs');
  4. var start = new Date();
  5. describe('has-binarydata', function(){
  6. it('should work with buffer', function(){
  7. assert(hasBinary(fs.readFileSync('./test.js')));
  8. });
  9. it('should work with an array that does not contain binary', function() {
  10. var arr = [1, 'cool', 2];
  11. assert(!hasBinary(arr));
  12. });
  13. it('should work with an array that contains a buffer', function() {
  14. var arr = [1, new Buffer('asdfasdf', 'utf8'), 2];
  15. assert(hasBinary(arr));
  16. });
  17. it('should work with an object that does not contain binary', function() {
  18. var ob = {a: 'a', b: [], c: 1234};
  19. assert(!hasBinary(ob));
  20. });
  21. it('should work with an object that contains a buffer', function() {
  22. var ob = {a: 'a', b: new Buffer('abc'), c: 1234};
  23. assert(hasBinary(ob));
  24. });
  25. it('should work with null', function() {
  26. assert(!hasBinary(null));
  27. });
  28. it('should work with undefined', function() {
  29. assert(!hasBinary(undefined));
  30. });
  31. it('should work with a complex object that contains undefined and no binary', function() {
  32. var ob = {
  33. x: ['a', 'b', 123],
  34. y: undefined,
  35. z: {a: 'x', b: 'y', c: 3, d: null},
  36. w: []
  37. };
  38. assert(!hasBinary(ob));
  39. });
  40. it('should work with a complex object that contains undefined and binary', function() {
  41. var ob = {
  42. x: ['a', 'b', 123],
  43. y: undefined,
  44. z: {a: 'x', b: 'y', c: 3, d: null},
  45. w: [],
  46. bin: new Buffer('xxx')
  47. };
  48. assert(hasBinary(ob));
  49. });
  50. it('should handle a very large json object with no binary', function(done) {
  51. this.timeout();
  52. fs.readFile(__dirname + '/fixtures/big.json', function(err, data) {
  53. if (err) {
  54. console.log(err);
  55. assert(false);
  56. }
  57. data = JSON.parse(data);
  58. assert(!hasBinary(data));
  59. done();
  60. });
  61. });
  62. it('should handle a very large json object with binary', function(done) {
  63. this.timeout();
  64. fs.readFile(__dirname + '/fixtures/big.json', function(err, data) {
  65. if (err) {
  66. console.log(err);
  67. assert(false);
  68. }
  69. var ob = JSON.parse(data);
  70. ob.bin = {bin: {bin: {bin: new Buffer('abc')}}};
  71. assert(hasBinary(ob));
  72. done();
  73. });
  74. });
  75. if (global.ArrayBuffer) {
  76. it('should work with an ArrayBuffer', function() {
  77. assert(hasBinary(new ArrayBuffer()));
  78. });
  79. }
  80. if (global.Blob) {
  81. it('should work with a Blob', function() {
  82. assert(hasBinary(new Blob()));
  83. });
  84. }
  85. it('should print the test time', function() {
  86. var end = new Date();
  87. var diff = end - start;
  88. console.log('\ntest time: ' + diff + ' ms');
  89. });
  90. });