basic_spec.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 includes reading of different files', function () {
  8. var fs = require('fs');
  9. var MultiIni = require('../src');
  10. var Serializer = require('../src/serializer');
  11. it('Availability of the class ', function () {
  12. expect(MultiIni).not.to.be.undefined;
  13. expect(MultiIni).not.to.be.null;
  14. });
  15. it('Read a basic with a section and 2 simple keys and a comment', function () {
  16. var ini = new MultiIni.Class();
  17. var data = ini.read('test/data/single.ini');
  18. expect(data).not.to.be.null;
  19. expect(data['section1']).to.be.defined;
  20. expect(data['section1']['key1']).to.equal('value1');
  21. expect(data['section1']['key2']).to.equal('value2');
  22. expect(data['section1']['key3']).to.equal('value3');
  23. expect(data['section1']['key4']).to.equal('value4');
  24. expect(data['section1']['key5']).to.equal('value5');
  25. expect(data['section1']['key6']).to.equal('value6');
  26. expect(data['section1']['key7']).to.equal(' value7');
  27. expect(data['section1']['key8']).to.equal('VALUE8');
  28. });
  29. it('Read a basic, detect boolean values and properly convert them', function () {
  30. var ini = new MultiIni.Class({ filters: [MultiIni.filters.boolean] });
  31. var data = ini.read('test/data/boolean_values.ini');
  32. expect(data).not.to.be.null;
  33. expect(data['section1']).to.be.defined;
  34. expect(data['section1']['key1']).to.equal(true);
  35. expect(data['section1']['key2']).to.equal(false);
  36. expect(data['section1']['key3']).to.equal(true);
  37. expect(data['section1']['key4']).to.equal(false);
  38. expect(data['section1']['key5']).to.equal(true);
  39. expect(data['section1']['key6']).to.equal(false);
  40. expect(data['section1']['key7']).to.equal(true);
  41. expect(data['section1']['key8']).to.equal(false);
  42. expect(data['section1']['key10']).to.equal(true);
  43. expect(data['section1']['key11']).to.equal(false);
  44. });
  45. it('Read a basic with a section with multi line values', function () {
  46. var ini = new MultiIni.Class();
  47. var data = ini.read('test/data/multi_line.ini');
  48. expect(data).not.to.be.null;
  49. expect(data['section1']).to.be.defined;
  50. // test first value
  51. expect(data['section1']['key1']).to.equal('value1');
  52. // multi line parsing should stop to fetch key5
  53. expect(data['section1']['key5']).to.equal('value5');
  54. expect(data['section1']['key2']).to.equal('line1\nline2');
  55. expect(data['section1']['key3']).to.equal('\nline2\nline3');
  56. expect(data['section1']['key4']).to.equal('\nline2\nline3\n');
  57. expect(data['section1']['key6']).to.equal('\nline1\nline2=dummy\n');
  58. expect(data['section1']['key7']).to.equal('line1\n line2\n line3');
  59. });
  60. it('Read a basic with a section with multi level keys and single and multi line values', function () {
  61. var ini = new MultiIni.Class();
  62. var data = ini.read('test/data/multi_level_line.ini');
  63. expect(data).not.to.be.null;
  64. expect(data['section1']).to.be.defined;
  65. // check for the second level
  66. expect(data['section1']['key1']).to.be.defined;
  67. expect(data['section1']['key1']['subkey1']).to.equal('value1');
  68. expect(data['section1']['key1']['subkey2']).to.equal('value2');
  69. // check the third level
  70. expect(data['section1']['key2']).to.be.defined;
  71. expect(data['section1']['key2']['subkey1']['subkey1']).to.equal('value3');
  72. expect(data['section1']['key2']['subkey1']['subkey2']).to.equal('value4');
  73. expect(data['section1']['key2']['subkey2']).to.equal('value5');
  74. expect(data['section1']['key2']['subkey3']).to.be.defined;
  75. expect(data['section1']['key2']['subkey3']['subkey1']).to.equal('value6');
  76. // multi line parsing with second level
  77. expect(data['section1']['key3']).to.be.defined;
  78. expect(data['section1']['key3']['subkey1']).to.equal('line1\nline2');
  79. expect(data['section1']['key3']['subkey2']).to.equal('\nline2\nline3');
  80. expect(data['section1']['key3']['subkey3']).to.equal('\nline2\nline3\n');
  81. // multi line parsing with third level
  82. expect(data['section1']['key4']).to.be.defined;
  83. expect(data['section1']['key4']['subkey1']).to.be.defined;
  84. expect(data['section1']['key4']['subkey1']['subkey1']).to.equal('line1\nline2');
  85. expect(data['section1']['key4']['subkey1']['subkey2']).to.equal('\nline2\nline3');
  86. expect(data['section1']['key4']['subkey1']['subkey3']).to.equal('\nline2\nline3\n');
  87. expect(data['section1']['key4']['subkey2']).to.equal('value');
  88. });
  89. it('Read ini with array definitions', function () {
  90. var ini = new MultiIni.Class();
  91. var data = ini.read('test/data/array.ini');
  92. expect(data).not.to.be.null;
  93. expect(data['section1']).to.be.defined;
  94. // array in key
  95. expect(data['section1']['key1']).to.be.defined;
  96. expect(data['section1']['key1'].length).to.equal(2);
  97. expect(data['section1']['key1'][0]).to.equal('value1');
  98. expect(data['section1']['key1'][1]).to.equal('value2');
  99. // normal key value
  100. expect(data['section1']['key2']).to.equal('value3');
  101. // array in subkey
  102. expect(data['section1']['key3']).to.be.defined;
  103. expect(data['section1']['key3']['subkey']).to.be.defined;
  104. expect(data['section1']['key3']['subkey'].length).to.equal(2);
  105. expect(data['section1']['key3']['subkey'][0]).to.equal('value4');
  106. expect(data['section1']['key3']['subkey'][1]).to.equal('value5');
  107. });
  108. it('Read ini with section inheritance', function () {
  109. var ini = new MultiIni.Class();
  110. var data = ini.read('test/data/section_inheritance.ini');
  111. console.error(data);
  112. expect(data).not.to.be.null;
  113. expect(data['section2']).to.be.defined;
  114. expect(data['section2']['key1']).to.equal('value1');
  115. expect(data['section1']['key2']).to.equal('value2');
  116. expect(data['section2']['key2']).to.equal('overwritten value2');
  117. expect(data['section2']['key3']).to.equal('value3');
  118. expect(data['section2']['key4']['nested']['deeper']).to.equal('value4');
  119. expect(data['section1']['key5']['nested']['deeper']).to.equal('value5');
  120. expect(data['section2']['key5']['nested']['deeper']).to.equal('overwritten value5');
  121. });
  122. it('Read ini with section inheritance for arrays', function () {
  123. var ini = new MultiIni.Class();
  124. var data = ini.read('test/data/section_inheritance.ini');
  125. expect(data).not.to.be.null;
  126. expect(data['section2']).to.be.defined;
  127. expect(data['section1']['key6']).to.be.defined;
  128. expect(data['section1']['key6']['subkey']).to.be.defined;
  129. expect(data['section1']['key6']['subkey'].length).to.equal(1);
  130. expect(data['section1']['key6']['subkey'][0]).to.equal('valueX');
  131. expect(data['section1']['key6']['subkey'][1]).to.be.undefined;
  132. expect(data['section2']['key6']['subkey'].length).to.equal(2);
  133. expect(data['section2']['key6']['subkey'][0]).to.equal('valueX');
  134. expect(data['section2']['key6']['subkey'][1]).to.equal('valueY');
  135. });
  136. it('Write ini file with one section and multiple single line values', function () {
  137. var data = {
  138. section1: {
  139. key1: 'value1',
  140. key2: 'value2',
  141. },
  142. };
  143. var ini = new MultiIni.Class();
  144. ini.write('test/out/single.ini', data);
  145. var content = fs.readFileSync('test/out/single.ini', { encoding: 'utf8' });
  146. var expectedContent = fs.readFileSync('test/data/result/single.ini', { encoding: 'utf8' });
  147. expect(content).to.equal(expectedContent);
  148. });
  149. it('Write ini file with one section and multiple multi level single line values', function () {
  150. var data = {
  151. section1: {
  152. key1: {
  153. subkey1: 'value1',
  154. subkey2: 'value2',
  155. },
  156. key2: {
  157. subkey: 'value3',
  158. },
  159. },
  160. };
  161. var ini = new MultiIni.Class();
  162. ini.write('test/out/multi_level.ini', data);
  163. var content = fs.readFileSync('test/out/multi_level.ini', { encoding: 'utf8' });
  164. var expectedContent = fs.readFileSync('test/data/result/multi_level.ini', {
  165. encoding: 'utf8',
  166. });
  167. expect(content).to.equal(expectedContent);
  168. });
  169. it('Write a file with single and multi level and array definitions', function () {
  170. var data = {
  171. section1: {
  172. key1: {
  173. subkey1: ['value1', 'value2'],
  174. subkey2: 'value3',
  175. },
  176. key2: ['value4', 'value5'],
  177. },
  178. };
  179. var ini = new MultiIni.Class();
  180. ini.write('test/out/array.ini', data);
  181. var content = fs.readFileSync('test/out/array.ini', { encoding: 'utf8' });
  182. var expectedContent = fs.readFileSync('test/data/result/array.ini', { encoding: 'utf8' });
  183. expect(content).to.equal(expectedContent);
  184. });
  185. it('Write a file with single and multi level with multi line', function () {
  186. var data = {
  187. section1: {
  188. key1: {
  189. subkey1: 'line1\nline2',
  190. subkey2: '\nline2',
  191. subkey3: '\nline2\n',
  192. subkey4: 'value1',
  193. },
  194. key2: 'line1\nline2',
  195. key3: '\nline2',
  196. key4: '\nline2\n',
  197. key5: 'value2',
  198. },
  199. };
  200. var ini = new MultiIni.Class();
  201. ini.write('test/out/multi_line.ini', data);
  202. var content = fs.readFileSync('test/out/multi_line.ini', { encoding: 'utf8' });
  203. var expectedContent = fs.readFileSync('test/data/result/multi_line.ini', {
  204. encoding: 'utf8',
  205. });
  206. expect(content).to.equal(expectedContent);
  207. });
  208. it('Write a file with single and multi level, multi line and array', function () {
  209. var data = {
  210. section1: {
  211. key1: {
  212. subkey1: ['line1\nline2', '\nline2', '\nline2\n', 'value1'],
  213. subkey2: 'value2',
  214. },
  215. key2: ['line1\nline2', '\nline2', '\nline2\n', 'value3'],
  216. key3: 'value4',
  217. },
  218. };
  219. var ini = new MultiIni.Class();
  220. ini.write('test/out/all.ini', data);
  221. var content = fs.readFileSync('test/out/all.ini', { encoding: 'utf8' });
  222. var expectedContent = fs.readFileSync('test/data/result/all.ini', { encoding: 'utf8' });
  223. expect(content).to.equal(expectedContent);
  224. });
  225. it('Write a ascii ini file and write it manuel', function () {
  226. var ini = new MultiIni.Class({ encoding: 'ascii' });
  227. var serializer = new Serializer({ encoding: 'ascii', line_breaks: 'unix' });
  228. var data = {
  229. section: {
  230. key: 'Straße mit Häusern',
  231. },
  232. };
  233. ini.write('test/out/ascii.ini', data);
  234. fs.writeFileSync('test/out/ascii_serialized.ini', serializer.serialize(data), {
  235. encoding: 'ascii',
  236. });
  237. var content = fs.readFileSync('test/out/ascii.ini', { encoding: 'ascii' });
  238. var expectedContent = fs.readFileSync('test/out/ascii_serialized.ini', {
  239. encoding: 'ascii',
  240. });
  241. expect(content).to.equal(expectedContent);
  242. });
  243. // it("Compare both reading results, via ini and manuel", function () {
  244. // var data = {
  245. // section: {
  246. // key: "Straße mit Häusern"
  247. // }
  248. // };
  249. //
  250. // MultiIni.write('test/out/ascii_read_write.ini', data, {encoding: 'ascii'});
  251. //
  252. // var content = MultiIni.read('test/out/ascii_read_write.ini', {encoding: 'ascii'});
  253. //
  254. // expect(content.section.key).to.equal(data.section.key);
  255. // });
  256. // it("Read a file with an invalid lines ignoring it", function () {
  257. // var data = MultiIni.read('test/data/invalid_line.ini')
  258. //
  259. // expect(data).not.to.be.null;
  260. //
  261. // expect(data['section']).to.be.defined;
  262. //
  263. // expect(data['section']['valid']).to.equal('valid');
  264. //
  265. // expect(data['section']['invalid']).not.to.be.defined;
  266. //
  267. // expect(data['section']['invalid_multiline']).not.to.be.defined;
  268. // });
  269. //
  270. // it("Read a file with an invalid lines reading it", function () {
  271. // var data = MultiIni.read('test/data/invalid_line.ini', {ignore_invalid: false})
  272. //
  273. // expect(data).not.to.be.null;
  274. //
  275. // expect(data['section']).to.be.defined;
  276. //
  277. // expect(data['section']['valid']).to.equal('valid');
  278. //
  279. // expect(data['section']['invalid']).to.equal('invalid');
  280. //
  281. // expect(data['section']['invalid_multiline']).to.equal('line1\nline2\nline3');
  282. // });
  283. //
  284. // it("Read a file with an invalid with invalid callback and abort", function () {
  285. // var cb = function (line) {
  286. // expect(line).to.equal('invalid="invalid"a')
  287. // return false
  288. // };
  289. // var data = MultiIni.read('test/data/invalid_line.ini', {ignore_invalid: false, oninvalid: cb})
  290. //
  291. // expect(data).to.be.undefined;
  292. // });
  293. it('Read a basic file with a section and 2 simple keys and one multiline with keep quotes', function () {
  294. var ini = new MultiIni.Class({ keep_quotes: true });
  295. var data = ini.read('test/data/combined_keep_quotes.ini');
  296. expect(data).not.to.be.null;
  297. expect(data['section1']).to.be.defined;
  298. expect(data['section1']['key1']).to.equal('"value1"');
  299. expect(data['section1']['multiline']).to.equal('"line1\nline2\nline3\n"');
  300. expect(data['section1']['key2']).to.equal('value2');
  301. });
  302. it('Read a basic file with a section and 2 simple keys and one multiline with keep quotes and writing it', function () {
  303. var ini = new MultiIni.Class({ keep_quotes: true });
  304. var data = ini.read('test/data/combined_keep_quotes.ini');
  305. ini.write('test/out/combined_keep_quotes.ini', data);
  306. var content = fs.readFileSync('test/out/combined_keep_quotes.ini', { encoding: 'utf8' });
  307. var expectedContent = fs.readFileSync('test/data/result/combined_keep_quotes.ini', {
  308. encoding: 'utf8',
  309. });
  310. expect(content).to.equal(expectedContent);
  311. });
  312. it('Read and writing a file with constants', function () {
  313. var ini = new MultiIni.Class();
  314. var data = ini.read('test/data/constant.ini');
  315. expect(data).not.to.be.null;
  316. expect(data['section']).to.be.defined;
  317. expect(data['section']['key1']).to.equal('"Part1 " CONSTANT');
  318. expect(data['section']['key2']).to.equal('CONSTANT " Part2"');
  319. expect(data['section']['key3']).to.equal('Part1" CONSTANT "Part2');
  320. ini.write('test/out/constant.ini', data);
  321. });
  322. it('Read and writing a file with constants with keeping quotes', function () {
  323. var ini = new MultiIni.Class({ keep_quotes: true });
  324. var data = ini.read('test/data/constant.ini');
  325. expect(data).not.to.be.null;
  326. expect(data['section']).to.be.defined;
  327. expect(data['section']['key1']).to.equal('"Part1 " CONSTANT');
  328. expect(data['section']['key2']).to.equal('CONSTANT " Part2"');
  329. expect(data['section']['key3']).to.equal('"Part1" CONSTANT "Part2"');
  330. ini.write('test/out/constant_keep.ini', data);
  331. });
  332. it('Read a file with a section with values having escaped quotes', function () {
  333. var ini = new MultiIni.Class();
  334. var data = ini.read('test/data/escaped_quotes.ini');
  335. ini.write('test/out/escaped_quotes.ini', data);
  336. var content = fs.readFileSync('test/out/escaped_quotes.ini', { encoding: 'utf8' });
  337. var expectedContent = fs.readFileSync('test/data/result/escaped_quotes.ini', {
  338. encoding: 'utf8',
  339. });
  340. expect(content).to.equal(expectedContent);
  341. });
  342. it('Apply uppercase filter', function () {
  343. var ini = new MultiIni.Class({
  344. filters: [MultiIni.filters.uppercase],
  345. });
  346. var data = ini.read('test/data/single.ini');
  347. expect(data).not.to.be.null;
  348. expect(data['section1']).to.be.defined;
  349. expect(data['section1']['key1']).to.equal('VALUE1');
  350. expect(data['section1']['key8']).to.equal('VALUE8');
  351. });
  352. it('Apply lowercase filter', function () {
  353. var ini = new MultiIni.Class({
  354. filters: [MultiIni.filters.lowercase],
  355. });
  356. var data = ini.read('test/data/single.ini');
  357. expect(data).not.to.be.null;
  358. expect(data['section1']).to.be.defined;
  359. expect(data['section1']['key1']).to.equal('value1');
  360. expect(data['section1']['key8']).to.equal('value8');
  361. });
  362. it('Apply trim empty lines filter', function () {
  363. var ini = new MultiIni.Class({
  364. filters: [MultiIni.filters.trim],
  365. });
  366. var data = ini.read('test/data/multi_line.ini');
  367. expect(data).not.to.be.null;
  368. expect(data['section1']).to.be.defined;
  369. expect(data['section1']['key4']).to.equal('line2\nline3');
  370. });
  371. it('Read multi line ini of issue #12 with windows line breaks', function () {
  372. var instance = new MultiIni.Class({ line_breaks: 'windows' });
  373. var data = instance.read('test/data/issue_12.ini');
  374. expect(data['(U+Pu) métal H2O']['description']).to.equal(
  375. '(U+Pu) métal modération H2O\nParamétrage de la modération en H/(U+Pu)',
  376. );
  377. });
  378. describe('Support section names with .', () => {
  379. it('enabled', function () {
  380. var instance = new MultiIni.Class({ nested_section_names: true });
  381. var data = instance.read('test/data/section_names.ini');
  382. expect(data).to.deep.equal({
  383. normal: {
  384. item: '0',
  385. },
  386. nested: {
  387. child1: {
  388. item1: '1',
  389. },
  390. child2: {
  391. item2: '2',
  392. },
  393. },
  394. });
  395. });
  396. it('default/disabled', function () {
  397. var instance = new MultiIni.Class();
  398. var data = instance.read('test/data/section_names.ini');
  399. expect(data).to.deep.equal({
  400. normal: {
  401. item: '0',
  402. },
  403. 'nested.child1': {
  404. item1: '1',
  405. },
  406. 'nested.child2': {
  407. item2: '2',
  408. },
  409. });
  410. });
  411. });
  412. });