Parser.test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*globals describe it */
  2. require("should");
  3. var Parser = require("../");
  4. var testdata = [
  5. {
  6. name: "simple string",
  7. states: {
  8. "start": {
  9. "[d-gm-rv]+": function(match, index) {
  10. if(!this.data) this.data = [];
  11. this.data.push({
  12. match: match,
  13. index: index
  14. });
  15. }
  16. }
  17. },
  18. string: "abcdefghijklmnopqrstuvwxyz",
  19. expected: {
  20. data: [
  21. { match: "defg", index: 3 },
  22. { match: "mnopqr", index: 12 },
  23. { match: "v", index: 21 }
  24. ]
  25. }
  26. },
  27. {
  28. name: "state switing",
  29. states: {
  30. "number": {
  31. "([0-9]+)": function(match, number) {
  32. if(!this.data) this.data = {};
  33. this.data[this.ident] = +number;
  34. delete this.ident;
  35. return "start";
  36. },
  37. "-\\?": true,
  38. "\\?": "start"
  39. },
  40. "start": {
  41. "([a-z]+)": function(match, name) {
  42. this.ident = name;
  43. return "number";
  44. }
  45. }
  46. },
  47. string: "a 1 b 2 c f 3 d ? e -? 4",
  48. expected: {
  49. data: {
  50. a: 1, b: 2, c: 3, e: 4
  51. }
  52. }
  53. },
  54. {
  55. name: "state array",
  56. states: {
  57. "start": [
  58. { "a": function() { this.a = true; } },
  59. {
  60. "b": function() { this.b = true; },
  61. "c": function() { this.c = true; }
  62. }
  63. ]
  64. },
  65. string: "hello abc",
  66. expected: {
  67. a: true, b: true, c: true
  68. }
  69. },
  70. {
  71. name: "reference other states",
  72. states: {
  73. "start": [
  74. { "a": function() { this.a = true; } },
  75. "bc"
  76. ],
  77. "bc": {
  78. "b": function() { this.b = true; },
  79. "c": function() { this.c = true; }
  80. }
  81. },
  82. string: "hello abc",
  83. expected: {
  84. a: true, b: true, c: true
  85. }
  86. }
  87. ];
  88. describe("Parser", function() {
  89. testdata.forEach(function(testcase) {
  90. it("should parse " + testcase.name, function() {
  91. var parser = new Parser(testcase.states);
  92. var actual = parser.parse("start", testcase.string, {});
  93. actual.should.be.eql(testcase.expected);
  94. });
  95. });
  96. it("should default context to empty object", function() {
  97. var parser = new Parser({
  98. "a": {
  99. "a": function() {
  100. this.should.be.eql({});
  101. }
  102. }
  103. });
  104. var result = parser.parse("a", "a");
  105. result.should.be.eql({});
  106. });
  107. it("should error for unexpected format", function() {
  108. (function() {
  109. var parser = new Parser({
  110. "a": 123
  111. });
  112. return parser;
  113. }).should.throw();
  114. });
  115. it("should error for not existing state", function() {
  116. var parser = new Parser({
  117. "a": {
  118. "a": "b"
  119. }
  120. });
  121. (function() {
  122. return parser.parse("a", "a");
  123. }).should.throw();
  124. });
  125. });