backward_compatibility_spec.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. 'use strict';
  2. const chai = require('chai');
  3. const sinon = require('sinon');
  4. const sinonChai = require('sinon-chai');
  5. const expect = chai.expect;
  6. chai.use(sinonChai);
  7. describe("Basic testing with backward compatibility to pre 0.3.x", function () {
  8. var fs = require('fs');
  9. var MultiIni = require('../src');
  10. it("Read a basic with a section and 2 simple keys and a comment", function () {
  11. var data = MultiIni.read('test/data/single.ini');
  12. expect(data).not.to.be.null;
  13. expect(data['section1']).to.be.defined;
  14. expect(data['section1']['key1']).to.equal('value1');
  15. expect(data['section1']['key2']).to.equal('value2');
  16. expect(data['section1']['key3']).to.equal('value3');
  17. expect(data['section1']['key4']).to.equal('value4');
  18. expect(data['section1']['key5']).to.equal('value5');
  19. expect(data['section1']['key6']).to.equal('value6');
  20. expect(data['section1']['key7']).to.equal(' value7');
  21. });
  22. it("Read a basic with a section with multi line values", function () {
  23. var data = MultiIni.read('test/data/multi_line.ini');
  24. expect(data).not.to.be.null;
  25. expect(data['section1']).to.be.defined;
  26. // test first value
  27. expect(data['section1']['key1']).to.equal('value1');
  28. // multi line parsing should stop to fetch key5
  29. expect(data['section1']['key5']).to.equal('value5');
  30. expect(data['section1']['key2']).to.equal('line1\nline2');
  31. expect(data['section1']['key3']).to.equal('\nline2\nline3');
  32. expect(data['section1']['key4']).to.equal('\nline2\nline3\n');
  33. });
  34. it("Read a basic with a section with multi level keys and single and multi line values", function () {
  35. var data = MultiIni.read('test/data/multi_level_line.ini');
  36. expect(data).not.to.be.null;
  37. expect(data['section1']).to.be.defined;
  38. // check for the second level
  39. expect(data['section1']['key1']).to.be.defined;
  40. expect(data['section1']['key1']['subkey1']).to.equal('value1');
  41. expect(data['section1']['key1']['subkey2']).to.equal('value2');
  42. // check the third level
  43. expect(data['section1']['key2']).to.be.defined;
  44. expect(data['section1']['key2']['subkey1']['subkey1']).to.equal('value3');
  45. expect(data['section1']['key2']['subkey1']['subkey2']).to.equal('value4');
  46. expect(data['section1']['key2']['subkey2']).to.equal('value5');
  47. expect(data['section1']['key2']['subkey3']).to.be.defined;
  48. expect(data['section1']['key2']['subkey3']['subkey1']).to.equal('value6');
  49. // multi line parsing with second level
  50. expect(data['section1']['key3']).to.be.defined;
  51. expect(data['section1']['key3']['subkey1']).to.equal('line1\nline2');
  52. expect(data['section1']['key3']['subkey2']).to.equal('\nline2\nline3');
  53. expect(data['section1']['key3']['subkey3']).to.equal('\nline2\nline3\n');
  54. // multi line parsing with third level
  55. expect(data['section1']['key4']).to.be.defined;
  56. expect(data['section1']['key4']['subkey1']).to.be.defined;
  57. expect(data['section1']['key4']['subkey1']['subkey1']).to.equal('line1\nline2');
  58. expect(data['section1']['key4']['subkey1']['subkey2']).to.equal('\nline2\nline3');
  59. expect(data['section1']['key4']['subkey1']['subkey3']).to.equal('\nline2\nline3\n');
  60. expect(data['section1']['key4']['subkey2']).to.equal('value');
  61. });
  62. it("Read ini with array definitions", function () {
  63. var data = MultiIni.read('test/data/array.ini');
  64. expect(data).not.to.be.null;
  65. expect(data['section1']).to.be.defined;
  66. // array in key
  67. expect(data['section1']['key1']).to.be.defined;
  68. expect(data['section1']['key1'].length).to.equal(2);
  69. expect(data['section1']['key1'][0]).to.equal('value1');
  70. expect(data['section1']['key1'][1]).to.equal('value2');
  71. // normal key value
  72. expect(data['section1']['key2']).to.equal('value3');
  73. // array in subkey
  74. expect(data['section1']['key3']).to.be.defined;
  75. expect(data['section1']['key3']['subkey']).to.be.defined;
  76. expect(data['section1']['key3']['subkey'].length).to.equal(2);
  77. expect(data['section1']['key3']['subkey'][0]).to.equal('value4');
  78. expect(data['section1']['key3']['subkey'][1]).to.equal('value5');
  79. });
  80. it("Write ini file with one section and multiple single line values", function () {
  81. var data = {
  82. section1: {
  83. key1: 'value1',
  84. key2: 'value2'
  85. }
  86. };
  87. MultiIni.write('test/out/single.ini', data);
  88. var content = fs.readFileSync('test/out/single.ini', {encoding: 'utf8'});
  89. var expectedContent = fs.readFileSync('test/data/result/single.ini', {encoding: 'utf8'});
  90. expect(content).to.equal(expectedContent);
  91. });
  92. it("Write ini file with one section and multiple multi level single line values", function () {
  93. var data = {
  94. section1: {
  95. key1: {
  96. subkey1: 'value1',
  97. subkey2: 'value2'
  98. },
  99. key2: {
  100. subkey: 'value3'
  101. }
  102. }
  103. };
  104. MultiIni.write('test/out/multi_level.ini', data);
  105. var content = fs.readFileSync('test/out/multi_level.ini', {encoding: 'utf8'});
  106. var expectedContent = fs.readFileSync('test/data/result/multi_level.ini', {encoding: 'utf8'});
  107. expect(content).to.equal(expectedContent);
  108. });
  109. it("Write a file with single and multi level and array definitions", function () {
  110. var data = {
  111. section1: {
  112. key1: {
  113. subkey1: ['value1', 'value2'],
  114. subkey2: 'value3'
  115. },
  116. key2: ['value4', 'value5']
  117. }
  118. };
  119. MultiIni.write('test/out/array.ini', data);
  120. var content = fs.readFileSync('test/out/array.ini', {encoding: 'utf8'});
  121. var expectedContent = fs.readFileSync('test/data/result/array.ini', {encoding: 'utf8'});
  122. expect(content).to.equal(expectedContent);
  123. });
  124. it("Write a file with single and multi level with multi line", function () {
  125. var data = {
  126. section1: {
  127. key1: {
  128. subkey1: 'line1\nline2',
  129. subkey2: '\nline2',
  130. subkey3: '\nline2\n',
  131. subkey4: 'value1'
  132. },
  133. key2: 'line1\nline2',
  134. key3: '\nline2',
  135. key4: '\nline2\n',
  136. key5: 'value2'
  137. }
  138. };
  139. MultiIni.write('test/out/multi_line.ini', data);
  140. var content = fs.readFileSync('test/out/multi_line.ini', {encoding: 'utf8'});
  141. var expectedContent = fs.readFileSync('test/data/result/multi_line.ini', {encoding: 'utf8'});
  142. expect(content).to.equal(expectedContent);
  143. });
  144. it("Write a file with single and multi level, multi line and array", function () {
  145. var data = {
  146. section1: {
  147. key1: {
  148. subkey1: ['line1\nline2', '\nline2', '\nline2\n', 'value1'],
  149. subkey2: 'value2'
  150. },
  151. key2: ['line1\nline2', '\nline2', '\nline2\n', 'value3'],
  152. key3: 'value4'
  153. }
  154. };
  155. MultiIni.write('test/out/all.ini', data);
  156. var content = fs.readFileSync('test/out/all.ini', {encoding: 'utf8'});
  157. var expectedContent = fs.readFileSync('test/data/result/all.ini', {encoding: 'utf8'});
  158. expect(content).to.equal(expectedContent);
  159. });
  160. it("Read a basic file with a section and 2 simple keys and one multiline with keep quotes", function () {
  161. var data = MultiIni.read('test/data/combined_keep_quotes.ini', {keep_quotes: true});
  162. expect(data).not.to.be.null;
  163. expect(data['section1']).to.be.defined;
  164. expect(data['section1']['key1']).to.equal('"value1"');
  165. expect(data['section1']['multiline']).to.equal('"line1\nline2\nline3\n"');
  166. expect(data['section1']['key2']).to.equal('value2');
  167. });
  168. it("Read a basic file with a section and 2 simple keys and one multiline with keep quotes and writing it", function () {
  169. var data = MultiIni.read('test/data/combined_keep_quotes.ini', {keep_quotes: true});
  170. MultiIni.write('test/out/combined_keep_quotes.ini', data, {keep_quotes: true});
  171. var content = fs.readFileSync('test/out/combined_keep_quotes.ini', {encoding: 'utf8'});
  172. var expectedContent = fs.readFileSync('test/data/result/combined_keep_quotes.ini', {encoding: 'utf8'});
  173. expect(content).to.equal(expectedContent);
  174. });
  175. it("Read and writing a file with constants", function () {
  176. var data = MultiIni.read('test/data/constant.ini');
  177. expect(data).not.to.be.null;
  178. expect(data['section']).to.be.defined;
  179. expect(data['section']['key1']).to.equal('"Part1 " CONSTANT');
  180. expect(data['section']['key2']).to.equal('CONSTANT " Part2"');
  181. expect(data['section']['key3']).to.equal('Part1" CONSTANT "Part2');
  182. MultiIni.write('test/out/constant.ini', data);
  183. });
  184. it("Read and writing a file with constants with keeping quotes", function () {
  185. var data = MultiIni.read('test/data/constant.ini', {keep_quotes: true});
  186. expect(data).not.to.be.null;
  187. expect(data['section']).to.be.defined;
  188. expect(data['section']['key1']).to.equal('"Part1 " CONSTANT');
  189. expect(data['section']['key2']).to.equal('CONSTANT " Part2"');
  190. expect(data['section']['key3']).to.equal('"Part1" CONSTANT "Part2"');
  191. MultiIni.write('test/out/constant_keep.ini', data);
  192. });
  193. });