Skip to content

Commit

Permalink
chore: finish tests for async transform
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti committed Apr 5, 2024
1 parent e59ea96 commit 75a99b0
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ allowing `svelte-concurrency` to interrupt your function if you call it mid-exec
## How to write async transform tests?

If you want to write a new test for the async transformation you just need to create a `code.js` file in a new folder in `./src/lib/tests/expected-transforms`. Try to give the folder a descriptive name and the run `pnpm generate-expected`. This will create a series of `transform.js` files in the various folder which will later be used to test the transform. If you are modifying the transform make sure to run the tests before running the `generate-expected` script!

P.s. If, after you run the script, you'll see a folder with `code.js` and no `transform.js` this means that in that case the transform will not apply.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { task } from "svelte-concurrency";

/**
* @param {number} val
*/
function fn(val){
return val;
}

/**
*
* @param {TemplateStringsArray} quasi
* @param {string} strings
*/
function str(quasi, strings){

}

task(async ()=>{
const assign = await Promise.resolve();
const array = [await Promise.resolve()];
const sum = await Promise.resolve(1) + 2;
const sum2 = 2 + await Promise.resolve(1);
const function_call = fn(await Promise.resolve(2));
const conditional1 = await Promise.resolve(true) ? 1 : 2;
const conditional2 = true ? await Promise.resolve(1) : 2;
const conditional3 = false ? 1: await Promise.resolve(2);
const logical1 = await Promise.resolve(null) ?? 3;
const logical2 = null ?? await Promise.resolve(null);
const logical3 = null || await Promise.resolve(null);
const logical4 = await Promise.resolve(null) || null;
const logical5 = await Promise.resolve(null) && null;
const logical6 = true && await Promise.resolve(null);
const object_expression1 = {awaited: await Promise.resolve(2)};
const object_expression2 = {awaited: {nested: await Promise.resolve(2)}};
const object_expression3 = {awaited: {nested: [await Promise.resolve(2)]}};
const tagged_template = str`something ${await Promise.resolve("")}`;
const template_literal = `something ${await Promise.resolve("")}`;
const unary = !(await Promise.resolve(true))
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { task } from "svelte-concurrency";

function fn(val) {
return val;
}

function str(quasi, strings) {}

task(async function* () {
const assign = await Promise.resolve();

yield;

const array = [await Promise.resolve()];

yield;

const sum = await Promise.resolve(1) + 2;

yield;

const sum2 = 2 + await Promise.resolve(1);

yield;

const function_call = fn(await Promise.resolve(2));

yield;

const conditional1 = await Promise.resolve(true) ? 1 : 2;

yield;

const conditional2 = true ? await Promise.resolve(1) : 2;

yield;

const conditional3 = false ? 1 : await Promise.resolve(2);

yield;

const logical1 = await Promise.resolve(null) ?? 3;

yield;

const logical2 = null ?? await Promise.resolve(null);

yield;

const logical3 = null || await Promise.resolve(null);

yield;

const logical4 = await Promise.resolve(null) || null;

yield;

const logical5 = await Promise.resolve(null) && null;

yield;

const logical6 = true && await Promise.resolve(null);

yield;

const object_expression1 = { awaited: await Promise.resolve(2) };

yield;

const object_expression2 = {
awaited: { nested: await Promise.resolve(2) }
};

yield;

const object_expression3 = {
awaited: { nested: [await Promise.resolve(2)] }
};

yield;

const tagged_template = str`something ${await Promise.resolve("")}`;

yield;

const template_literal = `something ${await Promise.resolve("")}`;

yield;

const unary = !await Promise.resolve(true);

yield;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { task } from "svelte-concurrency";

task(async ()=>{
await Promise.resolve();
await Promise.resolve();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { task } from "svelte-concurrency";

task(async function* () {
await Promise.resolve();
yield;
await Promise.resolve();
yield;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { task as other_name } from "svelte-concurrency";
import { task } from "another-library";

task(async ()=>{
await Promise.resolve();
await Promise.resolve();
});

other_name(async ()=>{
await Promise.resolve();
await Promise.resolve();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { task as other_name } from "svelte-concurrency";
import { task } from "another-library";

task(async () => {
await Promise.resolve();
await Promise.resolve();
});

other_name(async function* () {
await Promise.resolve();
yield;
await Promise.resolve();
yield;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { task as other_name } from "svelte-concurrency";

/**
*
* @param {()=>void} fn
*/
function task(fn){
fn();
}

task(async ()=>{
await Promise.resolve();
await Promise.resolve();
});

other_name(async ()=>{
await Promise.resolve();
await Promise.resolve();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { task as other_name } from "svelte-concurrency";

function task(fn) {
fn();
}

task(async () => {
await Promise.resolve();
await Promise.resolve();
});

other_name(async function* () {
await Promise.resolve();
yield;
await Promise.resolve();
yield;
});
6 changes: 6 additions & 0 deletions src/lib/tests/expected-transforms/task-aliased/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { task as other_name } from "svelte-concurrency";

other_name(async ()=>{
await Promise.resolve();
await Promise.resolve();
});
8 changes: 8 additions & 0 deletions src/lib/tests/expected-transforms/task-aliased/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { task as other_name } from "svelte-concurrency";

other_name(async function* () {
await Promise.resolve();
yield;
await Promise.resolve();
yield;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { task } from "another-library";

task(async ()=>{
await Promise.resolve();
await Promise.resolve();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @param {()=>void} fn
*/
function task(fn){
fn();
}

task(async ()=>{
await Promise.resolve();
await Promise.resolve();
});

0 comments on commit 75a99b0

Please sign in to comment.