space.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var test = require('tape');
  2. var stringify = require('../');
  3. test('space parameter', function (t) {
  4. t.plan(1);
  5. var obj = { one: 1, two: 2 };
  6. t.equal(stringify(obj, {space: ' '}), ''
  7. + '{\n'
  8. + ' "one": 1,\n'
  9. + ' "two": 2\n'
  10. + '}'
  11. );
  12. });
  13. test('space parameter (with tabs)', function (t) {
  14. t.plan(1);
  15. var obj = { one: 1, two: 2 };
  16. t.equal(stringify(obj, {space: '\t'}), ''
  17. + '{\n'
  18. + '\t"one": 1,\n'
  19. + '\t"two": 2\n'
  20. + '}'
  21. );
  22. });
  23. test('space parameter (with a number)', function (t) {
  24. t.plan(1);
  25. var obj = { one: 1, two: 2 };
  26. t.equal(stringify(obj, {space: 3}), ''
  27. + '{\n'
  28. + ' "one": 1,\n'
  29. + ' "two": 2\n'
  30. + '}'
  31. );
  32. });
  33. test('space parameter (nested objects)', function (t) {
  34. t.plan(1);
  35. var obj = { one: 1, two: { b: 4, a: [2,3] } };
  36. t.equal(stringify(obj, {space: ' '}), ''
  37. + '{\n'
  38. + ' "one": 1,\n'
  39. + ' "two": {\n'
  40. + ' "a": [\n'
  41. + ' 2,\n'
  42. + ' 3\n'
  43. + ' ],\n'
  44. + ' "b": 4\n'
  45. + ' }\n'
  46. + '}'
  47. );
  48. });
  49. test('space parameter (same as native)', function (t) {
  50. t.plan(1);
  51. // for this test, properties need to be in alphabetical order
  52. var obj = { one: 1, two: { a: [2,3], b: 4 } };
  53. t.equal(stringify(obj, {space: ' '}), JSON.stringify(obj, null, ' '));
  54. });