Skip to content

Commit

Permalink
Merge 98eb428 into aa7c926
Browse files Browse the repository at this point in the history
  • Loading branch information
RauliL committed Mar 24, 2023
2 parents aa7c926 + 98eb428 commit 19ac76f
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 24 deletions.
3 changes: 2 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"collectCoverageFrom": [
"<rootDir>/src/**/*.ts",
"!<rootDir>/src/**/*/index.ts"
"!<rootDir>/src/**/*/index.ts",
"!<rootDir>/src/index.ts"
],
"preset": "ts-jest"
}
56 changes: 39 additions & 17 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { ExitStatus } from "./status";

describe("class Context", () => {
describe("#cwd", () => {
it("should return current working directory by default", () => {
expect(new Context().cwd).toEqual(process.cwd());
});
it("should return current working directory by default", () =>
expect(new Context().cwd).toEqual(process.cwd()));

it("should allow changing of current working directory", () => {
const context = new Context();
Expand All @@ -23,11 +22,10 @@ describe("class Context", () => {
expect(context.cwd).toEqual(rootDir);
});

it("should throw error when trying to set non-existing directory as cwd", () => {
it("should throw error when trying to set non-existing directory as cwd", () =>
expect(() => {
new Context().cwd = path.join(process.cwd(), "non-existent");
}).toThrow();
});
}).toThrow());

it("should treat '-' as previous working directory stored in $OLDPWD", () => {
const context = new Context();
Expand All @@ -53,11 +51,37 @@ describe("class Context", () => {
});
});

describe("#path", () => {
it("should return empty path by default", () => {
expect(new Context().path).toHaveLength(0);
describe("#home", () => {
it("should return environment variable $HOME by default", () => {
const context = new Context();

context.environment.HOME = "/test";

expect(context.home).toEqual("/test");
});

it("should fallback to user's real home directory", () =>
expect(new Context().home).toEqual(os.homedir()));

it("should allow changing home directory", () => {
const context = new Context();
const libDir = path.join(process.cwd(), "lib");

context.home = libDir;

expect(context.environment).toHaveProperty("HOME", libDir);
});

it("should throw error when trying to set non-existing directory as home directory", () =>
expect(() => {
new Context().home = path.join(process.cwd(), "non-existent");
}).toThrow());
});

describe("#path", () => {
it("should return empty path by default", () =>
expect(new Context().path).toHaveLength(0));

it("should allow array as new path", () => {
const context = new Context();

Expand Down Expand Up @@ -85,20 +109,18 @@ describe("class Context", () => {
});
});

describe("#resolveExecutable()", () => {
it("should resolve no non-absolute executables by default", () => {
expect(new Context().resolveExecutable("ls")).toBeNull();
});
describe("resolveExecutable()", () => {
it("should resolve no non-absolute executables by default", () =>
expect(new Context().resolveExecutable("ls")).toBeNull());

it("should resolve absolute executables", () => {
const exe = path.join(process.cwd(), "bin", "juokse");

expect(new Context().resolveExecutable(exe)).toEqual(exe);
});

it("should resolve builtin commands", () => {
expect(typeof new Context().resolveExecutable("!")).toBe("function");
});
it("should resolve builtin commands", () =>
expect(typeof new Context().resolveExecutable("!")).toBe("function"));

it("should resolve executables", () => {
const context = new Context();
Expand All @@ -111,7 +133,7 @@ describe("class Context", () => {
});
});

describe("#execute()", () => {
describe("execute()", () => {
it("should reject non-existing executables", () =>
expect(new Context().execute("non-existent")).rejects.toBeInstanceOf(
Error
Expand Down
55 changes: 49 additions & 6 deletions src/lexer/stack.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
import { Stack } from "./stack";

describe("class Stack", () => {
it("should throw an exception if attempting to remove item from empty stack", () => {
const stack = new Stack();
describe("empty()", () => {
it("should return `true` when the stack is empty", () =>
expect(new Stack().empty()).toBe(true));

expect(() => stack.pop()).toThrowError();
it("should return `false` when the stack is not empty", () => {
const stack = new Stack();

stack.push("test");

expect(stack.empty()).toBe(false);
});
});

it("should throw an exception if attempting to access item from empty stack", () => {
const stack = new Stack();
describe("push()", () => {
it("should insert elements into the stack", () => {
const stack = new Stack();

stack.push("foo");
stack.push("bar");

expect(stack).toHaveProperty(["elements", 0], "foo");
expect(stack).toHaveProperty(["elements", 1], "bar");
});
});

describe("pop()", () => {
it("should throw error if the stack is empty", () =>
expect(() => new Stack().pop()).toThrow());

it("should remove and return the top element from the stack", () => {
const stack = new Stack();

stack.push("foo");
stack.push("bar");

expect(stack.pop()).toBe("bar");
expect(stack).toHaveProperty(["elements", "length"], 1);
});
});

describe("top()", () => {
it("should throw error if the stack is empty", () =>
expect(() => new Stack().top()).toThrow());

it("should return the top element from the stack", () => {
const stack = new Stack();

stack.push("foo");
stack.push("bar");

expect(() => stack.top()).toThrowError();
expect(stack.top()).toBe("bar");
expect(stack).toHaveProperty(["elements", "length"], 2);
});
});
});

0 comments on commit 19ac76f

Please sign in to comment.