compile.test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. var assert = require('assert');
  2. var rttc = require('../');
  3. describe('.compile()', function() {
  4. // * | actual | util.inspect() | rttc.compile() |
  5. // * | ----------------------- | ----------------------------------------- | -------------------------------------|
  6. // * | a function | `[Function: foo]` | `'function foo (){}'` |
  7. // * | a Date | `Tue May 26 2015 20:05:37 GMT-0500 (CDT)` | `'2015-05-27T01:06:37.072Z'` |
  8. // * | a RegExp | `/foo/gi` | `'/foo/gi/'` |
  9. // * | an Error | `[Error]` | `'Error\n at repl:1:24\n...'` |
  10. // * | a deeply nested thing | `{ a: { b: { c: [Object] } } }` | `{ a: { b: { c: { d: {} } } } }` |
  11. // * | a circular thing | `{ y: { z: [Circular] } }` | `{ y: { z: '[Circular ~]' } }` |
  12. // * | undefined | `undefined` | `null` |
  13. // * | Readable (Node stream) | `{ _readableState: { highWaterMar..}}` | `null` |
  14. // * | Buffer (Node bytestring)| `<Buffer 61 62 63>` | `[ 97, 98, 99 ]` |
  15. it('should wrap strings in single quotes', function() {
  16. _assertCompiledResultIsCorrect({
  17. value: 'foo',
  18. expected: '\'foo\''
  19. });
  20. _assertCompiledResultIsCorrect({
  21. value: '"foo"',
  22. expected: '\'"foo"\''
  23. });
  24. _assertCompiledResultIsCorrect({
  25. value: '999999999',
  26. expected: '\'999999999\''
  27. });
  28. });
  29. it('should return string version of number', function() {
  30. _assertCompiledResultIsCorrect({
  31. value: 9999999999,
  32. expected: '9999999999'
  33. });
  34. });
  35. it('should return string version of boolean', function() {
  36. _assertCompiledResultIsCorrect({
  37. value: false,
  38. expected: 'false'
  39. });
  40. _assertCompiledResultIsCorrect({
  41. value: true,
  42. expected: 'true'
  43. });
  44. });
  45. it('should `.toString()` function BUT NOT wrap it in single quotes!!', function() {
  46. _assertCompiledResultIsCorrect({
  47. value: function foobar(x,y){ return x+y; },
  48. expected: 'function foobar(x,y){ return x+y; }'
  49. });
  50. });
  51. it('should remove any whitespace between function name and arguments declaration', function() {
  52. _assertCompiledResultIsCorrect({
  53. value: function foobar (x,y){ return x+y; },
  54. expected: 'function foobar(x,y){ return x+y; }'
  55. });
  56. });
  57. it('should get .stack property of Error and wrap it in single quotes', function() {
  58. var err = new Error('some passive aggressive message');
  59. err.stack = 'setting this stack property to something inane so that it\'s easy to compare, and so tests don\'t depend on file paths from the stack trace of my computer';
  60. _assertCompiledResultIsCorrect({
  61. value: err,
  62. expected: '\'setting this stack property to something inane so that it\\\'s easy to compare, and so tests don\\\'t depend on file paths from the stack trace of my computer\''
  63. });
  64. });
  65. it('should get timezone-agnostic ISO 6801 timestamp for Date and wrap it in single quotes', function() {
  66. _assertCompiledResultIsCorrect({
  67. value: new Date('November 5, 1605 GMT'),
  68. expected: '\'1605-11-05T00:00:00.000Z\''
  69. });
  70. });
  71. it('should call `.toString()` on RegExp, then wrap it in single quotes', function() {
  72. _assertCompiledResultIsCorrect({
  73. value: /waldo/gi,
  74. expected: '\'/waldo/gi\''
  75. });
  76. });
  77. it('should return string that looks like `null` for `undefined` and `null`', function() {
  78. _assertCompiledResultIsCorrect({
  79. value: undefined,
  80. expected: 'null'
  81. });
  82. _assertCompiledResultIsCorrect({
  83. value: null,
  84. expected: 'null'
  85. });
  86. });
  87. it('should return string that looks like `0` for weird values like Infinity, -Infinity, and NaN', function() {
  88. _assertCompiledResultIsCorrect({
  89. value: -Infinity,
  90. expected: '0'
  91. });
  92. _assertCompiledResultIsCorrect({
  93. value: Infinity,
  94. expected: '0'
  95. });
  96. _assertCompiledResultIsCorrect({
  97. value: NaN,
  98. expected: '0'
  99. });
  100. });
  101. it('should return string that looks like `null` for stream.Readable instances', function() {
  102. _assertCompiledResultIsCorrect({
  103. value: new (require('stream').Readable)(),
  104. expected: 'null'
  105. });
  106. });
  107. // TODO: make this work
  108. it.skip('should return string that looks like `null` for Buffer instances`', function() {
  109. _assertCompiledResultIsCorrect({
  110. value: new Buffer('alive with the glory of love'),
  111. expected: 'null'
  112. });
  113. });
  114. it('should return string that looks like dictionary for dictionary', function() {
  115. _assertCompiledResultIsCorrect({
  116. value: {},
  117. expected: '{}'
  118. });
  119. _assertCompiledResultIsCorrect({
  120. value: { a: 'b' },
  121. expected: '{ a: \'b\' }'
  122. });
  123. });
  124. it('should return string that looks like array for array', function() {
  125. _assertCompiledResultIsCorrect({
  126. value: [],
  127. expected: '[]'
  128. });
  129. _assertCompiledResultIsCorrect({
  130. value: [ 'a', 'b', 45 ],
  131. expected: '[ \'a\', \'b\', 45 ]'
  132. });
  133. });
  134. it('should put spaces on the insides of brackets/braces for arrays/dictionaries, and remove extraneous spaces between keys and values, and between array items', function() {
  135. _assertCompiledResultIsCorrect({
  136. value: {a: 'b'},
  137. expected: '{ a: \'b\' }'
  138. });
  139. _assertCompiledResultIsCorrect({
  140. value: ['a','b',45],
  141. expected: '[ \'a\', \'b\', 45 ]'
  142. });
  143. _assertCompiledResultIsCorrect({
  144. value: ['a','b',45,{x: 'stuff!'}],
  145. expected: '[ \'a\', \'b\', 45, { x: \'stuff!\' } ]'
  146. });
  147. _assertCompiledResultIsCorrect({
  148. value: ['a','b',45,[ {x: 'stuff!'}, null, true] ],
  149. expected: '[ \'a\', \'b\', 45, [ { x: \'stuff!\' }, null, true ] ]'
  150. });
  151. });
  152. it('should start using newlines when dictionaries have values that end up taking more characters when rendered (key length doesn\'t seem to matter)', function() {
  153. _assertCompiledResultIsCorrect({
  154. value: {a: 'b', c: 'dogfooddogfooddogfooddogfooddogfooddogfooddogfooddogfood' },
  155. expected: '{ a: \'b\',\n c: \'dogfooddogfooddogfooddogfooddogfooddogfooddogfooddogfood\' }'
  156. });
  157. _assertCompiledResultIsCorrect({
  158. value: {a: 'b', catfoodcatfoodcatfoodcatfoodcatfood: 'd' },
  159. expected: '{ a: \'b\', catfoodcatfoodcatfoodcatfoodcatfood: \'d\' }'
  160. });
  161. });
  162. it('should act like `dehydrate` for nested values, and follow the same indentation/newline formatting rules as at the top-level', function() {
  163. _assertCompiledResultIsCorrect({
  164. value: [{
  165. someDate: new Date('November 5, 1605 GMT'),
  166. someRegExp: /waldo/gi,
  167. someError: (function(){
  168. var err = new Error('some passive aggressive message');
  169. err.stack = 'setting this stack property to something inane so that it\'s easy to compare, and so tests don\'t depend on file paths from the stack trace of my computer';
  170. return err;
  171. })(),
  172. someFunction: function foobar(x,y){ return x+y; },
  173. weirdNumbers: [Infinity, -Infinity, NaN, -0, 0],
  174. weirdExistentials: [null, undefined],
  175. nodejsThings: {
  176. stream: new (require('stream').Readable)()
  177. }
  178. }],
  179. expected: '[ { someDate: \'1605-11-05T00:00:00.000Z\',\n someRegExp: \'/waldo/gi\',\n someError: \'setting this stack property to something inane so that it\\\'s easy to compare, and so tests don\\\'t depend on file paths from the stack trace of my computer\',\n someFunction: function foobar(x,y){ return x+y; },\n weirdNumbers: [ 0, 0, 0, 0, 0 ],\n weirdExistentials: [ null ],\n nodejsThings: { stream: null } } ]'
  180. });
  181. });
  182. });
  183. function _assertCompiledResultIsCorrect(opts){
  184. // console.log('ACTUAL:',rttc.compile(opts.value));
  185. // console.log('\n\n\nEXPECTING:',opts.expected);
  186. assert.strictEqual(typeof rttc.compile(opts.value), 'string');
  187. assert.strictEqual(rttc.compile(opts.value), opts.expected);
  188. }