Skip to content

Commit

Permalink
Merge pull request #68 from spiltcoffee/raws
Browse files Browse the repository at this point in the history
feat(grammar): capturing raws
  • Loading branch information
spiltcoffee authored Mar 12, 2019
2 parents fec0351 + fe32b1f commit 8abc5eb
Show file tree
Hide file tree
Showing 30 changed files with 1,096 additions and 506 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.dfm eol=crlf
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ module.exports = {
"^.+\\.tsx?$": "ts-jest"
},
collectCoverage: true,
collectCoverageFrom: ["**/*.ts", "!**/node_modules/**", "!**/dist/**"],
collectCoverageFrom: [
"**/*.ts",
"!**/node_modules/**",
"!**/dist/**",
"!packages/@postdfm/dfm2ast/src/grammar.ts"
],
coverageReporters: ["lcov", "text"],
coverageDirectory: "coverage",
rootDir: "./"
Expand Down
6 changes: 3 additions & 3 deletions packages/@postdfm/ast/__test__/item.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as AST from "../src";

describe("creating Items", () => {
test("empty Item", () => {
const node = new AST.Item([]);
const node = new AST.Item();

expect(node.properties.length).toBe(0);
expect(node.properties).toHaveLength(0);
});

test("Item", () => {
Expand All @@ -14,6 +14,6 @@ describe("creating Items", () => {
);
const node = new AST.Item([propertyNode]);

expect(node.properties).toContain(propertyNode);
expect(node.properties).toContainEqual(propertyNode);
});
});
16 changes: 8 additions & 8 deletions packages/@postdfm/ast/__test__/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as AST from "../src";

describe("creating Lists", () => {
test("empty StringList", () => {
const node = new AST.StringList([]);
const node = new AST.StringList();

expect(node.values.length).toBe(0);
expect(node.values).toHaveLength(0);
});
test("StringList", () => {
const node = new AST.StringList([
Expand All @@ -15,9 +15,9 @@ describe("creating Lists", () => {
expect(node.values).toContainEqual(new AST.StringValue("world"));
});
test("empty QualifiedList", () => {
const node = new AST.QualifiedList([]);
const node = new AST.QualifiedList();

expect(node.values.length).toBe(0);
expect(node.values).toHaveLength(0);
});
test("QualifiedList", () => {
const node = new AST.QualifiedList([
Expand All @@ -28,17 +28,17 @@ describe("creating Lists", () => {
expect(node.values).toContainEqual(new AST.QualifiedValue("enWorld"));
});
test("empty ItemList", () => {
const node = new AST.ItemList([]);
const node = new AST.ItemList();

expect(node.values.length).toBe(0);
expect(node.values).toHaveLength(0);
});
test("ItemList", () => {
const itemNode1 = new AST.Item([]);
const itemNode1 = new AST.Item();
const itemNode2 = new AST.Item([
new AST.Property("Font.Name", new AST.StringValue("sans-serif"))
]);
const node = new AST.ItemList([itemNode1, itemNode2]);

expect(node.values).toContain(itemNode2);
expect(node.values).toContainEqual(itemNode2);
});
});
Original file line number Diff line number Diff line change
@@ -1,109 +1,92 @@
import * as AST from "../src";

describe("creating FormObjects", () => {
test("empty FormObject", () => {
const node = new AST.FormObject(
AST.ObjectKind.Inherited,
"MyForm",
"TMyForm"
);

expect(node.kind).toBe(AST.ObjectKind.Inherited);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBeUndefined();
expect(node.properties).toHaveLength(0);
expect(node.children).toHaveLength(0);
});

test("FormObject with order", () => {
const node = new AST.FormObject(
AST.ObjectKind.Inline,
"MyForm",
"TMyForm",
0
);

expect(node.kind).toBe(AST.ObjectKind.Inline);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBe(0);
expect(node.properties).toHaveLength(0);
expect(node.children).toHaveLength(0);
});

test("FormObject with properties", () => {
const propertyNode = new AST.Property(
"Font.Name",
new AST.StringValue("sans-serif")
);

const node = new AST.FormObject(
AST.ObjectKind.Object,
"MyForm",
"TMyForm",
undefined,
[propertyNode]
);

expect(node.kind).toBe(AST.ObjectKind.Object);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBeUndefined();
expect(node.properties).toContainEqual(propertyNode);
expect(node.children).toHaveLength(0);
});

test("FormObject with children", () => {
const childNode = new AST.FormObject(
AST.ObjectKind.Object,
"MyEdit",
"TEdit"
);

const node = new AST.FormObject(
AST.ObjectKind.Object,
"MyForm",
"TMyForm",
undefined,
undefined,
[childNode]
);

expect(node.kind).toBe(AST.ObjectKind.Object);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBeUndefined();
expect(node.properties).toHaveLength(0);
expect(node.children).toContainEqual(childNode);
});

test("FormObject with a bit of everything", () => {
const propertyNode = new AST.Property(
"Font.Name",
new AST.StringValue("sans-serif")
);

const childNode = new AST.FormObject(
AST.ObjectKind.Object,
"MyEdit",
"TEdit"
);

const node = new AST.FormObject(
AST.ObjectKind.Object,
"MyForm",
"TMyForm",
0,
[propertyNode],
[childNode]
);

expect(node.kind).toBe(AST.ObjectKind.Object);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBe(0);
expect(node.properties).toContainEqual(propertyNode);
expect(node.children).toContainEqual(childNode);
});
});
import * as AST from "../src";

describe("creating FormObjects", () => {
test("empty FormObject", () => {
const node = new AST.DObject(AST.ObjectKind.Inherited, "MyForm", "TMyForm");

expect(node.kind).toBe(AST.ObjectKind.Inherited);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBeUndefined();
expect(node.properties).toHaveLength(0);
expect(node.children).toHaveLength(0);
});

test("FormObject with order", () => {
const node = new AST.DObject(AST.ObjectKind.Inline, "MyForm", "TMyForm", 0);

expect(node.kind).toBe(AST.ObjectKind.Inline);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBe(0);
expect(node.properties).toHaveLength(0);
expect(node.children).toHaveLength(0);
});

test("FormObject with properties", () => {
const propertyNode = new AST.Property(
"Font.Name",
new AST.StringValue("sans-serif")
);

const node = new AST.DObject(
AST.ObjectKind.Object,
"MyForm",
"TMyForm",
undefined,
[propertyNode]
);

expect(node.kind).toBe(AST.ObjectKind.Object);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBeUndefined();
expect(node.properties).toContainEqual(propertyNode);
expect(node.children).toHaveLength(0);
});

test("FormObject with children", () => {
const childNode = new AST.DObject(AST.ObjectKind.Object, "MyEdit", "TEdit");

const node = new AST.DObject(
AST.ObjectKind.Object,
"MyForm",
"TMyForm",
undefined,
undefined,
[childNode]
);

expect(node.kind).toBe(AST.ObjectKind.Object);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBeUndefined();
expect(node.properties).toHaveLength(0);
expect(node.children).toContainEqual(childNode);
});

test("FormObject with a bit of everything", () => {
const propertyNode = new AST.Property(
"Font.Name",
new AST.StringValue("sans-serif")
);

const childNode = new AST.DObject(AST.ObjectKind.Object, "MyEdit", "TEdit");

const node = new AST.DObject(
AST.ObjectKind.Object,
"MyForm",
"TMyForm",
0,
[propertyNode],
[childNode]
);

expect(node.kind).toBe(AST.ObjectKind.Object);
expect(node.name).toBe("MyForm");
expect(node.type).toBe("TMyForm");
expect(node.order).toBe(0);
expect(node.properties).toContainEqual(propertyNode);
expect(node.children).toContainEqual(childNode);
});
});
24 changes: 24 additions & 0 deletions packages/@postdfm/ast/__test__/root.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as AST from "../src";

describe("creating FormObjects", () => {
test("empty Root", () => {
const node = new AST.Root();

expect(node.child).toBeUndefined();
});

test("empty FormObject", () => {
const objectNode = new AST.DObject(
AST.ObjectKind.Inherited,
"MyForm",
"TMyForm"
);

const node = new AST.Root(objectNode);

expect(node.child).toBeDefined();
if (node.child) {
expect(node.child.kind).toBe(AST.ObjectKind.Inherited);
}
});
});
34 changes: 32 additions & 2 deletions packages/@postdfm/ast/__test__/value.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
import * as AST from "../src";

describe("creating Values", () => {
test("empty StringValue", () => {
const node = new AST.StringValue();
expect(node.value).toBe("");
});

test("StringValue", () => {
const node = new AST.StringValue("hello world");
expect(node.value).toBe("hello world");
});

test("empty HexStringValue", () => {
const node = new AST.HexStringValue();
expect(node.value).toBe("");
});

test("HexStringValue", () => {
const node = new AST.HexStringValue("FFFFFFFF");
expect(node.value).toBe("FFFFFFFF");
});

test("empty IntegerValue", () => {
const node = new AST.IntegerValue();
expect(node.value).toBe(0);
});

test("IntegerValue", () => {
const node = new AST.IntegerValue(1337);
expect(node.value).toBe(1337);
});

test("empty DoubleValue", () => {
const node = new AST.DoubleValue();

expect(node.value).toEqual({ integer: "0" });
});

test("DoubleValue", () => {
const node = new AST.DoubleValue(420.69);
expect(node.value).toBe(420.69);
const node = new AST.DoubleValue({
exponent: "1337",
fraction: "000069",
integer: "420"
});

expect(node.value).toEqual({
exponent: "1337",
fraction: "000069",
integer: "420"
});
});

test("BooleanValue", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/@postdfm/ast/src/astNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ASTType } from "./astType";

export class ASTNode {
public astType: ASTType;

// define?
public raws: any;
constructor(astType: ASTType) {
this.astType = astType;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@postdfm/ast/src/astType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export enum ASTType {
QualifiedList = "qualifiedList",
ItemList = "itemList",
Property = "property",
Object = "object"
Object = "object",
Root = "root"
}
Loading

0 comments on commit 8abc5eb

Please sign in to comment.