indent-option.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. var test = require('tape');
  2. var forEach = require('for-each');
  3. var inspect = require('../');
  4. test('bad indent options', function (t) {
  5. forEach([
  6. undefined,
  7. true,
  8. false,
  9. -1,
  10. 1.2,
  11. Infinity,
  12. -Infinity,
  13. NaN
  14. ], function (indent) {
  15. t['throws'](
  16. function () { inspect('', { indent: indent }); },
  17. TypeError,
  18. inspect(indent) + ' is invalid'
  19. );
  20. });
  21. t.end();
  22. });
  23. test('simple object with indent', function (t) {
  24. t.plan(2);
  25. var obj = { a: 1, b: 2 };
  26. var expectedSpaces = [
  27. '{',
  28. ' a: 1,',
  29. ' b: 2',
  30. '}'
  31. ].join('\n');
  32. var expectedTabs = [
  33. '{',
  34. ' a: 1,',
  35. ' b: 2',
  36. '}'
  37. ].join('\n');
  38. t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
  39. t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
  40. });
  41. test('two deep object with indent', function (t) {
  42. t.plan(2);
  43. var obj = { a: 1, b: { c: 3, d: 4 } };
  44. var expectedSpaces = [
  45. '{',
  46. ' a: 1,',
  47. ' b: {',
  48. ' c: 3,',
  49. ' d: 4',
  50. ' }',
  51. '}'
  52. ].join('\n');
  53. var expectedTabs = [
  54. '{',
  55. ' a: 1,',
  56. ' b: {',
  57. ' c: 3,',
  58. ' d: 4',
  59. ' }',
  60. '}'
  61. ].join('\n');
  62. t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
  63. t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
  64. });
  65. test('simple array with all single line elements', function (t) {
  66. t.plan(2);
  67. var obj = [1, 2, 3, 'asdf\nsdf'];
  68. var expected = '[ 1, 2, 3, \'asdf\\nsdf\' ]';
  69. t.equal(inspect(obj, { indent: 2 }), expected, 'two');
  70. t.equal(inspect(obj, { indent: '\t' }), expected, 'tabs');
  71. });
  72. test('array with complex elements', function (t) {
  73. t.plan(2);
  74. var obj = [1, { a: 1, b: { c: 1 } }, 'asdf\nsdf'];
  75. var expectedSpaces = [
  76. '[',
  77. ' 1,',
  78. ' {',
  79. ' a: 1,',
  80. ' b: {',
  81. ' c: 1',
  82. ' }',
  83. ' },',
  84. ' \'asdf\\nsdf\'',
  85. ']'
  86. ].join('\n');
  87. var expectedTabs = [
  88. '[',
  89. ' 1,',
  90. ' {',
  91. ' a: 1,',
  92. ' b: {',
  93. ' c: 1',
  94. ' }',
  95. ' },',
  96. ' \'asdf\\nsdf\'',
  97. ']'
  98. ].join('\n');
  99. t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
  100. t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
  101. });
  102. test('values', function (t) {
  103. t.plan(2);
  104. var obj = [{}, [], { 'a-b': 5 }];
  105. var expectedSpaces = [
  106. '[',
  107. ' {},',
  108. ' [],',
  109. ' {',
  110. ' \'a-b\': 5',
  111. ' }',
  112. ']'
  113. ].join('\n');
  114. var expectedTabs = [
  115. '[',
  116. ' {},',
  117. ' [],',
  118. ' {',
  119. ' \'a-b\': 5',
  120. ' }',
  121. ']'
  122. ].join('\n');
  123. t.equal(inspect(obj, { indent: 2 }), expectedSpaces, 'two');
  124. t.equal(inspect(obj, { indent: '\t' }), expectedTabs, 'tabs');
  125. });
  126. test('Map', { skip: typeof Map !== 'function' }, function (t) {
  127. var map = new Map();
  128. map.set({ a: 1 }, ['b']);
  129. map.set(3, NaN);
  130. var expectedStringSpaces = [
  131. 'Map (2) {',
  132. ' { a: 1 } => [ \'b\' ],',
  133. ' 3 => NaN',
  134. '}'
  135. ].join('\n');
  136. var expectedStringTabs = [
  137. 'Map (2) {',
  138. ' { a: 1 } => [ \'b\' ],',
  139. ' 3 => NaN',
  140. '}'
  141. ].join('\n');
  142. var expectedStringTabsDoubleQuotes = [
  143. 'Map (2) {',
  144. ' { a: 1 } => [ "b" ],',
  145. ' 3 => NaN',
  146. '}'
  147. ].join('\n');
  148. t.equal(
  149. inspect(map, { indent: 2 }),
  150. expectedStringSpaces,
  151. 'Map keys are not indented (two)'
  152. );
  153. t.equal(
  154. inspect(map, { indent: '\t' }),
  155. expectedStringTabs,
  156. 'Map keys are not indented (tabs)'
  157. );
  158. t.equal(
  159. inspect(map, { indent: '\t', quoteStyle: 'double' }),
  160. expectedStringTabsDoubleQuotes,
  161. 'Map keys are not indented (tabs + double quotes)'
  162. );
  163. t.equal(inspect(new Map(), { indent: 2 }), 'Map (0) {}', 'empty Map should show as empty (two)');
  164. t.equal(inspect(new Map(), { indent: '\t' }), 'Map (0) {}', 'empty Map should show as empty (tabs)');
  165. var nestedMap = new Map();
  166. nestedMap.set(nestedMap, map);
  167. var expectedNestedSpaces = [
  168. 'Map (1) {',
  169. ' [Circular] => Map (2) {',
  170. ' { a: 1 } => [ \'b\' ],',
  171. ' 3 => NaN',
  172. ' }',
  173. '}'
  174. ].join('\n');
  175. var expectedNestedTabs = [
  176. 'Map (1) {',
  177. ' [Circular] => Map (2) {',
  178. ' { a: 1 } => [ \'b\' ],',
  179. ' 3 => NaN',
  180. ' }',
  181. '}'
  182. ].join('\n');
  183. t.equal(inspect(nestedMap, { indent: 2 }), expectedNestedSpaces, 'Map containing a Map should work (two)');
  184. t.equal(inspect(nestedMap, { indent: '\t' }), expectedNestedTabs, 'Map containing a Map should work (tabs)');
  185. t.end();
  186. });
  187. test('Set', { skip: typeof Set !== 'function' }, function (t) {
  188. var set = new Set();
  189. set.add({ a: 1 });
  190. set.add(['b']);
  191. var expectedStringSpaces = [
  192. 'Set (2) {',
  193. ' {',
  194. ' a: 1',
  195. ' },',
  196. ' [ \'b\' ]',
  197. '}'
  198. ].join('\n');
  199. var expectedStringTabs = [
  200. 'Set (2) {',
  201. ' {',
  202. ' a: 1',
  203. ' },',
  204. ' [ \'b\' ]',
  205. '}'
  206. ].join('\n');
  207. t.equal(inspect(set, { indent: 2 }), expectedStringSpaces, 'new Set([{ a: 1 }, ["b"]]) should show size and contents (two)');
  208. t.equal(inspect(set, { indent: '\t' }), expectedStringTabs, 'new Set([{ a: 1 }, ["b"]]) should show size and contents (tabs)');
  209. t.equal(inspect(new Set(), { indent: 2 }), 'Set (0) {}', 'empty Set should show as empty (two)');
  210. t.equal(inspect(new Set(), { indent: '\t' }), 'Set (0) {}', 'empty Set should show as empty (tabs)');
  211. var nestedSet = new Set();
  212. nestedSet.add(set);
  213. nestedSet.add(nestedSet);
  214. var expectedNestedSpaces = [
  215. 'Set (2) {',
  216. ' Set (2) {',
  217. ' {',
  218. ' a: 1',
  219. ' },',
  220. ' [ \'b\' ]',
  221. ' },',
  222. ' [Circular]',
  223. '}'
  224. ].join('\n');
  225. var expectedNestedTabs = [
  226. 'Set (2) {',
  227. ' Set (2) {',
  228. ' {',
  229. ' a: 1',
  230. ' },',
  231. ' [ \'b\' ]',
  232. ' },',
  233. ' [Circular]',
  234. '}'
  235. ].join('\n');
  236. t.equal(inspect(nestedSet, { indent: 2 }), expectedNestedSpaces, 'Set containing a Set should work (two)');
  237. t.equal(inspect(nestedSet, { indent: '\t' }), expectedNestedTabs, 'Set containing a Set should work (tabs)');
  238. t.end();
  239. });