Skip to content

Commit

Permalink
Optimize the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cedx committed Jan 26, 2024
1 parent 3f92d02 commit 67482bc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 70 deletions.
26 changes: 9 additions & 17 deletions test/fast_transformer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,20 @@ describe("FastTransformer", () => {
});

describe("transform()", () => {
const map = new Map([
["should remove the inline comments", "<?= 'Hello World!' ?>"],
["should remove the multi-line comments", "namespace dummy; class Dummy"],
["should remove the single-line comments", "$className = get_class($this); return $className;"],
["should remove the whitespace", "__construct() { $this->property"]
]);

const script = "res/sample.php";
const transformer = new FastTransformer;
after(() => transformer.close());

it("should remove the inline comments", async () => {
const output = await transformer.transform(script);
ok(output.includes("<?= 'Hello World!' ?>"));
});

it("should remove the multi-line comments", async () => {
const output = await transformer.transform(script);
ok(output.includes("namespace dummy; class Dummy"));
});

it("should remove the single-line comments", async () => {
const output = await transformer.transform(script);
ok(output.includes("$className = get_class($this); return $className;"));
});

it("should remove the whitespace", async () => {
for (const [key, value] of map) it(key, async () => {
const output = await transformer.transform(script);
ok(output.includes("__construct() { $this->property"));
ok(output.includes(value));
});
});
});
46 changes: 10 additions & 36 deletions test/gulp/plugin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,28 @@ import {Plugin, TransformMode} from "#phpMinifier";
describe("Plugin", () => {
describe("_transform()", () => {
const script = {path: resolve("res/sample.php")};
const map = new Map([
["should remove the inline comments", "<?= 'Hello World!' ?>"],
["should remove the multi-line comments", "namespace dummy; class Dummy"],
["should remove the single-line comments", "$className = get_class($this); return $className;"],
["should remove the whitespace", "__construct() { $this->property"]
]);

describe("TransformMode.fast", () => {
const plugin = new Plugin({mode: TransformMode.fast, silent: true});
after(() => plugin.emit("end"));

it("should remove the inline comments", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("<?= 'Hello World!' ?>"));
})));

it("should remove the multi-line comments", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("namespace dummy; class Dummy"));
})));

it("should remove the single-line comments", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("$className = get_class($this); return $className;"));
})));

it("should remove the whitespace", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
for (const [key, value] of map) it(key, () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("__construct() { $this->property"));
ok(chunk.contents.toString().includes(value));
})));
});

describe("TransformMode.safe", () => {
const plugin = new Plugin({mode: TransformMode.safe, silent: true});
after(() => plugin.emit("end"));

it("should remove the inline comments", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("<?= 'Hello World!' ?>"));
})));

it("should remove the multi-line comments", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("namespace dummy; class Dummy"));
})));

it("should remove the single-line comments", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("$className = get_class($this); return $className;"));
})));

it("should remove the whitespace", () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
for (const [key, value] of map) it(key, () => doesNotReject(plugin._transform(new Vinyl(script), "utf8", (error, chunk) => {
ifError(error);
ok(chunk.contents.toString().includes("__construct() { $this->property"));
ok(chunk.contents.toString().includes(value));
})));
});
});
Expand Down
26 changes: 9 additions & 17 deletions test/safe_transformer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,20 @@ describe("SafeTransformer", () => {
});

describe("transform()", () => {
const map = new Map([
["should remove the inline comments", "<?= 'Hello World!' ?>"],
["should remove the multi-line comments", "namespace dummy; class Dummy"],
["should remove the single-line comments", "$className = get_class($this); return $className;"],
["should remove the whitespace", "__construct() { $this->property"]
]);

const script = "res/sample.php";
const transformer = new SafeTransformer;
after(() => transformer.close());

it("should remove the inline comments", async () => {
const output = await transformer.transform(script);
ok(output.includes("<?= 'Hello World!' ?>"));
});

it("should remove the multi-line comments", async () => {
const output = await transformer.transform(script);
ok(output.includes("namespace dummy; class Dummy"));
});

it("should remove the single-line comments", async () => {
const output = await transformer.transform(script);
ok(output.includes("$className = get_class($this); return $className;"));
});

it("should remove the whitespace", async () => {
for (const [key, value] of map) it(key, async () => {
const output = await transformer.transform(script);
ok(output.includes("__construct() { $this->property"));
ok(output.includes(value));
});
});
});

0 comments on commit 67482bc

Please sign in to comment.