git.dumitru.net fructose / master src / tests / parser / combinators / opt.test.ts
master

Tree @master (Download .tar.gz)

opt.test.ts @masterraw · history · blame

import { matchChar, opt } from "../../../parser/Parsers";
import { doc, loc, val } from "../../utils";

describe("opt combinator", () => {
  const p0 = opt(() => matchChar("a"));

  test("has a descriptive name", () => {
    expect(p0.name).toEqual("\"a\"?");
  });

  test("parses matching input", () => {
    expect(
      p0.parse(doc("a"), loc(0, 0)))
    .toEqual(
      val("a", loc(0, 1)));
  });

  test("returns a null value in case no match is possible", () => {
    expect(
      p0.parse(doc("b"), loc(0, 0)))
    .toHaveProperty(
      "value", null);
  });

  test("does not advance input in case no match is possible", () => {
    expect(
      p0.parse(doc("b"), loc(0, 0)))
    .toHaveProperty(
      "location", loc(0, 0));
  });
});