byFunction.js 4.1 KB

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