Skip to content

Commit

Permalink
Merge pull request #49 from jyoo980/fix-dirdecl
Browse files Browse the repository at this point in the history
Fix Tests
  • Loading branch information
scveloso committed Oct 13, 2019
2 parents 09f0da0 + 1468ca2 commit 4f2a9d5
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 56 deletions.
6 changes: 3 additions & 3 deletions src/ast/DirDecl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class DirDecl extends Content {
tabLevel: number;

public parse(context: Tokenizer): any {
this.tabLevel = context.getCurrentLineTabLevel() === 0 ? -1 : context.getCurrentLineTabLevel();
this.tabLevel = context.getCurrentLineTabLevel();

context.getAndCheckNext("dir");
this.directoryName = context.getNext();
Expand All @@ -29,7 +29,7 @@ export class DirDecl extends Content {
protected parseContents(context: Tokenizer): any {
this.contents = [];

do {
while (context.getCurrentLineTabLevel() > this.tabLevel && !context.checkToken("NO_MORE_TOKENS")) {
let contentDecl: Content;

if (context.checkToken("dir")) {
Expand All @@ -44,7 +44,7 @@ export class DirDecl extends Content {

contentDecl.parse(context);
this.contents.push(contentDecl);
} while (context.getCurrentLineTabLevel() > this.tabLevel && !context.checkToken("NO_MORE_TOKENS"))
}
}

public evaluate(): any {
Expand Down
4 changes: 2 additions & 2 deletions src/ast/errors/ASTErrors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class ValidationError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, ValidationError);
Object.setPrototypeOf(this, new.target.prototype);
}
}
}
12 changes: 11 additions & 1 deletion src/ast/symbols/TypeTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { TypeNode, SyntaxKind } from "typescript";
export class TypeCheckError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, TypeCheckError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

Expand Down Expand Up @@ -87,4 +87,14 @@ export class TypeTable {
public areValidTypes(candidates: string[]): boolean {
return candidates.every((candidate) => this.isValidType(candidate));
}

// NOTE: Should be used for testing only
public resetTypeTable() {
this.table = new Map([
["number", new TypeNodePair("number")],
["boolean", new TypeNodePair("boolean")],
["string", new TypeNodePair("string")],
["void", new TypeNodePair("void")]
]);
}
}
10 changes: 5 additions & 5 deletions src/util/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,35 @@ import * as fs from "fs-extra";
export class FileReadError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, FileReadError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class FileWriteError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, FileWriteError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class FileDeleteError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, FileDeleteError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class DirectoryWriteError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, DirectoryWriteError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class PathExistsError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, PathExistsError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/util/Tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import FileSystem from "./FileSystem";
export class TokenizerError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, TokenizerError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

export class ParseError extends Error {
constructor(...args: any[]) {
super(...args);
Error.captureStackTrace(this, ParseError);
Object.setPrototypeOf(this, new.target.prototype);
}
}

Expand Down Expand Up @@ -122,7 +122,6 @@ export class Tokenizer {
// 5. split on reservedword
this.tokens.push(line.split(Tokenizer.reservedTokenWord).filter((str) => str !==''));
});
console.log(this.tokens);
}

