offset.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var test = require('tape');
  2. var Parser = require('../');
  3. var input = '{\n "string": "value",\n "number": 3,\n "object"';
  4. var input2 = ': {\n "key": "vд"\n },\n "array": [\n -1,\n 12\n ]\n ';
  5. var input3 = '"null": null, "true": true, "false": false, "frac": 3.14 }';
  6. var offsets = [
  7. [ 0, Parser.C.LEFT_BRACE ],
  8. [ 4, Parser.C.STRING ],
  9. [ 12, Parser.C.COLON ],
  10. [ 14, Parser.C.STRING ],
  11. [ 21, Parser.C.COMMA ],
  12. [ 25, Parser.C.STRING ],
  13. [ 33, Parser.C.COLON ],
  14. [ 35, Parser.C.NUMBER ],
  15. [ 36, Parser.C.COMMA ],
  16. [ 40, Parser.C.STRING ],
  17. [ 48, Parser.C.COLON ],
  18. [ 50, Parser.C.LEFT_BRACE ],
  19. [ 54, Parser.C.STRING ],
  20. [ 59, Parser.C.COLON ],
  21. [ 61, Parser.C.STRING ],
  22. [ 69, Parser.C.RIGHT_BRACE ],
  23. [ 70, Parser.C.COMMA ],
  24. [ 74, Parser.C.STRING ],
  25. [ 81, Parser.C.COLON ],
  26. [ 83, Parser.C.LEFT_BRACKET ],
  27. [ 87, Parser.C.NUMBER ],
  28. [ 89, Parser.C.COMMA ],
  29. [ 93, Parser.C.NUMBER ],
  30. [ 98, Parser.C.RIGHT_BRACKET ],
  31. [ 102, Parser.C.STRING ],
  32. [ 108, Parser.C.COLON ],
  33. [ 110, Parser.C.NULL ],
  34. [ 114, Parser.C.COMMA ],
  35. [ 116, Parser.C.STRING ],
  36. [ 122, Parser.C.COLON ],
  37. [ 124, Parser.C.TRUE ],
  38. [ 128, Parser.C.COMMA ],
  39. [ 130, Parser.C.STRING ],
  40. [ 137, Parser.C.COLON ],
  41. [ 139, Parser.C.FALSE ],
  42. [ 144, Parser.C.COMMA ],
  43. [ 146, Parser.C.STRING ],
  44. [ 152, Parser.C.COLON ],
  45. [ 154, Parser.C.NUMBER ],
  46. [ 159, Parser.C.RIGHT_BRACE ]
  47. ];
  48. test('offset', function(t) {
  49. t.plan(offsets.length * 2 + 1);
  50. var p = new Parser();
  51. var i = 0;
  52. p.onToken = function (token) {
  53. t.equal(p.offset, offsets[i][0]);
  54. t.equal(token, offsets[i][1]);
  55. i++;
  56. };
  57. p.write(input);
  58. p.write(input2);
  59. p.write(input3);
  60. t.equal(i, offsets.length);
  61. });