test-config-codec.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var run = require('./run.js');
  2. // The thing we mean to test.
  3. var codec = require('../lib/config-codec.js');
  4. var sample = '\
  5. [user]\n\
  6. \tname = Tim Caswell\n\
  7. \temail = tim@creationix.com\n\
  8. [core]\n\
  9. \teditor = vim\n\
  10. \twhitespace = fix,-indent-with-non-tab,trailing-space,cr-at-eol\n\
  11. [web]\n\
  12. \tbrowser = google-chrome\n\
  13. [color]\n\
  14. \tui = true\n\
  15. [color "branch"]\n\
  16. \tcurrent = yellow bold\n\
  17. \tlocal = green bold\n\
  18. \tremote = cyan bold\n\
  19. [color "diff"]\n\
  20. \tmeta = yellow bold\n\
  21. \tfrag = magenta bold\n\
  22. \told = red bold\n\
  23. \tnew = green bold\n\
  24. \twhitespace = red reverse\n\
  25. [github]\n\
  26. \tuser = creationix\n\
  27. \ttoken = token';
  28. var config;
  29. run([
  30. function testDecode() {
  31. config = codec.decode(sample);
  32. if (config.user.name !== "Tim Caswell") {
  33. throw new Error("Failed to parse user.name");
  34. }
  35. if (config.color.ui != "true") {
  36. throw new Error("Failed to parse color.ui");
  37. }
  38. if (config.color.diff.meta !== "yellow bold") {
  39. throw new Error("Failed to parse color.diff.meta");
  40. }
  41. },
  42. function testEncode() {
  43. var encoded = codec.encode(config);
  44. var config2 = codec.decode(encoded);
  45. if (JSON.stringify(config) !== JSON.stringify(config2)) {
  46. console.log(config);
  47. console.log(encoded);
  48. console.log(config2);
  49. throw new Error("Encode failed");
  50. }
  51. },
  52. function testEncode2() {
  53. var encoded = codec.encode({
  54. foo: {
  55. bar: {
  56. baz: true
  57. }
  58. }
  59. });
  60. if (encoded !== '[foo "bar"]\n\tbaz = true\n') {
  61. console.log(encoded);
  62. throw new Error("Invalid encoding of single deep config");
  63. }
  64. }
  65. ]);