option.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var json = require('../');
  2. var gulp = require('gulp');
  3. it('should pass-through second argument to js-beautify', function(done) {
  4. var stream = gulp.src('test/test.json').pipe(json({
  5. version: '2.0.0',
  6. description: 'this is test',
  7. array: [
  8. '1234567890', '1234567890', '1234567890', '1234567890',
  9. '1234567890', '1234567890', '1234567890', '1234567890',
  10. ],
  11. nested: {
  12. version: '2.0.1',
  13. description: 'this is test for nested',
  14. },
  15. },
  16. {
  17. indent_size: 3,
  18. indent_char: ' ',
  19. brace_style: 'expand',
  20. preserve_newlines: false,
  21. wrap_line_length: 80,
  22. }));
  23. stream.on('data', function(file) {
  24. var expected =
  25. '{\n' +
  26. ' "name": "test object",\n' +
  27. ' "version": "2.0.0",\n' +
  28. ' "nested":\n' +
  29. ' {\n' +
  30. ' "name": "nested object",\n' +
  31. ' "version": "2.0.1",\n' +
  32. ' "description": "this is test for nested"\n' +
  33. ' },\n' +
  34. ' "authors": ["tom"],\n' +
  35. ' "description": "this is test",\n' +
  36. ' "array": ["1234567890", "1234567890", "1234567890", "1234567890",\n' +
  37. ' "1234567890", "1234567890", "1234567890", "1234567890"\n' +
  38. ' ]\n' +
  39. '}';
  40. file.contents.toString().should.eql(expected);
  41. done();
  42. });
  43. });
  44. it('should keep indentation', function(done) {
  45. var stream = gulp.src('test/test.json').pipe(json({
  46. version: '2.0.0',
  47. description: 'this is test',
  48. array: [
  49. '1234567890', '1234567890', '1234567890', '1234567890',
  50. '1234567890', '1234567890', '1234567890', '1234567890',
  51. ],
  52. nested: {
  53. version: '2.0.1',
  54. description: 'this is test for nested',
  55. },
  56. },
  57. {
  58. brace_style: 'expand',
  59. preserve_newlines: false,
  60. wrap_line_length: 80,
  61. }));
  62. stream.on('data', function(file) {
  63. var expected =
  64. '{\n' +
  65. ' "name": "test object",\n' +
  66. ' "version": "2.0.0",\n' +
  67. ' "nested":\n' +
  68. ' {\n' +
  69. ' "name": "nested object",\n' +
  70. ' "version": "2.0.1",\n' +
  71. ' "description": "this is test for nested"\n' +
  72. ' },\n' +
  73. ' "authors": ["tom"],\n' +
  74. ' "description": "this is test",\n' +
  75. ' "array": ["1234567890", "1234567890", "1234567890", "1234567890",\n' +
  76. ' "1234567890", "1234567890", "1234567890", "1234567890"\n' +
  77. ' ]\n' +
  78. '}';
  79. file.contents.toString().should.eql(expected);
  80. done();
  81. });
  82. });
  83. it('should bypass beautification when property is set', function(done) {
  84. var stream = gulp.src('test/test.json').pipe(json({
  85. version: '2.0.0',
  86. description: 'this is test',
  87. array: [
  88. '1234567890', '1234567890', '1234567890', '1234567890',
  89. '1234567890', '1234567890', '1234567890', '1234567890',
  90. ],
  91. nested: {
  92. version: '2.0.1',
  93. description: 'this is test for nested',
  94. },
  95. },
  96. {
  97. beautify: false,
  98. }));
  99. stream.on('data', function(file) {
  100. var expected = '{"name":"test object","version":"2.0.0",' +
  101. '"nested":{"name":"nested object","version":"2.0.1",' +
  102. '"description":"this is test for nested"},"authors":["tom"],' +
  103. '"description":"this is test",' +
  104. '"array":["1234567890","1234567890","1234567890","1234567890",' +
  105. '"1234567890","1234567890","1234567890","1234567890"]}';
  106. file.contents.toString().should.eql(expected);
  107. done();
  108. });
  109. });
  110. it('should pass-through third argument to deepmerge and do an overwriteMerge', function(done) {
  111. var stream = gulp.src('test/test.json').pipe(json({
  112. authors: ['tomcat'],
  113. },{},{
  114. arrayMerge: function(dist,source) {
  115. return source;
  116. },
  117. }));
  118. stream.on('data', function(file) {
  119. var expected =
  120. '{\n' +
  121. ' "name": "test object",\n' +
  122. ' "version": "1.0.0",\n' +
  123. ' "nested": {\n' +
  124. ' "name": "nested object",\n' +
  125. ' "version": "1.0.0"\n' +
  126. ' },\n' +
  127. ' "authors": ["tomcat"]\n' +
  128. '}';
  129. file.contents.toString().should.eql(expected);
  130. done();
  131. });
  132. });