/**
Expand Down
17 changes: 0 additions & 17 deletions test/ast/ClassDecl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@ describe("ClassDecl parse test", () => {

const DUMMY_ROOT_DIR: string = ".";


it("parses single-line, simple class definition", () => {
let tokenizer : Tokenizer = new Tokenizer("classDeclSimple.txt", "./test/testFiles");
let classDec : ClassDecl = new ClassDecl(DUMMY_ROOT_DIR);
classDec = classDec.parse(tokenizer);
expect(classDec.isAbstract).to.be.true;
expect(classDec.className).to.equal("Time");
expect(classDec.implementsNodes).to.be.undefined;
expect(classDec.extendsNodes.parentName).to.equal("TimeClass");
expect(classDec.comments).to.be.undefined;
expect(classDec.fields.length).to.equal(0);
expect(classDec.functions.length).to.equal(0);
let typeTable : TypeTable = TypeTable.getInstance();
expect(typeTable.table.size).to.equal(5);
expect(typeTable.getTypeNode(classDec.className)).to.not.be.undefined;
});

it("parses complex class definition", () => {
let tokenizer : Tokenizer = new Tokenizer("classDeclComplex.txt", "./test/testFiles");
let classDec : ClassDecl = new ClassDecl(DUMMY_ROOT_DIR);
Expand Down
3 changes: 2 additions & 1 deletion test/ast/FieldDecl.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ParseError, Tokenizer} from "../../src/util/Tokenizer";
import {FieldDecl} from "../../src/ast/FieldDecl";
import {expect} from "chai";
import {TypeCheckError} from "../../src/ast/symbols/TypeTable";
import {TypeCheckError, TypeTable} from "../../src/ast/symbols/TypeTable";

describe("FieldDecl tokenizer test", () => {

Expand Down Expand Up @@ -47,6 +47,7 @@ describe("FieldDecl tokenizer test", () => {

it("should throw a TypeCheckError when it contains types which are undeclared", () => {
const tokenizer: Tokenizer = new Tokenizer("fieldDeclExample.txt", "./test/testFiles");
TypeTable.getInstance().resetTypeTable();
fieldDecl.parse(tokenizer);
expect(() => {
return fieldDecl.typeCheck();
Expand Down
2 changes: 1 addition & 1 deletion test/testFiles/classDeclSimple.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
abstract class Time extends TimeClass
abstract class Time extends TimeClass
12 changes: 6 additions & 6 deletions test/testFiles/simpleProgram.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project ZooProject
modules [ ]
dir src/AnimalModels
interface Animal
fields public [ string name, number age ]
generate getters
generate setters
modules [ ]
dir src/AnimalModels
interface Animal
fields public [ string name, number age ]
generate getters
generate setters
22 changes: 11 additions & 11 deletions test/testFiles/simpleProgramClass.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
project TestProject
modules [ ]
dir src/model
class Time
fields private [ number dayOfWeek, number hour, number minute ]
generate getters
generate setters
fields public [ number date ]
generate getters
function private getDate
params [ string id, string content, string kind ]
returns string
modules [ ]
dir src/model
class Time
fields private [ number dayOfWeek, number hour, number minute ]
generate getters
generate setters
fields public [ number date ]
generate getters
function private getDate
params [ string id, string content, string kind ]
returns string


16 changes: 11 additions & 5 deletions test/util/FileSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ describe("FileSystem read tests", () => {

it("should successfully read contents of a file", () => {
fs = new FileSystem();
let contents = fs.readFileSync("projectstructureex.txt", testDir).toString();
expect(contents).to.equal("dir src\n" +
"\tclass Time\n" +
"\tdir TransitModels\n" +
"dir tests\n");
let contents = fs.readFileSync("programExample.txt", testDir).toString();
expect(contents).to.equal(
"project ts-dsl\n" +
" dir src\n" +
" dir ast\n" +
" dir codegen\n" +
" dir util\n" +
" dir test\n" +
" dir ast\n" +
" dir testFiles\n" +
" dir util\n");
});

it("should throw a FileReadError when reading contents of a file that does not exist", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/util/PackageJson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('PackageJson generation and add modules test', function() {
} catch (err) {
result = err;
} finally {
expect(result).to.equal('npm failed when installing abcabcabcabc with: 404 Not Found - GET https://registry.yarnpkg.com/abcabcabcabc - Not found');
expect(result).to.equal('npm failed when installing abcabcabcabc with: 404 Not Found: abcabcabcabc@latest');
expect(verifiyDependency(module)).to.equal(null);
}
});
Expand Down

0 comments on commit 4f2a9d5

Please sign in to comment.