byFunction.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var json = require('../');
  2. var gulp = require('gulp');
  3. it('should modify property of JSON object (by function editor)', function(done) {
  4. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  5. obj.version = '2.0.0';
  6. return obj;
  7. }));
  8. stream.on('data', function(file) {
  9. var expected =
  10. '{\n' +
  11. ' "name": "test object",\n' +
  12. ' "version": "2.0.0",\n' +
  13. ' "nested": {\n' +
  14. ' "name": "nested object",\n' +
  15. ' "version": "1.0.0"\n' +
  16. ' },\n' +
  17. ' "authors": ["tom"]\n' +
  18. '}';
  19. file.contents.toString().should.eql(expected);
  20. done();
  21. });
  22. });
  23. it('should add property of JSON object (by function editor)', function(done) {
  24. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  25. obj.description = 'this is test';
  26. return obj;
  27. }));
  28. stream.on('data', function(file) {
  29. var expected =
  30. '{\n' +
  31. ' "name": "test object",\n' +
  32. ' "version": "1.0.0",\n' +
  33. ' "nested": {\n' +
  34. ' "name": "nested object",\n' +
  35. ' "version": "1.0.0"\n' +
  36. ' },\n' +
  37. ' "authors": ["tom"],\n' +
  38. ' "description": "this is test"\n' +
  39. '}';
  40. file.contents.toString().should.eql(expected);
  41. done();
  42. });
  43. });
  44. it('should remove property of JSON object (by function editor)', function(done) {
  45. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  46. delete obj.name;
  47. return obj;
  48. }));
  49. stream.on('data', function(file) {
  50. var expected =
  51. '{\n' +
  52. ' "version": "1.0.0",\n' +
  53. ' "nested": {\n' +
  54. ' "name": "nested object",\n' +
  55. ' "version": "1.0.0"\n' +
  56. ' },\n' +
  57. ' "authors": ["tom"]\n' +
  58. '}';
  59. file.contents.toString().should.eql(expected);
  60. done();
  61. });
  62. });
  63. it('should modify nested property of JSON object (by function editor)', function(done) {
  64. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  65. obj.nested.version = '2.0.1';
  66. return obj;
  67. }));
  68. stream.on('data', function(file) {
  69. var expected =
  70. '{\n' +
  71. ' "name": "test object",\n' +
  72. ' "version": "1.0.0",\n' +
  73. ' "nested": {\n' +
  74. ' "name": "nested object",\n' +
  75. ' "version": "2.0.1"\n' +
  76. ' },\n' +
  77. ' "authors": ["tom"]\n' +
  78. '}';
  79. file.contents.toString().should.eql(expected);
  80. done();
  81. });
  82. });
  83. it('should add nested property of JSON object (by function editor)', function(done) {
  84. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  85. obj.nested.description = 'this is test for nested';
  86. return obj;
  87. }));
  88. stream.on('data', function(file) {
  89. var expected =
  90. '{\n' +
  91. ' "name": "test object",\n' +
  92. ' "version": "1.0.0",\n' +
  93. ' "nested": {\n' +
  94. ' "name": "nested object",\n' +
  95. ' "version": "1.0.0",\n' +
  96. ' "description": "this is test for nested"\n' +
  97. ' },\n' +
  98. ' "authors": ["tom"]\n' +
  99. '}';
  100. file.contents.toString().should.eql(expected);
  101. done();
  102. });
  103. });
  104. it('should remove nested property of JSON object (by function editor)', function(done) {
  105. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  106. delete obj.nested.name;
  107. return obj;
  108. }));
  109. stream.on('data', function(file) {
  110. var expected =
  111. '{\n' +
  112. ' "name": "test object",\n' +
  113. ' "version": "1.0.0",\n' +
  114. ' "nested": {\n' +
  115. ' "version": "1.0.0"\n' +
  116. ' },\n' +
  117. ' "authors": ["tom"]\n' +
  118. '}';
  119. file.contents.toString().should.eql(expected);
  120. done();
  121. });
  122. });
  123. it('should multiple properties of JSON object (by function editor)', function(done) {
  124. var stream = gulp.src('test/test.json').pipe(json(function(obj) {
  125. obj.version = '2.0.0';
  126. obj.description = 'this is test';
  127. delete obj.name;
  128. obj.nested.version = '2.0.1';
  129. obj.nested.description = 'this is test for nested';
  130. delete obj.nested.name;
  131. return obj;
  132. }));
  133. stream.on('data', function(file) {
  134. var expected =
  135. '{\n' +
  136. ' "version": "2.0.0",\n' +
  137. ' "nested": {\n' +
  138. ' "version": "2.0.1",\n' +
  139. ' "description": "this is test for nested"\n' +
  140. ' },\n' +
  141. ' "authors": ["tom"],\n' +
  142. ' "description": "this is test"\n' +
  143. '}';
  144. file.contents.toString().should.eql(expected);
  145. done();
  146. });
  147. });