Skip to content

Commit

Permalink
Separate function and generator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xethlyx committed Apr 2, 2024
1 parent 4e6974d commit 2e0d177
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 29 deletions.
29 changes: 0 additions & 29 deletions tests/src/tests/function.spec.ts
Expand Up @@ -274,33 +274,4 @@ export = () => {
const args: unknown[] = ["123", "456"];
expect(select(1, ...args)[1]).to.equal("456");
});

it("should support generator function declarations", () => {
function* a() {
yield 1;
yield 2;
return 3;
}

function* c() {
return 61;
}

function* b() {
yield 0;
yield* c();
const output = yield* a();
yield output;
yield 4;
return 5;
}

const result = [...b()];
expect(result.size()).to.equal(5);
expect(result[0]).to.equal(0);
expect(result[1]).to.equal(1);
expect(result[2]).to.equal(2);
expect(result[3]).to.equal(3);
expect(result[4]).to.equal(4);
});
};
90 changes: 90 additions & 0 deletions tests/src/tests/generator.spec.ts
@@ -0,0 +1,90 @@
export = () => {
it("should support generator function declarations", () => {
function* foo() {
yield 1;
return 2;
}
expect(foo()).to.be.ok();
});

it("should support no return value", () => {
function* foo() {
yield 1;
}

for (const result of foo()) {
expect(result).to.equal(1);
}
});

it("should support multiple yields", () => {
function* foo() {
yield 10;
yield 20;
}

const result = [...foo()];
expect(result.size()).to.equal(2);
expect(result[0]).to.equal(10);
expect(result[1]).to.equal(20);
});

it("should support yield with asterisk token", () => {
function* foo() {
yield 1;
}

function* bar() {
yield* foo();
}

const result = [...bar()];
expect(result.size()).to.equal(1);
expect(result[0]).to.equal(1);
});

it("should not resume finished generator", () => {
function* foo() {
yield 1;
}

function* bar() {
const generated = foo();
yield* generated;
yield* generated;
}

const result = [...bar()];
expect(result.size()).to.equal(1);
expect(result[0]).to.equal(1);
});

it("should properly define yield vs return", () => {
function* a() {
yield 1;
yield 2;
return 3;
}

function* c() {
return 61;
}

function* b() {
yield 0;
yield* c();
const output = yield* a();
yield output;
yield 4;
return 5;
}

const result = [...b()];
expect(result.size()).to.equal(5);
expect(result[0]).to.equal(0);
expect(result[1]).to.equal(1);
expect(result[2]).to.equal(2);
expect(result[3]).to.equal(3);
expect(result[4]).to.equal(4);
});
};

0 comments on commit 2e0d177

Please sign in to comment.