stringify-human.test.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. var util = require('util');
  2. var assert = require('assert');
  3. var _ = require('@sailshq/lodash');
  4. var rttc = require('../');
  5. describe('.stringifyHuman()', function() {
  6. it('should coerce things to match the type schema, but as strings', function() {
  7. assert.strictEqual( rttc.stringifyHuman('foo', 'string'), 'foo');
  8. assert.strictEqual( rttc.stringifyHuman('', 'string'), '');
  9. assert.strictEqual( rttc.stringifyHuman('4', 'string'), '4');
  10. assert.strictEqual( rttc.stringifyHuman('-99', 'string'), '-99');
  11. assert.strictEqual( rttc.stringifyHuman('Infinity', 'string'), 'Infinity');
  12. assert.strictEqual( rttc.stringifyHuman('-Infinity', 'string'), '-Infinity');
  13. assert.strictEqual( rttc.stringifyHuman('NaN', 'string'), 'NaN');
  14. assert.strictEqual( rttc.stringifyHuman('true', 'string'), 'true');
  15. assert.strictEqual( rttc.stringifyHuman('false', 'string'), 'false');
  16. assert.strictEqual( rttc.stringifyHuman('foo', 'json'), '"foo"');
  17. assert.strictEqual( rttc.stringifyHuman('', 'json'), '""');
  18. assert.strictEqual( rttc.stringifyHuman('4', 'json'), '"4"');
  19. assert.strictEqual( rttc.stringifyHuman('-99', 'json'), '"-99"');
  20. assert.strictEqual( rttc.stringifyHuman('true', 'json'), '"true"');
  21. assert.strictEqual( rttc.stringifyHuman('false', 'json'), '"false"');
  22. assert.strictEqual( rttc.stringifyHuman('null', 'json'), '"null"');
  23. assert.strictEqual( rttc.stringifyHuman('foo', 'ref'), '"foo"');
  24. assert.strictEqual( rttc.stringifyHuman('', 'ref'), '""');
  25. assert.strictEqual( rttc.stringifyHuman('4', 'ref'), '"4"');
  26. assert.strictEqual( rttc.stringifyHuman('-99', 'ref'), '"-99"');
  27. assert.strictEqual( rttc.stringifyHuman('true', 'ref'), '"true"');
  28. assert.strictEqual( rttc.stringifyHuman('false', 'ref'), '"false"');
  29. assert.strictEqual( rttc.stringifyHuman('null', 'ref'), '"null"');
  30. assert.strictEqual( rttc.stringifyHuman(4, 'number'), '4');
  31. assert.strictEqual( rttc.stringifyHuman(10.345, 'number'), '10.345');
  32. assert.strictEqual( rttc.stringifyHuman(-99, 'number'), '-99');
  33. assert.strictEqual( rttc.stringifyHuman(Math.PI, 'number'), '3.141592653589793');
  34. assert.strictEqual( rttc.stringifyHuman(4, 'json'), '4');
  35. assert.strictEqual( rttc.stringifyHuman(10.345, 'json'), '10.345');
  36. assert.strictEqual( rttc.stringifyHuman(-99, 'json'), '-99');
  37. assert.strictEqual( rttc.stringifyHuman(Math.PI, 'json'), '3.141592653589793');
  38. assert.strictEqual( rttc.stringifyHuman(4, 'ref'), '4');
  39. assert.strictEqual( rttc.stringifyHuman(10.345, 'ref'), '10.345');
  40. assert.strictEqual( rttc.stringifyHuman(-99, 'ref'), '-99');
  41. assert.strictEqual( rttc.stringifyHuman(Math.PI, 'ref'), '3.141592653589793');
  42. assert.strictEqual( rttc.stringifyHuman(false, 'boolean'), 'false');
  43. assert.strictEqual( rttc.stringifyHuman(true, 'boolean'), 'true');
  44. assert.strictEqual( rttc.stringifyHuman(true, 'json'), 'true');
  45. assert.strictEqual( rttc.stringifyHuman(false, 'json'), 'false');
  46. assert.strictEqual( rttc.stringifyHuman(true, 'ref'), 'true');
  47. assert.strictEqual( rttc.stringifyHuman(false, 'ref'), 'false');
  48. assert.strictEqual( rttc.stringifyHuman({}, {}), '{}');
  49. assert.strictEqual( rttc.stringifyHuman({}, 'json'), '{}');
  50. assert.strictEqual( rttc.stringifyHuman({}, 'ref'), '{}');
  51. assert.strictEqual( rttc.stringifyHuman([], []), '[]');
  52. assert.strictEqual( rttc.stringifyHuman([], 'json'), '[]');
  53. assert.strictEqual( rttc.stringifyHuman([], 'ref'), '[]');
  54. assert.strictEqual( rttc.stringifyHuman(function asdf(){}, 'lamda'), 'function asdf(){}');
  55. assert.strictEqual( rttc.stringifyHuman(null, 'json'), 'null');
  56. assert.strictEqual( rttc.stringifyHuman(null, 'ref'), 'null');
  57. });
  58. it('should be reversible using .parseHuman()', function() {
  59. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('foo', 'string'), 'string', true), 'foo', 'string' ) );
  60. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('', 'string'), 'string', true), '', 'string' ) );
  61. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('4', 'string'), 'string', true), '4', 'string' ) );
  62. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('-99', 'string'), 'string', true), '-99', 'string' ) );
  63. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('Infinity', 'string'), 'string', true), 'Infinity', 'string' ) );
  64. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('-Infinity', 'string'), 'string', true), '-Infinity', 'string' ) );
  65. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('NaN', 'string'), 'string', true), 'NaN', 'string' ) );
  66. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('true', 'string'), 'string', true), 'true', 'string' ) );
  67. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('false', 'string'), 'string', true), 'false', 'string' ) );
  68. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('foo', 'json'), 'json', true), 'foo', 'json' ) );
  69. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('', 'json'), 'json', true), '', 'json' ) );
  70. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('4', 'json'), 'json', true), '4', 'json' ) );
  71. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('-99', 'json'), 'json', true), '-99', 'json' ) );
  72. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('true', 'json'), 'json', true), 'true', 'json' ) );
  73. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('false', 'json'), 'json', true), 'false', 'json' ) );
  74. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('null', 'json'), 'json', true), 'null', 'json' ) );
  75. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('foo', 'ref'), 'ref', true), 'foo', 'ref' ) );
  76. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('', 'ref'), 'ref', true), '', 'ref' ) );
  77. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('4', 'ref'), 'ref', true), '4', 'ref' ) );
  78. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('-99', 'ref'), 'ref', true), '-99', 'ref' ) );
  79. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('true', 'ref'), 'ref', true), 'true', 'ref' ) );
  80. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('false', 'ref'), 'ref', true), 'false', 'ref' ) );
  81. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman('null', 'ref'), 'ref', true), 'null', 'ref' ) );
  82. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(4, 'number'), 'number', true), 4, 'number' ) );
  83. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(10.345, 'number'), 'number', true), 10.345, 'number' ) );
  84. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(-99, 'number'), 'number', true), -99, 'number' ) );
  85. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(Math.PI, 'number'), 'number', true), Math.PI, 'number' ) );
  86. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(4, 'json'), 'json', true), 4, 'json' ) );
  87. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(10.345, 'json'), 'json', true), 10.345, 'json' ) );
  88. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(-99, 'json'), 'json', true), -99, 'json' ) );
  89. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(Math.PI, 'json'), 'json', true), Math.PI, 'json' ) );
  90. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(4, 'ref'), 'ref', true), 4, 'ref' ) );
  91. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(10.345, 'ref'), 'ref', true), 10.345, 'ref' ) );
  92. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(-99, 'ref'), 'ref', true), -99, 'ref' ) );
  93. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(Math.PI, 'ref'), 'ref', true), Math.PI, 'ref' ) );
  94. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(false, 'boolean'), 'boolean', true), false, 'boolean' ) );
  95. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(true, 'boolean'), 'boolean', true), true, 'boolean' ) );
  96. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(true, 'json'), 'json', true), true, 'json' ) );
  97. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(false, 'json'), 'json', true), false, 'json' ) );
  98. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(true, 'ref'), 'ref', true), true, 'ref' ) );
  99. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(false, 'ref'), 'ref', true), false, 'ref' ) );
  100. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman({}, {}), {}, true), {}, {} ) );
  101. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman({}, 'json'), 'json', true), {}, 'json' ) );
  102. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman({}, 'ref'), 'ref', true), {}, 'ref' ) );
  103. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman([], []), [], true), [], [] ) );
  104. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman([], 'json'), 'json', true), [], 'json' ) );
  105. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman([], 'ref'), 'ref', true), [], 'ref' ) );
  106. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(function asdf(){}, 'lamda'), 'lamda', true), function asdf(){}, 'lamda' ) );
  107. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(null, 'json'), 'json', true), null, 'json' ) );
  108. assert( rttc.isEqual( rttc.parseHuman(rttc.stringifyHuman(null, 'ref'), 'ref', true), null, 'ref' ) );
  109. });
  110. it('should add quotes to strings when type schema is "json"', function() {
  111. assert.strictEqual(rttc.stringifyHuman('stuff', 'json'), '"stuff"' );
  112. });
  113. it('should add NOT quotes to numbers, booleans, dictionaries, arrays, or `null` when type schema is "json"', function() {
  114. assert.strictEqual(rttc.stringifyHuman(3, 'json'), '3' );
  115. assert.strictEqual(rttc.stringifyHuman(true, 'json'), 'true' );
  116. assert.strictEqual(rttc.stringifyHuman({}, 'json'), '{}' );
  117. assert.strictEqual(rttc.stringifyHuman([], 'json'), '[]' );
  118. assert.strictEqual(rttc.stringifyHuman(null, 'json'), 'null' );
  119. });
  120. it('should tolerate `null` for "json" type schemas', function() {
  121. assert.strictEqual( rttc.stringifyHuman(null, 'json'), 'null');
  122. });
  123. it('should tolerate `null` for "ref" type schemas', function() {
  124. assert.strictEqual( rttc.stringifyHuman(null, 'ref'), 'null' );
  125. });
  126. it('should tolerate `null` for type schemas w/ nested "json" types', function() {
  127. assert.strictEqual( rttc.stringifyHuman([null], ['json']), '[null]' );
  128. assert.strictEqual( rttc.stringifyHuman({x:null}, {x:'json'}), '{"x":null}' );
  129. });
  130. it('should tolerate `null` for type schemas w/ nested "ref" types', function() {
  131. assert.strictEqual( rttc.stringifyHuman([null], ['ref']), '[null]');
  132. assert.strictEqual( rttc.stringifyHuman({x:null}, {x:'ref'}), '{"x":null}' );
  133. });
  134. it('should NOT tolerate `null` for {} or [] type schemas', function() {
  135. assert.throws(function (){
  136. rttc.stringifyHuman(null, {});
  137. });
  138. assert.throws(function (){
  139. rttc.stringifyHuman(null, []);
  140. });
  141. assert.throws(function (){
  142. rttc.stringifyHuman([null], [{}]);
  143. });
  144. assert.throws(function (){
  145. rttc.stringifyHuman([null], [[]]);
  146. });
  147. assert.throws(function (){
  148. rttc.stringifyHuman({x:null}, {x:{}});
  149. });
  150. assert.throws(function (){
  151. rttc.stringifyHuman({x:null}, {x:[]});
  152. });
  153. });
  154. it('should dehydrate functions (if we have a lamda type schema)', function() {
  155. assert(
  156. rttc.isEqual(rttc.stringifyHuman(function foo (){}, 'lamda'), 'function foo(){}', 'lamda')
  157. );
  158. });
  159. it('should dehydrate nested functions (if we have a type schema w/ nested lamdas in the appropriate places)', function() {
  160. assert.equal(
  161. rttc.stringifyHuman({x:function foo (){}}, {x:'lamda'}),
  162. '{"x":"function foo(){}"}'
  163. );
  164. assert.equal(
  165. rttc.stringifyHuman({x:{y:function foo (){}}}, {x:{y:'lamda'}}),
  166. '{"x":{"y":"function foo(){}"}}'
  167. );
  168. assert.equal(
  169. rttc.stringifyHuman([{x:{y:function foo (){}}}], [{x:{y:'lamda'}}]),
  170. '[{"x":{"y":"function foo(){}"}}]'
  171. );
  172. assert.equal(
  173. rttc.stringifyHuman([function foo (){}], ['lamda']),
  174. '["function foo(){}"]'
  175. );
  176. });
  177. describe('edge-cases', function (){
  178. it('should refuse to work if not provided a type schema', function() {
  179. assert.throws(function (){
  180. rttc.stringifyHuman(4);
  181. });
  182. });
  183. describe('stringification cannot be safely reversed', function(){
  184. // Against `json`
  185. it('should throw when given a function against type: `ref`',function(){
  186. assert.throws(function (){
  187. rttc.stringifyHuman(function asdf(){}, 'ref');
  188. });
  189. });
  190. it('should throw when given an Error against type: `ref`',function(){
  191. assert.throws(function (){
  192. rttc.stringifyHuman(new Error('foo'), 'ref');
  193. });
  194. });
  195. it('should throw when given a Date against type: `ref`',function(){
  196. assert.throws(function (){
  197. rttc.stringifyHuman(new Date('foo'), 'ref');
  198. });
  199. });
  200. it('should throw when given a RegExp against type: `ref`',function(){
  201. assert.throws(function (){
  202. rttc.stringifyHuman(/sadg/g, 'ref');
  203. });
  204. });
  205. it('should throw when given a Stream against type: `ref`',function(){
  206. assert.throws(function (){
  207. rttc.stringifyHuman(new (require('stream').Readable)(), 'ref');
  208. });
  209. assert.throws(function (){
  210. rttc.stringifyHuman(new (require('stream').Stream)(), 'ref');
  211. });
  212. });
  213. it('should throw when given a Buffer against type: `ref`',function(){
  214. assert.throws(function (){
  215. rttc.stringifyHuman(new Buffer(), 'ref');
  216. });
  217. });
  218. it('should throw when given Infinity, -Infinity, or NaN against type: `ref`',function(){
  219. assert.throws(function (){
  220. rttc.stringifyHuman(Infinity, 'ref');
  221. });
  222. assert.throws(function (){
  223. rttc.stringifyHuman(-Infinity, 'ref');
  224. });
  225. assert.throws(function (){
  226. rttc.stringifyHuman(NaN, 'ref');
  227. });
  228. });
  229. // Against `json`
  230. it('should throw when given a function against type: `json`',function(){
  231. assert.throws(function (){
  232. rttc.stringifyHuman(function asdf(){}, 'json');
  233. });
  234. });
  235. it('should throw when given an Error against type: `json`',function(){
  236. assert.throws(function (){
  237. rttc.stringifyHuman(new Error('foo'), 'json');
  238. });
  239. });
  240. it('should throw when given a Date against type: `json`',function(){
  241. assert.throws(function (){
  242. rttc.stringifyHuman(new Date('foo'), 'json');
  243. });
  244. });
  245. it('should throw when given a RegExp against type: `json`',function(){
  246. assert.throws(function (){
  247. rttc.stringifyHuman(/sadg/g, 'json');
  248. });
  249. });
  250. it('should throw when given a Stream against type: `json`',function(){
  251. assert.throws(function (){
  252. rttc.stringifyHuman(new (require('stream').Readable)(), 'json');
  253. });
  254. assert.throws(function (){
  255. rttc.stringifyHuman(new (require('stream').Stream)(), 'json');
  256. });
  257. });
  258. it('should throw when given a Buffer against type: `json`',function(){
  259. assert.throws(function (){
  260. rttc.stringifyHuman(new Buffer(), 'json');
  261. });
  262. });
  263. it('should throw when given Infinity, -Infinity, or NaN against type: `json`',function(){
  264. assert.throws(function (){
  265. rttc.stringifyHuman(Infinity, 'json');
  266. });
  267. assert.throws(function (){
  268. rttc.stringifyHuman(-Infinity, 'json');
  269. });
  270. assert.throws(function (){
  271. rttc.stringifyHuman(NaN, 'json');
  272. });
  273. });
  274. });
  275. it('should refuse to work if value does not strictly validate against type schema', function() {
  276. assert.throws(function (){
  277. rttc.stringifyHuman('whatever', 'lamda');
  278. });
  279. assert.throws(function (){
  280. rttc.stringifyHuman('whatever', 'boolean');
  281. });
  282. assert.throws(function (){
  283. rttc.stringifyHuman(4, 'boolean');
  284. });
  285. assert.throws(function (){
  286. rttc.stringifyHuman(4, 'string');
  287. });
  288. assert.throws(function (){
  289. rttc.stringifyHuman('4', 'number');
  290. });
  291. assert.throws(function(){
  292. rttc.stringifyHuman(function asdf(){}, 'json');
  293. });
  294. });
  295. });
  296. });