Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

test.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var fs = require("fs");
  2. var assert = require("assert");
  3. var prop = require("../index.js");
  4. var syncData = prop.read("./test-cases.properties");
  5. prop.read("./test-cases.properties", function(err, data) {
  6. assert.deepEqual(data, syncData);
  7. assert.equal(data["lala"], 'ℊ the foo foo lalala;');
  8. assert.equal(data["website"], 'http://en.wikipedia.org/');
  9. assert.equal(data["language"], 'English');
  10. assert.equal(data["message"], 'Welcome to Wikipedia!');
  11. assert.equal(data["key with spaces"], 'This is the value that could be looked up with the key "key with spaces".');
  12. assert.equal(data["tab"], '\t');
  13. assert.equal(data["long-unicode"], '\u00000009');
  14. assert.equal(data["space separator"], 'key val \n three');
  15. assert.equal(data["another-test"], ':: hihi');
  16. assert.equal(data["null-prop"], '');
  17. assert.ok(data["valueOf"] == null, "Properties are set that shouldn't be (valueOf)");
  18. assert.ok(data["toString"] == null, "Properties are set that shouldn't be (toString)");
  19. console.log("Tests all passed...");
  20. if(process.argv[2] === "repl") {
  21. var repl = require("repl").start("test-repl> ");
  22. repl.context.data = data;
  23. repl.context.prop = prop;
  24. }
  25. });
  26. var editor1 = prop.createEditor();
  27. editor1.set("basic", "prop1");
  28. assert.equal(editor1.toString(), "basic=prop1");
  29. editor1.set("basic", "prop2", "A comment\nmulti-line1");
  30. assert.equal(editor1.toString(), "# A comment\n# multi-line1\nbasic=prop2");
  31. editor1.set("basic", "prop3", "A comment\nmulti-line2");
  32. assert.equal(editor1.toString(), "# A comment\n# multi-line2\nbasic=prop3");
  33. editor1.set("basic", "prop4");
  34. assert.equal(editor1.toString(), "# A comment\n# multi-line2\nbasic=prop4");
  35. editor1.set("basic", "prop5", null); // Delete's comment
  36. assert.equal(editor1.toString(), "basic=prop5");
  37. editor1.set("basic1", "prop6");
  38. assert.equal(editor1.toString(), "basic=prop5\nbasic1=prop6");
  39. editor1.addHeadComment("Head Comment");
  40. assert.equal(editor1.toString(), "# Head Comment\nbasic=prop5\nbasic1=prop6");
  41. assert.ok(editor1.get("valueOf") == null);
  42. assert.ok(editor1.get("toString") == null);
  43. var editor2 = prop.createEditor("./test-cases.properties");
  44. assert.equal(fs.readFileSync("./test-cases.properties").toString(), editor2.toString());
  45. editor2.set("lala", "prop1");
  46. assert.ok(editor2.toString().indexOf("lala=prop1") > -1);
  47. editor2.set("lala", "prop2", "A comment\nmulti-line1");
  48. assert.ok(editor2.toString().indexOf("# A comment\n# multi-line1\nlala=prop2") > -1);
  49. editor2.set("lala", "prop3", "A comment\nmulti-line2");
  50. assert.ok(editor2.toString().indexOf("# A comment\n# multi-line2\nlala=prop3") > -1);
  51. editor2.set("lala", "prop4");
  52. assert.ok(editor2.toString().indexOf("# A comment\n# multi-line2\nlala=prop4") > -1);
  53. editor2.set("lala", "prop5", null); // Delete's comment
  54. assert.ok(editor2.toString().indexOf("! The exclamation mark can also mark text as comments.\nlala=prop5") > -1);
  55. editor2.set("basic-non-existing", "prop6");
  56. assert.ok(editor2.toString().indexOf("\nbasic-non-existing=prop6") > -1);
  57. editor2.addHeadComment("Head Comment");
  58. assert.equal(editor2.toString().indexOf("# Head Comment\n"), 0);
  59. assert.ok(editor2.get("valueOf") == null);
  60. assert.ok(editor2.get("toString") == null);
  61. var editor3 = prop.createEditor();
  62. editor3.set("stay", "ok");
  63. editor3.unset("key");
  64. editor3.unset("key", null);
  65. editor3.unset("key", undefined);
  66. assert.equal(editor3.toString().trim(), "stay=ok");
  67. editor3.set("key", "val");
  68. editor3.unset("key");
  69. assert.equal(editor3.toString().trim(), "stay=ok");
  70. editor3.set("key", "val");
  71. editor3.set("key", null);
  72. assert.equal(editor3.toString().trim(), "stay=ok");
  73. editor3.set("key", "val");
  74. editor3.set("key", undefined);
  75. assert.equal(editor3.toString().trim(), "stay=ok");
  76. var editor4 = prop.createEditor({ separator: ' ' });
  77. editor4.set("stay", "ok");
  78. assert.equal(editor4.toString().trim(), "stay ok");
  79. prop.createEditor("./test-cases.properties", function(err, editor) {
  80. var properties = {};
  81. properties.lala = 'whatever';
  82. properties.website = 'whatever';
  83. properties.language = 'whatever';
  84. properties.message = 'whatever';
  85. properties['key with spaces'] = 'whatever';
  86. properties.tab = 'whatever';
  87. properties['long-unicode'] = 'whatever';
  88. properties['another-test'] = 'whatever';
  89. for (var item in properties) {
  90. editor.set(item, properties[item]);
  91. }
  92. assert.equal(
  93. editor.toString(),
  94. '# You are reading the ".properties" entry.\n' +
  95. '! The exclamation mark can also mark text as comments.\n' +
  96. 'lala=whatever\n' +
  97. 'website = whatever\n' +
  98. 'language = whatever\n' +
  99. '# The backslash below tells the application to continue reading\n' +
  100. '# the value onto the next line.\n' +
  101. 'message = whatever\n' +
  102. '# Add spaces to the key\n' +
  103. 'key\\ with\\ spaces = whatever\n' +
  104. '# Unicode\n' +
  105. 'tab : whatever\n' +
  106. 'long-unicode : whatever\n' +
  107. 'space\\ separator key val \\n three\n' +
  108. 'another-test :whatever\n' +
  109. ' null-prop'
  110. );
  111. });
  112. // Escaping keys
  113. var editor4 = prop.createEditor();
  114. editor4.set("space test", "value");
  115. editor4.set("equals=key", "value");
  116. editor4.set("colon:key", "value");
  117. editor4.set("new\nline", "value");
  118. editor4.set("üñîçø∂é", "value");
  119. assert.equal(
  120. editor4.toString(),
  121. "space\\ test=value\n" +
  122. "equals\\=key=value\n" +
  123. "colon\\:key=value\n" +
  124. "new\\nline=value\n" +
  125. "\\u00fc\\u00f1\\u00ee\\u00e7\\u00f8\\u2202\\u00e9=value"
  126. );
  127. // java ReadProperties test-cases.properties
  128. // javac ReadProperties.java