Skip to content

Commit

Permalink
add new tests to Type
Browse files Browse the repository at this point in the history
  • Loading branch information
Tengs Penkwe committed Feb 2, 2024
1 parent 7af7677 commit ea3f2f9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
"devDependencies": {
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/sinon": "^17.0.3",
"@typescript-eslint/eslint-plugin": "^6.19.1",
"@typescript-eslint/parser": "^6.19.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"eslint": "8.31.0",
"mocha": "^10.2.0",
"nyc": "15.1.0",
"sinon": "^17.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
Expand Down
19 changes: 10 additions & 9 deletions src/ast/Type.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import {Farm} from "../backend/Farm";
import {Crop} from "../backend/Crop";

// The type of a value
export type Type = number | boolean | Farm | Crop | string | null;
export type TypeStr = "Num" | "Bool" | "Farm" | "Crop" | "String" | "Null";

// The type of an expression
export type ExprTypeStr =
| "Null" // Does nothing
Expand All @@ -23,16 +19,21 @@ export type ExprTypeStr =
| "Lte" // result: Bool
| "Call"; // Function call, result: unknown

// The type of a value
export type Type = number | boolean | Farm | Crop | string | null;
export type TypeStr = "Num" | "Bool" | "Farm" | "Crop" | "String" | "Null";

export function typeToString(type: Type): TypeStr {
if (type instanceof Farm) return "Farm";
if (type instanceof Crop) return "Crop";
if (type === null) return "Null"; // Handle null explicitly
switch (typeof type) {
case "number":
return "Num";
case "boolean":
return "Bool";
case "number": return "Num";
case "boolean": return "Bool";
case "string": return "String"; // Added missing case
default:
throw new Error("Unknown type: " + type);
console.warn("Unknown type: " + type); // Maybe log instead of throwing
return "Null"; // Fallback or throw Error if strict typing is needed
}
}

Expand Down
55 changes: 51 additions & 4 deletions test/ast/Type.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,54 @@
import {expect, use} from "chai";
import { expect } from "chai";
import { typeToString, Result } from "../../src/ast/Type";
import * as sinon from "sinon";

describe("Type System", () => {
describe("typeToString", () => {
it("should correctly identify numbers", () => {
expect(typeToString(42)).to.equal("Num");
});

it("should correctly identify strings", () => {
expect(typeToString("Hello")).to.equal("String");
});

it("should correctly identify boolean values", () => {
expect(typeToString(true)).to.equal("Bool");
expect(typeToString(false)).to.equal("Bool");
});

it("should handle null values", () => {
expect(typeToString(null)).to.equal("Null");
});

});

describe("Result class", () => {
it("should store and return the correct type and value", () => {
const numResult = new Result("Num", 123);
expect(numResult.type).to.equal("Num");
expect(numResult.value).to.equal(123);

const boolResult = new Result("Bool", true);
expect(boolResult.type).to.equal("Bool");
expect(boolResult.value).to.equal(true);
});

it("should display the correct information with show()", () => {
let spy = sinon.spy(console, "log");
const stringResult = new Result("String", "Test");
stringResult.show();
expect(spy.calledWith("Test")).to.be.true;
spy.restore(); // Clean up the spy

spy = sinon.spy(console, "log");
const nullResult = new Result("Null", null);
nullResult.show();
expect(spy.called).to.be.false;
spy.restore(); // Clean up the spy
});

describe("Type", () => {
it("should convert types to strings", () => {
expect("a").to.equal("a");
});

});

0 comments on commit ea3f2f9

Please sign in to comment.