reify.test.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. var util = require('util');
  2. var assert = require('assert');
  3. var _ = require('@sailshq/lodash');
  4. var rttc = require('../');
  5. describe('.reify()', function() {
  6. // TODO: test that this doesn't go nuts given circular objects
  7. // (they should never be circular because they are type schemas, but still, for everyone's sanity)
  8. it('should leave "string" alone', function() {
  9. assert.deepEqual( 'string', rttc.reify('string') );
  10. });
  11. it('should leave "number" alone', function() {
  12. assert.deepEqual( 'number', rttc.reify('number') );
  13. });
  14. it('should leave "boolean" alone', function() {
  15. assert.deepEqual( 'boolean', rttc.reify('boolean') );
  16. });
  17. it('should leave "lamda" alone', function() {
  18. assert.deepEqual( 'lamda', rttc.reify('lamda') );
  19. });
  20. it('should leave faceted dictionaries alone', function() {
  21. assert.deepEqual( {x: 'string'}, rttc.reify( {x: 'string'} ) );
  22. assert.deepEqual( {y:'number'}, rttc.reify( {y:'number'} ) );
  23. assert.deepEqual( {z: 'boolean'}, rttc.reify( {z: 'boolean'} ) );
  24. assert.deepEqual( {l: 'lamda'}, rttc.reify( {l: 'lamda'} ) );
  25. assert.deepEqual( {foo:'string', bar: 'boolean', baz: 'number'}, rttc.reify( {foo:'string', bar: 'boolean', baz: 'number'} ) );
  26. assert.deepEqual( {x: ['string'], y: ['number'], z: ['boolean'], l:{a:{m:{d:{a:'lamda'}}}} } , rttc.reify( {x: ['string'], y: ['number'], z: ['boolean'], l:{a:{m:{d:{a:'lamda'}}}} } ) );
  27. });
  28. it('should leave patterned arrays alone', function() {
  29. assert.deepEqual( ['string'] , rttc.reify( ['string'] ) );
  30. assert.deepEqual( ['number'] , rttc.reify( ['number'] ) );
  31. assert.deepEqual( ['boolean'] , rttc.reify( ['boolean'] ) );
  32. assert.deepEqual( ['lamda'] , rttc.reify( ['lamda'] ) );
  33. assert.deepEqual( [{foo:'string', bar: 'boolean', baz: 'number'}] , rttc.reify( [{foo:'string', bar: 'boolean', baz: 'number'}] ) );
  34. assert.deepEqual( [['string']] , rttc.reify( [['string']] ) );
  35. assert.deepEqual( [['number']] , rttc.reify( [['number']] ) );
  36. assert.deepEqual( [['boolean']] , rttc.reify( [['boolean']] ) );
  37. assert.deepEqual( [['lamda']] , rttc.reify( [['lamda']] ) );
  38. assert.deepEqual( [[{foo:'string', bar: 'boolean', baz: 'number'}]] , rttc.reify( [[{foo:'string', bar: 'boolean', baz: 'number'}]] ) );
  39. });
  40. it('should collapse generic dictionaries', function() {
  41. assert( _.isUndefined( rttc.reify({}) ) );
  42. });
  43. it('should collapse generic arrays', function() {
  44. assert( _.isUndefined( rttc.reify([]) ) );
  45. });
  46. it('should collapse "json"', function() {
  47. assert( _.isUndefined( rttc.reify('json') ) );
  48. });
  49. it('should collapse "ref"', function() {
  50. assert( _.isUndefined( rttc.reify('ref') ) );
  51. });
  52. it('should collapse and strip nested generics', function() {
  53. var exhibitA = {
  54. foo: 'json',
  55. bar: {},
  56. baz: ['json'],
  57. bang: [{
  58. a: {
  59. x: 'ref'
  60. },
  61. b: 'lamda'
  62. }],
  63. beep: ['string'],
  64. boop: {
  65. hello: 'string',
  66. world: 'json'
  67. }
  68. };
  69. var reified = {
  70. bang: [{
  71. b: 'lamda'
  72. }],
  73. beep: ['string'],
  74. boop: {
  75. hello: 'string'
  76. }
  77. };
  78. assert.deepEqual( reified, rttc.reify(exhibitA) );
  79. });
  80. });