Skip to content

Commit

Permalink
We got a basic test working!
Browse files Browse the repository at this point in the history
  • Loading branch information
faassen committed Jul 18, 2020
1 parent 889cabb commit 85525d8
Show file tree
Hide file tree
Showing 6 changed files with 2,628 additions and 49 deletions.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"license": "MIT",
"private": false,
"devDependencies": {
"@types/jest": "^26.0.4",
"@types/p5": "^0.9.0",
"jest": "^26.1.0",
"ts-jest": "^26.1.3",
"ts-loader": "^6.2.1",
"tslint": "^5.20.1",
"tslint-loader": "^3.5.4",
Expand All @@ -19,7 +22,8 @@
"p5": "^0.8.0"
},
"scripts": {
"build": "webpack",
"add_simulation": "func() { cp pages/template.html \"pages/$1.html\" && cp src/entry_points/template.ts src/entry_points/$1.ts; }; func"
"build": "webpack",
"test": "jest",
"add_simulation": "func() { cp pages/template.html \"pages/$1.html\" && cp src/entry_points/template.ts src/entry_points/$1.ts; }; func"
}
}
34 changes: 34 additions & 0 deletions src/entry_points/domain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
LSystemRule,
LSystemRuleMap,
Environment,
Tree,
TreeNode,
TransportRule,
TransportRuleMap,
RuleName,
ResourceCost,
} from "./domain";

test("LSystem layer works properly", () => {
class TestEnvironment extends Environment {
costs(ruleNames: RuleName[]): ResourceCost {
return { water: 0, energy: 0 };
}
}
const environment = new TestEnvironment();
const lSystemRuleMap = new Map<RuleName, LSystemRule>();
lSystemRuleMap.set("A", new LSystemRule("A", ["A", "B"]));
lSystemRuleMap.set("B", new LSystemRule("B", ["A"]));

const transportRuleMap = new Map<RuleName, TransportRule>();

const tree = new Tree(lSystemRuleMap, transportRuleMap, environment);
const rootNode = new TreeNode("A", null);

rootNode.update(tree);

expect(rootNode.children.length).toBe(2);
expect(rootNode.children[0].name).toBe("A");
expect(rootNode.children[1].name).toBe("B");
});
32 changes: 16 additions & 16 deletions src/entry_points/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ state: energy and amount
Environmental Layer
root is the source of W R
end nodes evaporate W
root is the source of Water
end nodes evaporate Water
end nodes (any node?) generate E. Perhaps we could only make certain end nodes of type L generate E.
perhaps other nodes (for the stem? can still generate a little bit to have different strategies, or we could have leaves, needles, etc)
Expand Down Expand Up @@ -64,33 +64,33 @@ A -> BC
*/

type RuleName = string;
export type RuleName = string;

type ResourceCost = {
export type ResourceCost = {
water: number;
energy: number;
};

class Environment {
export class Environment {
costs(ruleNames: RuleName[]): ResourceCost {
const waterCost = ruleNames.length * 10;
const energyCost = ruleNames.length * 10;
return { water: waterCost, energy: energyCost };
}
}

class Tree {
export class Tree {
constructor(
public lSystemRuleMap: LSystemRuleMap,
public transportRuleMap: TransportRuleMap,
public environment: Environment
) {}
}

class TreeNode {
children: TreeNode[];
water: number;
energy: number;
export class TreeNode {
children: TreeNode[] = [];
water: number = 0;
energy: number = 0;

constructor(public name: RuleName, public parent: TreeNode | null) {}

Expand Down Expand Up @@ -126,21 +126,21 @@ class TreeNode {
}
}

class LSystemRule {
export class LSystemRule {
constructor(public name: RuleName, public product: RuleName[]) {}
}

type ResourceType = "Water" | "Energy";
export type ResourceType = "Water" | "Energy";

type ResourceMap = Map<ResourceType, number>;
export type ResourceMap = Map<ResourceType, number>;

class TransportRule {
export class TransportRule {
constructor(
public name: RuleName,
public parentResourceMap: ResourceMap,
public childResourceMap: ResourceMap
) {}
}

type LSystemRuleMap = Map<RuleName, LSystemRule>;
type TransportRuleMap = Map<RuleName, TransportRule>;
export type LSystemRuleMap = Map<RuleName, LSystemRule>;
export type TransportRuleMap = Map<RuleName, TransportRule>;
19 changes: 6 additions & 13 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,18 @@
"compilerOptions": {
"target": "es2016",
"module": "es2015",
"lib": [
"es2017",
"dom",
"es6"
],
"lib": ["es2017", "dom", "es6"],
"sourceMap": true,
"moduleResolution": "node",
"downlevelIteration": true,
"removeComments": false,
"baseUrl": "./",
"typeRoots": [
"src/@types"
],
"strict": true,
"noImplicitReturns": true,
"strictNullChecks": true,
"noImplicitAny": true,
"noImplicitThis": true
"noImplicitThis": true,
"esModuleInterop": true
},
"include": [
"src/**/*"
]
}
"include": ["src/**/*"]
}
Loading

0 comments on commit 85525d8

Please sign in to comment.