|
| 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 | +); |
0 commit comments