12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /* 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 }");
- });
- });
|