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

Tree @master (Download .tar.gz)

either.test.ts @masterraw · history · blame

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

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

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

  test("parses either variants if possible", () => {
    expect(
      p0.parse(doc("a"), loc(0, 0)))
    .toEqual(
      val("a", loc(0, 1)));

    expect(
      p0.parse(doc("b"), loc(0, 0)))
    .toEqual(
      val("b", loc(0, 1)));
  });

  test("returns an error in case no match is possible", () => {
    expect(
      p0.parse(doc("c"), loc(0, 0)))
    .toBeInstanceOf(ParserError);
  });

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