get-display-type.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var util = require('util');
  2. var assert = require('assert');
  3. var _ = require('@sailshq/lodash');
  4. var rttc = require('../');
  5. describe('.getDisplayType()', function() {
  6. it('should always return a string', function() {
  7. assert.strictEqual(typeof rttc.getDisplayType('foo'), 'string');
  8. assert.strictEqual(typeof rttc.getDisplayType(''), 'string');
  9. assert.strictEqual(typeof rttc.getDisplayType(2323), 'string');
  10. assert.strictEqual(typeof rttc.getDisplayType(true), 'string');
  11. assert.strictEqual(typeof rttc.getDisplayType(null), 'string');
  12. assert.strictEqual(typeof rttc.getDisplayType(Infinity), 'string');
  13. assert.strictEqual(typeof rttc.getDisplayType(-Infinity), 'string');
  14. assert.strictEqual(typeof rttc.getDisplayType(NaN), 'string');
  15. assert.strictEqual(typeof rttc.getDisplayType(new Error('wat')), 'string');
  16. assert.strictEqual(typeof rttc.getDisplayType(new Buffer('stuff')), 'string');
  17. assert.strictEqual(typeof rttc.getDisplayType(new Date('stuff')), 'string');
  18. assert.strictEqual(typeof rttc.getDisplayType(new Error('stuff')), 'string');
  19. assert.strictEqual(typeof rttc.getDisplayType(new RegExp('stuff')), 'string');
  20. assert.strictEqual(typeof rttc.getDisplayType(function foo(a,b){}), 'string');
  21. assert.strictEqual(typeof rttc.getDisplayType({x:'foo',z: [{a:4}]}), 'string');
  22. assert.strictEqual(typeof rttc.getDisplayType([1,2,3]), 'string');
  23. assert.strictEqual(typeof rttc.getDisplayType(undefined), 'string');
  24. });
  25. it('should recognize special rttc exemplar syntax', function (){
  26. assert.strictEqual(rttc.getDisplayType('*'), 'json');
  27. assert.strictEqual(rttc.getDisplayType('==='), 'ref');
  28. assert.strictEqual(rttc.getDisplayType('->'), 'lamda');
  29. assert.strictEqual(rttc.getDisplayType('<-'), 'lamda');
  30. });
  31. it('should recognize strings', function (){
  32. assert.strictEqual(rttc.getDisplayType(''), 'string');
  33. assert.strictEqual(rttc.getDisplayType('stuff'), 'string');
  34. });
  35. it('should recognize invalid numbers', function (){
  36. assert.strictEqual(rttc.getDisplayType(Infinity), 'invalid number');
  37. assert.strictEqual(rttc.getDisplayType(-Infinity), 'invalid number');
  38. assert.strictEqual(rttc.getDisplayType(NaN), 'invalid number');
  39. });
  40. it('should recognize valid numbers', function (){
  41. assert.strictEqual(rttc.getDisplayType(3.239), 'number');
  42. assert.strictEqual(rttc.getDisplayType(-235235.3), 'number');
  43. assert.strictEqual(rttc.getDisplayType(2353), 'number');
  44. assert.strictEqual(rttc.getDisplayType(0), 'number');
  45. assert.strictEqual(rttc.getDisplayType(+0), 'number');
  46. assert.strictEqual(rttc.getDisplayType(-0), 'number');
  47. });
  48. it('should recognize booleans', function (){
  49. assert.strictEqual(rttc.getDisplayType(false), 'boolean');
  50. assert.strictEqual(rttc.getDisplayType(true), 'boolean');
  51. });
  52. it('should recognize null', function (){
  53. assert.strictEqual(rttc.getDisplayType(null), 'null');
  54. });
  55. it('should recognize undefined', function (){
  56. assert.strictEqual(rttc.getDisplayType(undefined), 'undefined');
  57. });
  58. it('should recognize Dates', function (){
  59. assert.strictEqual(rttc.getDisplayType(new Date()), 'Date');
  60. });
  61. it('should recognize RegExps', function (){
  62. assert.strictEqual(rttc.getDisplayType(new RegExp('stuff')), 'RegExp');
  63. assert.strictEqual(rttc.getDisplayType(/foo/ig), 'RegExp');
  64. });
  65. it('should recognize Errors', function (){
  66. assert.strictEqual(rttc.getDisplayType(new Error()), 'Error');
  67. });
  68. it('should recognize functions (including constructors and functions w/ properties)', function (){
  69. assert.strictEqual(rttc.getDisplayType(function (){}), 'function');
  70. assert.strictEqual(rttc.getDisplayType(function someFn(x,y){return x+y;}), 'function');
  71. assert.strictEqual(rttc.getDisplayType(new Function('return "stuff";')), 'function');
  72. assert.strictEqual(rttc.getDisplayType(Error), 'function');
  73. assert.strictEqual(rttc.getDisplayType(Date), 'function');
  74. assert.strictEqual(rttc.getDisplayType(RegExp), 'function');
  75. assert.strictEqual(rttc.getDisplayType(Array), 'function');
  76. assert.strictEqual(rttc.getDisplayType(Object), 'function');
  77. assert.strictEqual(rttc.getDisplayType((function(){
  78. var someFnWithExcitingSecrets = function (){};
  79. someFnWithExcitingSecrets.foo = 'bar';
  80. someFnWithExcitingSecrets.bar = someFnWithExcitingSecrets.foo;
  81. someFnWithExcitingSecrets.baz = {bar: someFnWithExcitingSecrets.bar};
  82. return someFnWithExcitingSecrets;
  83. })()), 'function');
  84. });
  85. it('should recognize arrays', function (){
  86. assert.strictEqual(rttc.getDisplayType([]), 'array');
  87. assert.strictEqual(rttc.getDisplayType(new Array()), 'array');
  88. });
  89. it('should recognize dictionaries', function (){
  90. assert.strictEqual(rttc.getDisplayType({}), 'dictionary');
  91. assert.strictEqual(rttc.getDisplayType({a:[]}), 'dictionary');
  92. assert.strictEqual(rttc.getDisplayType({a:{x:3}}), 'dictionary');
  93. assert.strictEqual(rttc.getDisplayType(new Object()), 'dictionary');
  94. });
  95. it('should recognize Streams', function (){
  96. assert.strictEqual(rttc.getDisplayType(new (require('stream').Readable)()), 'Readable');
  97. assert.strictEqual(rttc.getDisplayType(new (require('stream').Stream)()), 'Stream');
  98. assert.strictEqual(rttc.getDisplayType(new (require('stream').Writable)()), 'Writable');
  99. });
  100. it('should recognize Buffers', function (){
  101. assert.strictEqual(rttc.getDisplayType(new Buffer('asdf')), 'Buffer');
  102. });
  103. });