|
1 | 1 | var test = require("tape")
|
2 |
| -var reduceCssCalc = require("..") |
| 2 | +var reduceCSSCalc = require("..") |
3 | 3 |
|
4 | 4 | test("throws an syntax error if a parenthese is missing", function(t) {
|
5 |
| - t.throws(function() { reduceCssCalc("calc(")}, SyntaxError) |
6 |
| - t.throws(function() { reduceCssCalc("calc((2 + 1)")}, SyntaxError) |
7 |
| - t.throws(function() { reduceCssCalc("a calc(calc(2 + 1) b()")}, SyntaxError) |
| 5 | + t.throws(function() { reduceCSSCalc("calc(")}, SyntaxError) |
| 6 | + t.throws(function() { reduceCSSCalc("calc((2 + 1)")}, SyntaxError) |
| 7 | + t.throws(function() { reduceCSSCalc("a calc(calc(2 + 1) b()")}, SyntaxError) |
8 | 8 | t.end()
|
9 | 9 | })
|
10 | 10 |
|
11 | 11 | test("throws an error if a calc() is empty", function(t) {
|
12 |
| - t.throws(function() { reduceCssCalc("calc()")}, Error) |
13 |
| - t.throws(function() { reduceCssCalc("calc(2 + ())")}, SyntaxError) |
14 |
| - t.throws(function() { reduceCssCalc("calc(2 + calc())")}, SyntaxError) |
| 12 | + t.throws(function() { reduceCSSCalc("calc()")}, Error) |
| 13 | + t.throws(function() { reduceCSSCalc("calc(2 + ())")}, SyntaxError) |
| 14 | + t.throws(function() { reduceCSSCalc("calc(2 + calc())")}, SyntaxError) |
15 | 15 | t.end()
|
16 | 16 | })
|
17 | 17 |
|
18 | 18 | test("complete reduce for simple css calc()", function(t) {
|
19 |
| - t.equal(reduceCssCalc("calc(1 + 1)"), "2", "addition") |
20 |
| - t.equal(reduceCssCalc("calc(1 - 1)"), "0", "substraction") |
21 |
| - t.equal(reduceCssCalc("calc(2 * 2)"), "4", "multiplication") |
22 |
| - t.equal(reduceCssCalc("calc(8 / 2)"), "4", "division") |
23 |
| - t.equal(reduceCssCalc("calc((6 / 2) - (4 * 2) + 1)"), "-4", "embed operations") |
| 19 | + t.equal(reduceCSSCalc("calc(1 + 1)"), "2", "addition") |
| 20 | + t.equal(reduceCSSCalc("calc(1 - 1)"), "0", "substraction") |
| 21 | + t.equal(reduceCSSCalc("calc(2 * 2)"), "4", "multiplication") |
| 22 | + t.equal(reduceCSSCalc("calc(8 / 2)"), "4", "division") |
| 23 | + t.equal(reduceCSSCalc("calc((6 / 2) - (4 * 2) + 1)"), "-4", "embed operations") |
24 | 24 | t.end()
|
25 | 25 | })
|
26 | 26 |
|
27 | 27 | test("complete reduce for css calc() with a single unit", function(t) {
|
28 |
| - t.equal(reduceCssCalc("calc(3px * 2 - 1px)"), "5px", "px") |
29 |
| - t.equal(reduceCssCalc("calc(3rem * 2 - 1rem)"), "5rem", "rem") |
30 |
| - t.equal(reduceCssCalc("calc(3em * 2 - 1em)"), "5em", "em") |
31 |
| - t.equal(reduceCssCalc("calc(3pt * 2 - 1pt)"), "5pt", "pt") |
32 |
| - t.equal(reduceCssCalc("calc(3vh * 2 - 1vh)"), "5vh", "vh") |
| 28 | + t.equal(reduceCSSCalc("calc(3px * 2 - 1px)"), "5px", "px") |
| 29 | + t.equal(reduceCSSCalc("calc(3rem * 2 - 1rem)"), "5rem", "rem") |
| 30 | + t.equal(reduceCSSCalc("calc(3em * 2 - 1em)"), "5em", "em") |
| 31 | + t.equal(reduceCSSCalc("calc(3pt * 2 - 1pt)"), "5pt", "pt") |
| 32 | + t.equal(reduceCSSCalc("calc(3vh * 2 - 1vh)"), "5vh", "vh") |
33 | 33 | t.end()
|
34 | 34 | })
|
35 | 35 |
|
36 | 36 | test("complete & accurate reduce for css calc() with percentages", function(t) {
|
37 |
| - t.equal(reduceCssCalc("calc(2 * 50%)"), "100%", "integer * percentage") |
38 |
| - t.equal(reduceCssCalc("calc(120% * 50%)"), "60%", "percentage * percentage") |
| 37 | + t.equal(reduceCSSCalc("calc(2 * 50%)"), "100%", "integer * percentage") |
| 38 | + t.equal(reduceCSSCalc("calc(120% * 50%)"), "60%", "percentage * percentage") |
39 | 39 | t.end()
|
40 | 40 | })
|
41 | 41 |
|
42 | 42 | test("ignore value around css calc() functions ", function(t) {
|
43 |
| - t.equal(reduceCssCalc("calc(1 + 1) a"), "2 a", "value after") |
44 |
| - t.equal(reduceCssCalc("a calc(1 + 1)"), "a 2", "value before") |
45 |
| - t.equal(reduceCssCalc("calc(1 + 1) a calc(1 - 1)"), "2 a 0", "value between 2 calc()") |
46 |
| - t.equal(reduceCssCalc("a calc(1 + 1) b calc(1 - 1)"), "a 2 b 0", "value before & between 2 calc()") |
47 |
| - t.equal(reduceCssCalc("a calc(1 + 1) b calc(1 - 1) c"), "a 2 b 0 c", "value before, between & after 2 calc()") |
| 43 | + t.equal(reduceCSSCalc("calc(1 + 1) a"), "2 a", "value after") |
| 44 | + t.equal(reduceCSSCalc("a calc(1 + 1)"), "a 2", "value before") |
| 45 | + t.equal(reduceCSSCalc("calc(1 + 1) a calc(1 - 1)"), "2 a 0", "value between 2 calc()") |
| 46 | + t.equal(reduceCSSCalc("a calc(1 + 1) b calc(1 - 1)"), "a 2 b 0", "value before & between 2 calc()") |
| 47 | + t.equal(reduceCSSCalc("a calc(1 + 1) b calc(1 - 1) c"), "a 2 b 0 c", "value before, between & after 2 calc()") |
48 | 48 | t.end()
|
49 | 49 | })
|
50 | 50 |
|
51 | 51 | test("reduce complexe css calc()", function(t) {
|
52 |
| - t.equal(reduceCssCalc("calc(calc(100 + 10) + 1)"), "111", "integer") |
53 |
| - t.equal(reduceCssCalc("calc(calc(calc(1rem * 0.75) * 1.5) - 1rem)"), "0.125rem", "with a single unit") |
54 |
| - t.equal(reduceCssCalc("calc(calc(calc(1rem * 0.75) * 1.5) - 1px)"), "calc(1.125rem - 1px)", "multiple units with explicit calc") |
55 |
| - t.equal(reduceCssCalc("calc(((1rem * 0.75) * 1.5) - 1px)"), "calc(1.125rem - 1px)", "multiple units with implicit calc") |
56 |
| - t.equal(reduceCssCalc("calc(-1px + (1.5 * (1rem * 0.75)))"), "calc(-1px + 1.125rem)", "multiple units with implicit calc, reverse order") |
57 |
| - t.equal(reduceCssCalc("calc(2rem * (2 * (2 + 3)) + 4 + (5/2))"), "26.5rem", "complex math formula works correctly") |
| 52 | + t.equal(reduceCSSCalc("calc(calc(100 + 10) + 1)"), "111", "integer") |
| 53 | + t.equal(reduceCSSCalc("calc(calc(calc(1rem * 0.75) * 1.5) - 1rem)"), "0.125rem", "with a single unit") |
| 54 | + t.equal(reduceCSSCalc("calc(calc(calc(1rem * 0.75) * 1.5) - 1px)"), "calc(1.125rem - 1px)", "multiple units with explicit calc") |
| 55 | + t.equal(reduceCSSCalc("calc(((1rem * 0.75) * 1.5) - 1px)"), "calc(1.125rem - 1px)", "multiple units with implicit calc") |
| 56 | + t.equal(reduceCSSCalc("calc(-1px + (1.5 * (1rem * 0.75)))"), "calc(-1px + 1.125rem)", "multiple units with implicit calc, reverse order") |
| 57 | + t.equal(reduceCSSCalc("calc(2rem * (2 * (2 + 3)) + 4 + (5/2))"), "26.5rem", "complex math formula works correctly") |
58 | 58 |
|
59 |
| - t.equal(reduceCssCalc("calc((4 * 2) + 4.2 + 1 + (2rem * .4) + (2px * .4))"), "calc(8 + 4.2 + 1 + 0.8rem + 0.8px)", "handle long formula") |
| 59 | + t.equal(reduceCSSCalc("calc((4 * 2) + 4.2 + 1 + (2rem * .4) + (2px * .4))"), "calc(8 + 4.2 + 1 + 0.8rem + 0.8px)", "handle long formula") |
60 | 60 | t.end()
|
61 | 61 | })
|
62 | 62 |
|
63 | 63 | test("reduce prefixed css calc()", function(t) {
|
64 |
| - t.equal(reduceCssCalc("-webkit-calc(120% * 50%)"), "60%", "-webkit, complete reduce") |
65 |
| - t.equal(reduceCssCalc("-webkit-calc(100% - 2em)"), "-webkit-calc(100% - 2em)", "-webkit, multiple unit") |
| 64 | + t.equal(reduceCSSCalc("-webkit-calc(120% * 50%)"), "60%", "-webkit, complete reduce") |
| 65 | + t.equal(reduceCSSCalc("-webkit-calc(100% - 2em)"), "-webkit-calc(100% - 2em)", "-webkit, multiple unit") |
66 | 66 |
|
67 |
| - t.equal(reduceCssCalc("-moz-calc(100px / 2)"), "50px", "-moz, complete reduce") |
68 |
| - t.equal(reduceCssCalc("-moz-calc(50% - 2em)"), "-moz-calc(50% - 2em)","-moz, multiple unit") |
| 67 | + t.equal(reduceCSSCalc("-moz-calc(100px / 2)"), "50px", "-moz, complete reduce") |
| 68 | + t.equal(reduceCSSCalc("-moz-calc(50% - 2em)"), "-moz-calc(50% - 2em)","-moz, multiple unit") |
69 | 69 | t.end()
|
70 | 70 | })
|
71 | 71 |
|
72 | 72 | test("ignore unrecognized values", function(t) {
|
73 |
| - t.equal(reduceCssCalc("calc((4px * 2) + 4.2 + a1 + (2rem * .4))"), "calc(8px + 4.2 + a1 + 0.8rem)", "ignore when eval fail") |
| 73 | + t.equal(reduceCSSCalc("calc((4px * 2) + 4.2 + a1 + (2rem * .4))"), "calc(8px + 4.2 + a1 + 0.8rem)", "ignore when eval fail") |
74 | 74 | t.end()
|
75 | 75 | })
|
0 commit comments