123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /* tslint:disable:no-unused-expression */
- import { expect } from "chai";
-
- import * as AST from "../src/ast";
- import { isError } from "../src/error";
- import Lexer from "../src/lexer";
- import Parser from "../src/parser";
-
- const parse = (source: string): AST.Statement[] => {
- const tokens = new Lexer(source).scan();
- if (isError(tokens)) {
- throw tokens.message;
- } else {
- const tree = new Parser(tokens).parse();
- if (isError(tree)) {
- throw tree.message;
- }
- return tree;
- }
- };
-
- describe("Parser", () => {
- it("should parse a number selection", () => {
- const tree = parse("select 5");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Number(5)],
- }),
- ]);
- });
-
- it("should parse a multiple number selection", () => {
- const tree = parse("select 5, 6");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Number(5), new AST.Number(6)],
- }),
- ]);
- });
-
- it("should parse a selection of identifiers", () => {
- const tree = parse("select a, b");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Identifier("a"), new AST.Identifier("b")],
- }),
- ]);
- });
-
- it("should parse a selection of identifiers and numbers", () => {
- const tree = parse("select a, 5");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Identifier("a"), new AST.Number(5)],
- }),
- ]);
- });
-
- it("should parse the from of a selection", () => {
- const tree = parse("select a from t");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Identifier("a")],
- from: new AST.Identifier("t"),
- }),
- ]);
- });
-
- it("should parse aliases", () => {
- const tree = parse("select a as b");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [
- new AST.Alias(new AST.Identifier("a"), new AST.Identifier("b")),
- ],
- }),
- ]);
- });
-
- it("should parse aliases as from targets", () => {
- const tree = parse("select a from t as u");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Identifier("a")],
- from: new AST.Alias(new AST.Identifier("t"), new AST.Identifier("u")),
- }),
- ]);
- });
-
- it("should not allow a number as a from target", () => {
- const fn = () => parse("select 1 from 2");
- expect(fn).to.throw("Unexpected token: { kind: NUMBER, value: 2 }");
- });
-
- it("should parse a where with a number", () => {
- const tree = parse("select a from b where 1");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Identifier("a")],
- from: new AST.Identifier("b"),
- where: new AST.Number(1),
- }),
- ]);
- });
-
- it("should parse a where with an identifier", () => {
- const tree = parse("select a from b where c");
- expect(tree).to.deep.equal([
- new AST.SelectStatement({
- arguments: [new AST.Identifier("a")],
- from: new AST.Identifier("b"),
- where: new AST.Identifier("c"),
- }),
- ]);
- });
- });
|