Scanner.spec.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const Scanner = require("../../gramjs/extensions/Scanner");
  2. const helloScanner = new Scanner("Hello world");
  3. describe("Scanner", () => {
  4. beforeEach(() => helloScanner.reset());
  5. test("it should construct a new Scanner", () => {
  6. expect(helloScanner.str).toEqual("Hello world");
  7. expect(helloScanner.pos).toEqual(0);
  8. expect(helloScanner.lastMatch).toBeNull();
  9. });
  10. describe(".chr", () => {
  11. test("it should return the character at the current pos", () => {
  12. expect(helloScanner.chr).toEqual("H");
  13. });
  14. });
  15. describe(".peek", () => {
  16. test("it should return the character at the current pos", () => {
  17. expect(helloScanner.peek()).toEqual("H");
  18. });
  19. test("it should return the next n characters", () => {
  20. expect(helloScanner.peek(3)).toEqual("Hel");
  21. expect(helloScanner.peek(5)).toEqual("Hello");
  22. });
  23. });
  24. describe(".consume", () => {
  25. test("it should consume the current character", () => {
  26. const char = helloScanner.consume();
  27. expect(char).toEqual("H");
  28. expect(helloScanner.pos).toEqual(1);
  29. });
  30. test("it should consume the next n characters", () => {
  31. const chars = helloScanner.consume(5);
  32. expect(chars).toEqual("Hello");
  33. expect(helloScanner.pos).toEqual(5);
  34. });
  35. });
  36. describe(".reverse", () => {
  37. test("it should set pos back n characters", () => {
  38. helloScanner.consume(5);
  39. helloScanner.reverse(5);
  40. expect(helloScanner.pos).toEqual(0);
  41. });
  42. test("it should not go back further than 0", () => {
  43. helloScanner.reverse(10);
  44. expect(helloScanner.pos).toEqual(0);
  45. });
  46. });
  47. describe(".scanUntil", () => {
  48. test("it should scan the string for a regular expression starting at the current pos", () => {
  49. helloScanner.scanUntil(/w/);
  50. expect(helloScanner.pos).toEqual(6);
  51. });
  52. test("it should do nothing if the pattern is not found", () => {
  53. helloScanner.scanUntil(/G/);
  54. expect(helloScanner.pos).toEqual(0);
  55. });
  56. });
  57. describe(".rest", () => {
  58. test("it should return the unconsumed input", () => {
  59. helloScanner.consume(6);
  60. expect(helloScanner.rest).toEqual("world");
  61. });
  62. });
  63. describe(".reset", () => {
  64. test("it should reset the pos to 0", () => {
  65. helloScanner.consume(5);
  66. helloScanner.reset();
  67. expect(helloScanner.pos).toEqual(0);
  68. });
  69. });
  70. describe(".eof", () => {
  71. test("it should return true if the scanner has reached the end of the input", () => {
  72. expect(helloScanner.eof()).toBe(false);
  73. helloScanner.consume(11);
  74. expect(helloScanner.eof()).toBe(true);
  75. });
  76. });
  77. describe(".bof", () => {
  78. test("it should return true if pos is 0", () => {
  79. expect(helloScanner.bof()).toBe(true);
  80. helloScanner.consume(11);
  81. expect(helloScanner.bof()).toBe(false);
  82. });
  83. });
  84. });