parse.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var util = require('util');
  2. var assert = require('assert');
  3. var _ = require('@sailshq/lodash');
  4. var rttc = require('../');
  5. describe('.parse()', function() {
  6. it('should accurately recreate encoded strings', function() {
  7. checkSerialization('stuff');
  8. });
  9. it('should accurately recreate encoded numbers', function (){
  10. checkSerialization(8232);
  11. });
  12. it('should accurately recreate encoded booleans', function (){
  13. checkSerialization(false);
  14. });
  15. it('should accurately recreate encoded dictionaries', function (){
  16. checkSerialization({});
  17. checkSerialization({foo:'bar'});
  18. checkSerialization({foo:{bar:'baz'}});
  19. checkSerialization({foo:{bar:['baz']}});
  20. checkSerialization({foo:[{bar:['baz']}]});
  21. });
  22. it('should accurately recreate encoded arrays', function (){
  23. checkSerialization([]);
  24. checkSerialization(['Arya Stark', 'Jon Snow']);
  25. checkSerialization([2932,138,11,424]);
  26. checkSerialization([true, true, false]);
  27. checkSerialization([true, true, false, false]);
  28. checkSerialization([{species: 'Hopidor maxim', weight: 23821394, isAvailable: true}]);
  29. checkSerialization([['baz']]);
  30. checkSerialization([{foo:[{bar:['baz']}]}, {foo:[{bar:['brains']}]}], [{foo: [{bar: ['string']}]}], true);
  31. checkSerialization([{foo:[{bar:[function things(){}]}]}, {foo:[{bar:[function stuff(){}]}]}], [{foo: [{bar: ['lamda']}]}], true);
  32. });
  33. it('should throw when given a non-string', function (){
  34. assert.throws(function (){ rttc.parse(null); });
  35. assert.throws(function (){ rttc.parse(undefined); });
  36. assert.throws(function (){ rttc.parse(29); });
  37. assert.throws(function (){ rttc.parse(true); });
  38. assert.throws(function (){ rttc.parse(false); });
  39. assert.throws(function (){ rttc.parse({}); });
  40. assert.throws(function (){ rttc.parse([]); });
  41. assert.throws(function (){ rttc.parse(['a']); });
  42. });
  43. it('should throw given empty string (because it cannot be parsed as JSON)', function (){
  44. assert.throws(function (){ rttc.parse(''); });
  45. });
  46. it('should accurately recreate encoded functions if a "lamda" `typeSchema` was provided and `unsafeMode` was enabled', function (){
  47. var original = function someFn(x,y,z){ return x + y - z + x + Math.pow(y,z); };
  48. var encoded = rttc.stringify(original);
  49. var decoded = rttc.parse(encoded, 'lamda', true);
  50. assert(_.isFunction(decoded));
  51. assert.strictEqual(original(4,5,6), decoded(4,5,6));
  52. });
  53. it('should NOT accurately recreate encoded functions in the general case', function (){
  54. var original = function someFn(x,y,z){ return x + y - z + x + Math.pow(y,z); };
  55. var decoded = rttc.parse(rttc.stringify(original));
  56. assert(!_.isFunction(decoded));
  57. assert(_.isString(decoded));
  58. assert.strictEqual(decoded, original.toString());
  59. });
  60. it('should decode an Error to a string, and that string should be the `.stack` property of the original instance', function (){
  61. var original = new Error('some error');
  62. var decoded = rttc.parse(rttc.stringify(original));
  63. assert(!_.isError(decoded));
  64. assert(_.isString(decoded));
  65. assert.strictEqual(decoded, original.stack);
  66. });
  67. it('should decode a Date as an ISO 1608 timestamp', function (){
  68. var original = new Date('August 11, 1988');
  69. var decoded = rttc.parse(rttc.stringify(original));
  70. assert(!_.isDate(decoded));
  71. assert(_.isString(decoded));
  72. assert.strictEqual(decoded, original.toJSON());
  73. });
  74. it('should decode a RegExp as a .toString() version of the original', function (){
  75. var original = /some regexp/gi;
  76. var decoded = rttc.parse(rttc.stringify(original));
  77. assert(!_.isRegExp(decoded));
  78. assert(_.isString(decoded));
  79. assert.strictEqual(decoded, original.toString());
  80. });
  81. it('should properly decode the characters from "I Love Lucy"', function (){
  82. var encoded = '[{"name":"Lucy"},{"name":"Ricky"},{"name":"Louise"},{"name":"Ethyl"}]';
  83. var example = [{}];
  84. var decoded = rttc.parse(encoded, rttc.infer(example), true);
  85. assert.deepEqual([{name: 'Lucy'}, {name:'Ricky'},{name:'Louise'},{name:'Ethyl'}], decoded);
  86. });
  87. });
  88. /**
  89. * helper asserting that encoding and then decoding the original value results in an equivalent value.
  90. * @param {[type]} original [description]
  91. * @return {[type]} [description]
  92. */
  93. function checkSerialization(original) {
  94. var encoded = rttc.stringify(original);
  95. var decoded = rttc.parse(encoded);
  96. assert.deepEqual(decoded, original);
  97. }