Skip to content

Commit

Permalink
chore(formatting): Fix formatting for tests (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed May 24, 2017
1 parent 9829339 commit ca00355
Show file tree
Hide file tree
Showing 19 changed files with 1,318 additions and 2,636 deletions.
72 changes: 24 additions & 48 deletions packages/babel-plugin-minify-builtins/__tests__/minify-builtins.js
Expand Up @@ -12,46 +12,39 @@ function transform(code) {

describe("minify-builtins", () => {
it("should minify standard built in methods", () => {
const source = unpad(
`
const source = unpad(`
function c() {
let a = 10;
const d = Number.isNaN(a);
Math.max(a, b) + Math.max(b, a);
return d && Number.isFinite(a);
}
`
);
`);
// Jest arranges in alphabetical order, So keeping it as _source
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should minify standard built in properties", () => {
const source = unpad(
`
const source = unpad(`
function a () {
Number.NAN + Number.NAN;
return Math.PI + Math.PI + Number.EPSILON + Number.NAN;
}
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should take no of occurences in to account", () => {
const source = unpad(
`
const source = unpad(`
function a() {
Math.floor(a) + Math.floor(b) + Math.min(a, b);
}
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should collect and minify no matter any depth", () => {
const source = unpad(
`
const source = unpad(`
function a (){
Math.max(b, a);
const b = () => {
Expand All @@ -62,27 +55,23 @@ describe("minify-builtins", () => {
}
}
}
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("shouldn't minify builtins in the program scope to avoid leaking", () => {
const source = unpad(
`
const source = unpad(`
Math.max(c, d)
function a (){
Math.max(b, a) + Math.max(c, d);
}
Math.max(e, f)
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should minify builtins to method scope for class declarations", () => {
const source = unpad(
`
const source = unpad(`
class Test {
foo() {
Math.max(c, d)
Expand All @@ -98,14 +87,12 @@ describe("minify-builtins", () => {
Math.min(c, d)
}
}
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should minify builtins to function scopes ", () => {
const source = unpad(
`
const source = unpad(`
var a = () => {
Math.floor(b);
Math.floor(b);
Expand All @@ -122,14 +109,12 @@ describe("minify-builtins", () => {
Math.floor(d) + Math.floor(d,e);
Math.max(e,d);
})
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should collect and minify in segments in any depth if there is no LCA", () => {
const source = unpad(
`
const source = unpad(`
function b(){
Math.floor(as, bb);
function d(){
Expand Down Expand Up @@ -163,49 +148,40 @@ describe("minify-builtins", () => {
}
};
new A()
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should evalaute expressions if applicable and optimize it", () => {
const source = unpad(
`
const source = unpad(`
const a = Math.max(Math.floor(2), 5);
let b = 1.8;
let x = Math.floor(Math.max(a, b));
foo(x);
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should not evaluate if its side effecty", () => {
const source = unpad(
`
const source = unpad(`
Math.max(foo(), 1);
Math.random();
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should not minify for computed properties", () => {
const source = unpad(
`
const source = unpad(`
let max = "floor";
Math[max](1.5);
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});

it("should not minify for arrow fn without block statment", () => {
const source = unpad(
`
const source = unpad(`
const a = () => Math.floor(b) + Math.floor(b);
`
);
`);
expect({ _source: source, expected: transform(source) }).toMatchSnapshot();
});
});
Expand Up @@ -11,8 +11,7 @@ function transform(code) {

describe("constant-folding-plugin", () => {
it("should evaluate some expressions", () => {
const source = unpad(
`
const source = unpad(`
"a" + "b"
2 * 3;
1/3;
Expand All @@ -21,11 +20,9 @@ describe("constant-folding-plugin", () => {
var x = 1;
foo(x);
"b" + a + "c" + "d" + g + z + "f" + "h" + "z"
`
);
`);

const expected = unpad(
`
const expected = unpad(`
"ab";
6;
1 / 3;
Expand All @@ -34,99 +31,76 @@ describe("constant-folding-plugin", () => {
var x = 1;
foo(x);
"b" + a + "cd" + g + z + "fhz";
`
);
`);
expect(transform(source)).toBe(expected);
});

it("should skip -0", () => {
const source = unpad(
`
const source = unpad(`
-0;
+-0;
+0;
`
);
`);

const expected = unpad(
`
const expected = unpad(`
-0;
-0;
0;
`
);
`);
expect(transform(source)).toBe(expected);
});

it("should handle runtime errors", () => {
const source = unpad(
`
const source = unpad(`
try {
x({
toString: 0
} + '');
} catch (e) {}
`
);
`);
expect(transform(source)).toBe(source);
});

xit("should handle script escape", () => {
const source = unpad(
`
const source = unpad(`
"</" + "script"
`
);
`);

const expected = unpad(
`
const expected = unpad(`
"<\\\\/script";
`
);
`);
expect(transform(source)).toBe(expected);
});

xit("should handle style escape", () => {
const source = unpad(
`
const source = unpad(`
"</" + "style"
`
);
`);

const expected = unpad(
`
const expected = unpad(`
"<\\\\/style";
`
);
`);
expect(transform(source)).toBe(expected);
});

xit("should handle html comment escape", () => {
const source = unpad(
`
const source = unpad(`
"<!" + "--"
`
);
`);

const expected = unpad(
`
const expected = unpad(`
"\\\\x3C!--";
`
);
`);
expect(transform(source)).toBe(expected);
});

it("should fix #440", () => {
const source = unpad(
`
const source = unpad(`
var x = "'cool'" + "test";
`
);
const expected = unpad(
`
`);
const expected = unpad(`
var x = "'cool'test";
`
);
`);
expect(transform(source)).toBe(expected);
});
});

0 comments on commit ca00355

Please sign in to comment.