Skip to content

Commit bdb056a

Browse files
committed
Refactor project and add ava
1 parent efcfd1b commit bdb056a

File tree

10 files changed

+232
-70
lines changed

10 files changed

+232
-70
lines changed

.babelrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
{
2-
"presets": ["es2015"]
2+
"presets": ["es2015-loose", "stage-0"],
3+
"plugins": ["add-module-exports"],
4+
"env": {
5+
"development": {
6+
"sourceMaps": "inline"
7+
},
8+
"test": {
9+
"plugins": ["istanbul"]
10+
}
11+
}
312
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
test/fixtures/*.actual.css
3+
dist

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,32 @@
99
"calculation",
1010
"calc"
1111
],
12-
"main": "index.js",
12+
"main": "dist/index.js",
13+
"files": [
14+
"dist",
15+
"LICENSE"
16+
],
1317
"scripts": {
14-
"test": "eslint src/**/*.es6 && npm run build && node tests/test.js",
15-
"build": "babel src -d . && jison parser.jison"
18+
"prepublish": "npm run build && del-cli dist/__tests__",
19+
"build": "del-cli dist && cross-env BABEL_ENV=publish babel src --out-dir dist && jison parser.jison -o dist/parser.js",
20+
"pretest": "eslint src && npm run build",
21+
"test": "ava dist/__tests__/"
1622
},
1723
"author": "Maxime Thirouin",
1824
"license": "MIT",
1925
"repository": "https://github.com/postcss/postcss-calc.git",
2026
"devDependencies": {
27+
"ava": "^0.18.2",
2128
"babel-cli": "^6.18.0",
2229
"babel-core": "^6.21.0",
2330
"babel-eslint": "^7.1.1",
2431
"babel-plugin-add-module-exports": "^0.2.1",
2532
"babel-preset-es2015": "^6.18.0",
33+
"babel-preset-es2015-loose": "^7.0.0",
34+
"babel-preset-stage-0": "^6.3.13",
2635
"babel-register": "^6.18.0",
36+
"cross-env": "^3.1.4",
37+
"del-cli": "^0.2.1",
2738
"eslint": "^3.12.2",
2839
"eslint-plugin-babel": "^4.0.0",
2940
"eslint-plugin-import": "^2.2.0",

src/__tests__/index.js

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
import ava from 'ava';
2+
import reduceCalc from '..';
3+
import postcss from 'postcss';
4+
5+
function test(message, input, output = null, opts = {}, expectedWarnings = []) {
6+
if (output === null)
7+
output = input;
8+
9+
ava(message, t => {
10+
const out = postcss(reduceCalc(opts)).process(input);
11+
t.deepEqual(out.css, output);
12+
let warnings = out.warnings().map(warning => warning.text);
13+
t.deepEqual(warnings.length, expectedWarnings.length);
14+
warnings.forEach((warning, i) => {
15+
t.true(expectedWarnings[i].test(warning));
16+
});
17+
});
18+
}
19+
20+
function testThrows(message, input, expected, opts = {}) {
21+
ava(message, t => {
22+
t.throws(() => postcss(reduceCalc(opts)).process(input).css, expected);
23+
});
24+
}
25+
26+
test(
27+
'should reduce simple calc (1)',
28+
'foo{bar:calc(1px + 1px);baz:bat}',
29+
'foo{bar:2px;baz:bat}'
30+
);
31+
32+
test(
33+
'should reduce simple calc (2)',
34+
'foo{bar:calc(1px + 1px);baz:calc(2px+3px)}',
35+
'foo{bar:2px;baz:5px}'
36+
);
37+
38+
test(
39+
'should reduce simple calc (3)',
40+
'foo{bar:calc(1rem * 1.5)}',
41+
'foo{bar:1.5rem}'
42+
);
43+
44+
test(
45+
'should reduce calc with newline characters',
46+
'foo{bar:calc(\n1rem \n* 2 \n* 1.5)}',
47+
'foo{bar:3rem}'
48+
);
49+
50+
test(
51+
'should preserve calc with incompatible units',
52+
'foo{bar:calc(100% + 1px)}',
53+
'foo{bar:calc(100% + 1px)}'
54+
);
55+
56+
test(
57+
'should parse fractions without leading zero',
58+
'foo{margin:calc(2rem - .14285em)}',
59+
'foo{margin:calc(2rem - 0.14285em)}'
60+
);
61+
62+
test(
63+
'should handle precision correctly (1)',
64+
'foo{bar:calc(1/100)}',
65+
'foo{bar:0.01}'
66+
);
67+
68+
test(
69+
'should handle precision correctly (2)',
70+
'foo{bar:calc(5/1000000)}',
71+
'foo{bar:0.00001}'
72+
);
73+
74+
test(
75+
'should handle precision correctly (3)',
76+
'foo{bar:calc(5/1000000)}',
77+
'foo{bar:0.000005}',
78+
{ precision: 6 }
79+
);
80+
81+
test(
82+
'should ignore media queries',
83+
'@media (min-width:calc(10px+10px)){}',
84+
'@media (min-width:calc(10px+10px)){}'
85+
);
86+
87+
test(
88+
'should reduce calc in media queries when `mediaQueries` option is set to true',
89+
'@media (min-width:calc(10px+10px)){}',
90+
'@media (min-width:20px){}',
91+
{ mediaQueries: true }
92+
);
93+
94+
test(
95+
'should ignore selectors (1)',
96+
'div[data-size="calc(3*3)"]{}',
97+
'div[data-size="calc(3*3)"]{}'
98+
);
99+
100+
test(
101+
'should ignore selectors (2)',
102+
'div:nth-child(2n + calc(3*3)){}',
103+
'div:nth-child(2n + calc(3*3)){}'
104+
);
105+
106+
test(
107+
'should reduce calc in selectors when `selectors` option is set to true (1)',
108+
'div[data-size="calc(3*3)"]{}',
109+
'div[data-size="9"]{}',
110+
{ selectors: true }
111+
);
112+
113+
test(
114+
'should reduce calc in selectors when `selectors` option is set to true (2)',
115+
'div:nth-child(2n + calc(3*3)){}',
116+
'div:nth-child(2n + 9){}',
117+
{ selectors: true }
118+
);
119+
120+
test(
121+
'should preserve the original declaration when `preserve` option is set to true',
122+
'foo{bar:calc(1rem * 1.5)}',
123+
'foo{bar:1.5rem;bar:calc(1rem * 1.5)}',
124+
{ preserve: true }
125+
);
126+
127+
test(
128+
'should not yield warnings when nothing is wrong',
129+
'foo{bar:calc(500px - 0px)}',
130+
'foo{bar:500px}',
131+
{ warnWhenCannotResolve: true }
132+
);
133+
134+
test(
135+
'should warn when calc expression cannot be reduced to a single value',
136+
'foo{bar:calc(100% + 1px)}',
137+
'foo{bar:calc(100% + 1px)}',
138+
{ warnWhenCannotResolve: true },
139+
[ /^Could not reduce expression:/ ]
140+
);
141+
142+
test(
143+
'should produce simpler result (#25)',
144+
'foo{font-size: calc(14px + 6 * ((100vw - 320px) / 448))}',
145+
'foo{font-size: calc(9.71px + 1.34vw)}',
146+
{ precision: 2 }
147+
);
148+
149+
test(
150+
'should reduce mixed units of time (#33)',
151+
'foo{bar:calc(1s - 50ms)}',
152+
'foo{bar:0.95s}'
153+
);
154+
test(
155+
'should not parse variables as calc expressions (#35)',
156+
'foo:nth-child(2n + $var-calc){}',
157+
'foo:nth-child(2n + $var-calc){}',
158+
{ selectors: true }
159+
);
160+
161+
test(
162+
'should correctly reduce calc with mixed units (cssnano#211)',
163+
'foo{bar:calc(99.99% * 1/1 - 0rem)}',
164+
'foo{bar:99.99%}'
165+
);
166+
167+
test(
168+
'should apply algebraic reduction (cssnano#319)',
169+
'foo{bar:calc((100px - 1em) + (-50px + 1em))}',
170+
'foo{bar:50px}'
171+
);
172+
173+
test(
174+
'should apply optimization (cssnano#320)',
175+
'foo{bar:calc(50% + (5em + 5%))}',
176+
'foo{bar:calc(55% + 5em)}'
177+
);
178+
179+
test(
180+
'should discard zero values (reduce-css-calc#2) (1)',
181+
'foo{bar:calc(100vw / 2 - 6px + 0px)}',
182+
'foo{bar:calc(50vw - 6px)}'
183+
);
184+
185+
test(
186+
'should discard zero values (reduce-css-calc#2) (2)',
187+
'foo{bar:calc(500px - 0px)}',
188+
'foo{bar:500px}'
189+
);
190+
191+
test(
192+
'should not perform addition on unitless values (reduce-css-calc#3)',
193+
'foo{bar:calc(1px + 1)}',
194+
'foo{bar:calc(1px + 1)}'
195+
);
196+
197+
testThrows(
198+
'should throw an exception when attempting to divide by zero',
199+
'foo{bar:calc(500px/0)}',
200+
/Cannot divide by zero/
201+
);
202+
203+
testThrows(
204+
'should throw an exception when attempting to divide by unit',
205+
'foo{bar:calc(500px/2px)}',
206+
/Cannot divide by "px", number expected/
207+
);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/tests/test.es6

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)