type-info.test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. var util = require('util');
  2. var assert = require('assert');
  3. var _ = require('@sailshq/lodash');
  4. var rttc = require('../');
  5. describe('.typeInfo()', function() {
  6. describe('when given a valid type schema', function (){
  7. it('should always return an object', function() {
  8. assert.strictEqual(typeof rttc.typeInfo(rttc.infer('foo')), 'object');
  9. assert.strictEqual(typeof rttc.typeInfo(rttc.infer('')), 'object');
  10. assert.strictEqual(typeof rttc.typeInfo(rttc.infer(2323)), 'object');
  11. assert.strictEqual(typeof rttc.typeInfo(rttc.infer(true)), 'object');
  12. assert.strictEqual(typeof rttc.typeInfo(rttc.infer({x:'foo',z: [{a:4}]})), 'object');
  13. assert.strictEqual(typeof rttc.typeInfo(rttc.infer([1,2,3])), 'object');
  14. });
  15. /**
  16. * Helper fn that asserts that a specific typeSchema
  17. * @param {[type]} typeSchema [description]
  18. * @param {[type]} expectedTypeId [description]
  19. * @return {[type]} [description]
  20. */
  21. var checkResultingTypeDef = function(typeSchema, expectedTypeId) {
  22. assert.equal(typeof rttc.typeInfo(typeSchema),'object');
  23. assert.strictEqual(rttc.typeInfo(typeSchema).id, expectedTypeId);
  24. };
  25. it('should recognize facets and patterns', function() {
  26. checkResultingTypeDef(rttc.infer({a:3}), 'dictionary');
  27. checkResultingTypeDef(rttc.infer({a:'asdg', b:23, c: true}), 'dictionary');
  28. checkResultingTypeDef(rttc.infer({a:'asdg', b:23, c: {foo: 'bar'}}), 'dictionary');
  29. checkResultingTypeDef(rttc.infer([3]), 'array');
  30. checkResultingTypeDef(rttc.infer([3,4]), 'array');
  31. checkResultingTypeDef(rttc.infer(['a']), 'array');
  32. checkResultingTypeDef(rttc.infer(['a','b']), 'array');
  33. checkResultingTypeDef(rttc.infer([true]), 'array');
  34. checkResultingTypeDef(rttc.infer([false]), 'array');
  35. checkResultingTypeDef(rttc.infer([false, false, true, false]), 'array');
  36. checkResultingTypeDef(rttc.infer([false, null, 'a', -45.3, Infinity]), 'array');
  37. checkResultingTypeDef(rttc.infer({a:'asdg', b:23, c: [{foo: ['bar']}]}), 'dictionary');
  38. checkResultingTypeDef(rttc.infer([{a:'asdg', b:23, c: [true]}]), 'array');
  39. checkResultingTypeDef(rttc.infer(['asdg', {b:23}]), 'array');
  40. checkResultingTypeDef(rttc.infer([{a:'asdg', b:23, c: [{foo: ['bar']}]}]), 'array');
  41. });
  42. it('should recognize schemas with generic dictionaries', function() {
  43. checkResultingTypeDef(rttc.infer({}), 'dictionary');
  44. checkResultingTypeDef(rttc.infer({a:3}), 'dictionary');
  45. checkResultingTypeDef(rttc.infer({a:'asdg'}), 'dictionary');
  46. checkResultingTypeDef(rttc.infer({a:true}), 'dictionary');
  47. });
  48. it('should recognize schemas with generic arrays', function() {
  49. checkResultingTypeDef(rttc.infer([]), 'array');
  50. checkResultingTypeDef(rttc.infer([[]]), 'array');
  51. checkResultingTypeDef(rttc.infer([[[]]]), 'array');
  52. checkResultingTypeDef(rttc.infer([[[['a', 3, true]]]]), 'array');
  53. checkResultingTypeDef(rttc.infer([{}]), 'array');
  54. checkResultingTypeDef(rttc.infer([{a:[]}]), 'array');
  55. checkResultingTypeDef(rttc.infer([{a:[[[[]]]], b: [[]] }]), 'array');
  56. });
  57. });
  58. describe('when given an invalid type schema', function (){
  59. it('should throw', function (){
  60. assert.throws(function (){
  61. typeof rttc.typeInfo('undefined');
  62. });
  63. assert.throws(function (){
  64. typeof rttc.typeInfo(undefined);
  65. });
  66. assert.throws(function (){
  67. typeof rttc.typeInfo(rttc.infer(null));
  68. });
  69. assert.throws(function (){
  70. typeof rttc.typeInfo(rttc.infer(null));
  71. });
  72. assert.throws(function (){
  73. typeof rttc.typeInfo(rttc.infer(Infinity));
  74. });
  75. assert.throws(function (){
  76. typeof rttc.typeInfo(rttc.infer(-Infinity));
  77. });
  78. assert.throws(function (){
  79. typeof rttc.typeInfo(rttc.infer(NaN));
  80. });
  81. assert.throws(function (){
  82. typeof rttc.typeInfo(rttc.infer(new Error('wat')));
  83. });
  84. assert.throws(function (){
  85. typeof rttc.typeInfo(rttc.infer(new Buffer('stuff')));
  86. });
  87. assert.throws(function (){
  88. typeof rttc.typeInfo(rttc.infer(new Date('stuff')));
  89. });
  90. assert.throws(function (){
  91. typeof rttc.typeInfo(rttc.infer(new Error('stuff')));
  92. });
  93. assert.throws(function (){
  94. typeof rttc.typeInfo(rttc.infer(new RegExp('stuff')));
  95. });
  96. assert.throws(function (){
  97. typeof rttc.typeInfo(rttc.infer(function foo(a,b){}));
  98. });
  99. });
  100. });
  101. describe('when explicitly given known rttc types', function (){
  102. it('should recognize "json" and return type definition', function (){
  103. assert.strictEqual(typeof rttc.typeInfo('json'), 'object');
  104. assert.strictEqual(rttc.typeInfo('json').id, 'json');
  105. });
  106. it('should recognize "ref" and return type definition', function (){
  107. assert.strictEqual(typeof rttc.typeInfo('ref'), 'object');
  108. assert.strictEqual(rttc.typeInfo('ref').id, 'ref');
  109. });
  110. it('should recognize "lamda" and return type definition', function (){
  111. assert.strictEqual(typeof rttc.typeInfo('lamda'), 'object');
  112. assert.strictEqual(rttc.typeInfo('lamda').id, 'lamda');
  113. });
  114. it('should recognize "dictionary" and return type definition', function (){
  115. assert.strictEqual(typeof rttc.typeInfo('dictionary'), 'object');
  116. assert.strictEqual(rttc.typeInfo('dictionary').id, 'dictionary');
  117. });
  118. it('should recognize "array" and return type definition', function (){
  119. assert.strictEqual(typeof rttc.typeInfo('array'), 'object');
  120. assert.strictEqual(rttc.typeInfo('array').id, 'array');
  121. });
  122. it('should recognize "boolean" and return type definition', function (){
  123. assert.strictEqual(typeof rttc.typeInfo('boolean'), 'object');
  124. assert.strictEqual(rttc.typeInfo('boolean').id, 'boolean');
  125. });
  126. it('should recognize "number" and return type definition', function (){
  127. assert.strictEqual(typeof rttc.typeInfo('number'), 'object');
  128. assert.strictEqual(rttc.typeInfo('number').id, 'number');
  129. });
  130. it('should recognize "string" and return type definition', function (){
  131. assert.strictEqual(typeof rttc.typeInfo('string'), 'object');
  132. assert.strictEqual(rttc.typeInfo('string').id, 'string');
  133. });
  134. });
  135. describe('when given unknown or invalid types', function (){
  136. it('should not recognize "blah blah"', function (){
  137. assert.throws(function (){
  138. var typeDef = rttc.typeInfo('blah blah');
  139. });
  140. });
  141. it('should not recognize arbitrary things which aren\'t strings', function (){
  142. assert.throws(function (){
  143. rttc.typeInfo(/weird stuff/gi);
  144. });
  145. assert.throws(function (){
  146. rttc.typeInfo(new Error('whee'));
  147. });
  148. });
  149. });
  150. describe('when given an example you\'d normally use for type inference', function (){
  151. it('should not recognize "*" and throw', function (){
  152. assert.throws(function (){
  153. var typeDef = rttc.typeInfo('*');
  154. });
  155. });
  156. it('should not recognize "===" and throw', function (){
  157. assert.throws(function (){
  158. var typeDef = rttc.typeInfo('===');
  159. });
  160. });
  161. it('should not recognize "->" and throw', function (){
  162. assert.throws(function (){
  163. var typeDef = rttc.typeInfo('->');
  164. });
  165. });
  166. });
  167. });