rescript-polished 1.15.0 โ†’ 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,245 +1,239 @@
1
- open Jest
2
- open Expect
1
+ open TestFramework
3
2
  open Polished
4
3
 
5
- let keepGoing = _ => ()
6
-
7
- describe("Color", () => {
4
+ let colorTests = () => {
8
5
  open Color
9
6
 
10
- test("shade", () => {
11
- "#ed5051"->shade(~amount=0.25)->expect |> toBe("#b13c3c")
12
- })
13
-
14
- test("tint", () => {
15
- "#ed5051"->tint(~amount=0.25)->expect |> toBe("#f17b7c")
16
- })
17
-
18
- test("lighten", () => {
19
- "#ed5051"->lighten(~amount=0.25)->expect |> toBe("#f9c4c4")
20
- })
21
-
22
- test("darken", () => {
23
- "#ed5051"->darken(~amount=0.25)->expect |> toBe("#ac1213")
24
- })
25
-
26
- test("hsl", () => {
27
- hsl(~hue=30., ~saturation=0.5, ~lightness=0.3)->expect |> toBe("#734d26")
28
- })
29
-
30
- test("adjustHue", () => {
31
- "#ed5051"->adjustHue(~degree=30.5)->expect |> toBe("#ed9f50")
32
- })
33
-
34
- test("complement", () => {
35
- "#ed5051"->complement->expect |> toBe("#50edec")
36
- })
37
-
38
- test("desaturate", () => {
39
- "#ed5051"->desaturate(~amount=25.5)->expect |> toBe("#9f9f9f")
40
- })
41
-
42
- test("getContrast", () => {
43
- "#ed5051"->getContrast("#fff")->expect |> toBe(3.58)
44
- })
45
-
46
- test("getLuminance", () => {
47
- "#ed5051"->getLuminance->expect |> toBe(0.243)
48
- })
49
-
50
- test("grayscale", () => {
51
- "#ed5051"->grayscale->expect |> toBe("#9f9f9f")
52
- })
53
-
54
- test("hsla", () => {
55
- hsla(~hue=130., ~saturation=0.25, ~lightness=0.5, ~alpha=0.5)->expect
56
- |> toBe("rgba(96,159,106,0.5)")
57
- })
58
-
59
- test("invert", () => {
60
- "#ed5951"->invert->expect |> toBe("#12a6ae")
61
- })
62
-
63
- test("meetsContrastGuidelines", () => {
64
- let expected: contrastScores = {
65
- "AA": true,
66
- "AALarge": true,
67
- "AAA": true,
68
- "AAALarge": true,
69
- }
70
-
71
- "#000000"->meetsContrastGuidelines("#ffffff")->expect |> toEqual(expected)
72
- })
73
-
74
- test("transparentize", () => {
75
- "#ed5051"->transparentize(~amount=0.5)->expect |> toBe("rgba(237,80,81,0.5)")
76
- })
77
-
78
- test("hslToColorString", () => {
79
- let hsl = {
80
- "hue": 240.,
81
- "lightness": 1.0,
82
- "saturation": 0.5,
83
- }
84
-
85
- hsl->hslToColorString->expect |> toBe("#fff")
86
- })
87
-
88
- test("mix", () => {
89
- "#ed5051"->mix("#bc9090", ~weight=0.2)->expect |> toBe("#c58383")
90
- })
91
-
92
- test("opacify", () => {
93
- "#ed505100"->opacify(~amount=0.5)->expect |> toBe("rgba(237,80,81,0.5)")
94
- })
95
-
96
- test("parseToHsl", () => {
97
- let expected: hslColor = {
98
- "hue": 0.0,
99
- "lightness": 0.5,
100
- "saturation": 1.0,
101
- }
102
-
103
- "#FF0000"->parseToHsl->expect |> toEqual(expected)
104
- })
105
-
106
- test("parseToRgb", () => {
107
- let expected: rgbColor = {
108
- "red": 237,
109
- "green": 80,
110
- "blue": 81,
111
- }
112
-
113
- "#ed5051"->parseToRgb->expect |> toEqual(expected)
114
- })
115
-
116
- test("readableColor", () => {
117
- "#ed5051"->readableColor(~strict=false, ())->expect |> toBe("#fff") |> keepGoing
118
-
119
- "#000"->readableColor()->expect |> toBe("#000") |> keepGoing
120
-
121
- "black"->readableColor(~darkReturnColor="#ff8", ())->expect |> toBe("#ff8") |> keepGoing
122
-
123
- "white"->readableColor(~lightReturnColor="#001", ())->expect |> toBe("#001") |> keepGoing
124
-
125
- "red"
126
- ->readableColor(~lightReturnColor="#333", ~darkReturnColor="#ddd", ~strict=true, ())
127
- ->expect
128
- |> toBe("#000")
129
- |> keepGoing
130
-
131
- "yellow"
132
- ->readableColor(~lightReturnColor="#333", ~darkReturnColor="#ddd", ~strict=true, ())
133
- ->expect
134
- |> toBe("#333")
135
- |> keepGoing
136
-
137
- "blue"
138
- ->readableColor(~lightReturnColor="#333", ~darkReturnColor="#ddd", ~strict=true, ())
139
- ->expect |> toBe("#ddd")
140
- })
141
-
142
- test("rgb", () => {
143
- let subject: rgbColor = {
144
- "red": 255,
145
- "green": 255,
146
- "blue": 255,
147
- }
148
-
149
- subject->rgb->expect |> toBe("#fff")
150
- })
151
-
152
- test("rgba", () => {
153
- let subject: rgbaColor = {
154
- "red": 255,
155
- "green": 205,
156
- "blue": 100,
157
- "alpha": 0.7,
158
- }
159
-
160
- subject->rgba->expect |> toBe("rgba(255,205,100,0.7)")
161
- })
162
-
163
- test("saturate", () => {
164
- "#ed5051"->saturate(~amount=0.5)->expect |> toBe("#ff3e3f")
165
- })
166
-
167
- test("setHue", () => {
168
- "#CCCD64"->setHue(~hue=42)->expect |> toBe("#cdae64")
169
- })
170
-
171
- test("setLightness", () => {
172
- "#CCCD64"->setLightness(~lightness=0.2)->expect |> toBe("#4d4d19")
173
- })
174
-
175
- test("setSaturation", () => {
176
- "#CCCD64"->setSaturation(~saturation=0.2)->expect |> toBe("#adad84")
177
- })
178
- })
179
-
180
- describe("Math", () => {
7
+ [
8
+ test("shade", () => {
9
+ assertEqual("#ed5051"->shade(~amount=0.25), "#b13c3c")
10
+ }),
11
+ test("tint", () => {
12
+ assertEqual("#ed5051"->tint(~amount=0.25), "#f17b7c")
13
+ }),
14
+ test("lighten", () => {
15
+ assertEqual("#ed5051"->lighten(~amount=0.25), "#f9c4c4")
16
+ }),
17
+ test("darken", () => {
18
+ assertEqual("#ed5051"->darken(~amount=0.25), "#ac1213")
19
+ }),
20
+ test("hsl", () => {
21
+ assertEqual(hsl(~hue=30., ~saturation=0.5, ~lightness=0.3), "#734d26")
22
+ }),
23
+ test("adjustHue", () => {
24
+ assertEqual("#ed5051"->adjustHue(~degree=30.5), "#ed9f50")
25
+ }),
26
+ test("complement", () => {
27
+ assertEqual("#ed5051"->complement, "#50edec")
28
+ }),
29
+ test("desaturate", () => {
30
+ assertEqual("#ed5051"->desaturate(~amount=25.5), "#9f9f9f")
31
+ }),
32
+ test("getContrast", () => {
33
+ assertEqual("#ed5051"->getContrast("#fff"), 3.58)
34
+ }),
35
+ test("getLuminance", () => {
36
+ assertEqual("#ed5051"->getLuminance, 0.243)
37
+ }),
38
+ test("grayscale", () => {
39
+ assertEqual("#ed5051"->grayscale, "#9f9f9f")
40
+ }),
41
+ test("hsla", () => {
42
+ assertEqual(
43
+ hsla(~hue=130., ~saturation=0.25, ~lightness=0.5, ~alpha=0.5),
44
+ "rgba(96,159,106,0.5)",
45
+ )
46
+ }),
47
+ test("invert", () => {
48
+ assertEqual("#ed5951"->invert, "#12a6ae")
49
+ }),
50
+ test("meetsContrastGuidelines", () => {
51
+ let expected: contrastScores = {
52
+ "AA": true,
53
+ "AALarge": true,
54
+ "AAA": true,
55
+ "AAALarge": true,
56
+ }
57
+ assertEqual("#000000"->meetsContrastGuidelines("#ffffff"), expected)
58
+ }),
59
+ test("transparentize", () => {
60
+ assertEqual("#ed5051"->transparentize(~amount=0.5), "rgba(237,80,81,0.5)")
61
+ }),
62
+ test("hslToColorString", () => {
63
+ let hsl = {
64
+ "hue": 240.,
65
+ "lightness": 1.0,
66
+ "saturation": 0.5,
67
+ }
68
+ assertEqual(hsl->hslToColorString, "#fff")
69
+ }),
70
+ test("mix", () => {
71
+ assertEqual("#ed5051"->mix("#bc9090", ~weight=0.2), "#c58383")
72
+ }),
73
+ test("opacify", () => {
74
+ assertEqual("#ed505100"->opacify(~amount=0.5), "rgba(237,80,81,0.5)")
75
+ }),
76
+ test("parseToHsl", () => {
77
+ let expected: hslColor = {
78
+ "hue": 0.0,
79
+ "lightness": 0.5,
80
+ "saturation": 1.0,
81
+ }
82
+ assertEqual("#FF0000"->parseToHsl, expected)
83
+ }),
84
+ test("parseToRgb", () => {
85
+ let expected: rgbColor = {
86
+ "red": 237,
87
+ "green": 80,
88
+ "blue": 81,
89
+ }
90
+ assertEqual("#ed5051"->parseToRgb, expected)
91
+ }),
92
+ test("readableColor", () => {
93
+ combineResults([
94
+ assertEqual("#ed5051"->readableColor(~strict=false, ()), "#000"),
95
+ assertEqual("#000"->readableColor(), "#fff"),
96
+ assertEqual("black"->readableColor(~darkReturnColor="#ff8", ()), "#ff8"),
97
+ assertEqual("white"->readableColor(~lightReturnColor="#001", ()), "#001"),
98
+ assertEqual(
99
+ "red"->readableColor(
100
+ ~lightReturnColor="#333",
101
+ ~darkReturnColor="#ddd",
102
+ ~strict=true,
103
+ (),
104
+ ),
105
+ "#000",
106
+ ),
107
+ assertEqual(
108
+ "yellow"->readableColor(
109
+ ~lightReturnColor="#333",
110
+ ~darkReturnColor="#ddd",
111
+ ~strict=true,
112
+ (),
113
+ ),
114
+ "#333",
115
+ ),
116
+ assertEqual(
117
+ "blue"->readableColor(
118
+ ~lightReturnColor="#333",
119
+ ~darkReturnColor="#ddd",
120
+ ~strict=true,
121
+ (),
122
+ ),
123
+ "#ddd",
124
+ ),
125
+ ])
126
+ }),
127
+ test("rgb", () => {
128
+ let subject: rgbColor = {
129
+ "red": 255,
130
+ "green": 255,
131
+ "blue": 255,
132
+ }
133
+ assertEqual(subject->rgb, "#fff")
134
+ }),
135
+ test("rgba", () => {
136
+ let subject: rgbaColor = {
137
+ "red": 255,
138
+ "green": 205,
139
+ "blue": 100,
140
+ "alpha": 0.7,
141
+ }
142
+ assertEqual(subject->rgba, "rgba(255,205,100,0.7)")
143
+ }),
144
+ test("saturate", () => {
145
+ assertEqual("#ed5051"->saturate(~amount=0.5), "#ff3e3f")
146
+ }),
147
+ test("setHue", () => {
148
+ assertEqual("#CCCD64"->setHue(~hue=42), "#cdae64")
149
+ }),
150
+ test("setLightness", () => {
151
+ assertEqual("#CCCD64"->setLightness(~lightness=0.2), "#4d4d19")
152
+ }),
153
+ test("setSaturation", () => {
154
+ assertEqual("#CCCD64"->setSaturation(~saturation=0.2), "#adad84")
155
+ }),
156
+ ]
157
+ }
158
+
159
+ let mathTests = () => {
181
160
  open Math
182
161
 
183
- test("math", () => {
184
- "12px + 8px"->math()->expect |> toBe("20px") |> keepGoing
185
-
186
- "12rem + 8rem"->math()->expect |> toBe("20rem") |> keepGoing
187
-
188
- math("10px + 8rem")->expect |> toThrow
189
- })
190
- })
191
-
192
- describe("Mixins", () => {
162
+ [
163
+ test("math", () => {
164
+ combineResults([
165
+ assertEqual("12px + 8px"->math(), "20px"),
166
+ assertEqual("12rem + 8rem"->math(), "20rem"),
167
+ {
168
+ let result = try {
169
+ let _ = math("10px + 8rem", ())
170
+ Fail("Expected exception to be thrown")
171
+ } catch {
172
+ | _ => Pass
173
+ }
174
+ result
175
+ },
176
+ ])
177
+ }),
178
+ ]
179
+ }
180
+
181
+ let mixinTests = () => {
193
182
  open Mixins
194
183
 
195
- test("between", () => {
196
- between(
197
- ~fromSize=Size.makeString("16px"),
198
- ~toSize=Size.makeString("100px"),
199
- ~minScreen="400px",
200
- ~maxScreen="1000px",
201
- (),
202
- )->expect |> toMatchSnapshot
203
- })
204
-
205
- test("clearfix", () => {
206
- clearFix(~parent="div")->expect |> toMatchSnapshot
207
- })
208
-
209
- test("cover", () => {
210
- cover(~offset=Size.makeString("16px"), ())->expect |> toMatchSnapshot
211
- })
212
-
213
- test("ellipsis", () => {
214
- ellipsis(~width=Size.makeString("16px"), ~lines=10, ())->expect |> toMatchSnapshot
215
- })
216
-
217
- test("fluidRange", () => {
218
- {
219
- prop: "padding",
220
- fromSize: Size.makeString("20px"),
221
- toSize: Size.makeString("20px"),
222
- }
223
- ->fluidRange(~minScreen="320px", ~maxScreen="1024px")
224
- ->expect
225
- ->toMatchSnapshot
226
- })
227
-
228
- test("fluidRangeWithArray", () => {
229
- [
230
- {
231
- prop: "padding",
232
- fromSize: Size.makeString("16px"),
233
- toSize: Size.makeString("32px"),
234
- },
235
- {
236
- prop: "margin",
237
- fromSize: Size.makeString("16px"),
238
- toSize: Size.makeString("32px"),
239
- },
240
- ]
241
- ->fluidRangeWithArray(~minScreen="320px", ~maxScreen="1024px")
242
- ->expect
243
- ->toMatchSnapshot
244
- })
245
- })
184
+ [
185
+ test("between", () => {
186
+ let result = between(
187
+ ~fromSize=Size.makeString("16px"),
188
+ ~toSize=Size.makeString("100px"),
189
+ ~minScreen="400px",
190
+ ~maxScreen="1000px",
191
+ (),
192
+ )
193
+ assertTrue(result != "", ~message="between should return non-empty string")
194
+ }),
195
+ test("clearfix", () => {
196
+ let result = clearFix(~parent="div")
197
+ assertTrue(result != "", ~message="clearFix should return non-empty string")
198
+ }),
199
+ test("cover", () => {
200
+ let result = cover(~offset=Size.makeString("16px"), ())
201
+ assertTrue(result != "", ~message="cover should return non-empty string")
202
+ }),
203
+ test("ellipsis", () => {
204
+ let result = ellipsis(~width=Size.makeString("16px"), ~lines=10, ())
205
+ assertTrue(result != "", ~message="ellipsis should return non-empty string")
206
+ }),
207
+ test("fluidRange", () => {
208
+ let result =
209
+ {
210
+ prop: "padding",
211
+ fromSize: Size.makeString("20px"),
212
+ toSize: Size.makeString("20px"),
213
+ }->fluidRange(~minScreen="320px", ~maxScreen="1024px")
214
+ assertTrue(result != "", ~message="fluidRange should return non-empty string")
215
+ }),
216
+ test("fluidRangeWithArray", () => {
217
+ let result =
218
+ [
219
+ {
220
+ prop: "padding",
221
+ fromSize: Size.makeString("16px"),
222
+ toSize: Size.makeString("32px"),
223
+ },
224
+ {
225
+ prop: "margin",
226
+ fromSize: Size.makeString("16px"),
227
+ toSize: Size.makeString("32px"),
228
+ },
229
+ ]->fluidRangeWithArray(~minScreen="320px", ~maxScreen="1024px")
230
+ assertTrue(result != "", ~message="fluidRangeWithArray should return non-empty string")
231
+ }),
232
+ ]
233
+ }
234
+
235
+ let suites = [
236
+ suite("Color", colorTests()),
237
+ suite("Math", mathTests()),
238
+ suite("Mixins", mixinTests()),
239
+ ]
@@ -0,0 +1,5 @@
1
+ open TestFramework
2
+
3
+ let allSuites = Array.concat(PolishedTest.suites, PolishedCssTest.suites)
4
+
5
+ runSuites(allSuites)
@@ -0,0 +1,170 @@
1
+ // Simple test framework
2
+
3
+ type testResult = Pass | Fail(string)
4
+
5
+ type testCase = {
6
+ name: string,
7
+ run: unit => testResult,
8
+ }
9
+
10
+ type testSuite = {
11
+ name: string,
12
+ tests: array<testCase>,
13
+ }
14
+
15
+ let test = (name: string, run: unit => testResult): testCase => {
16
+ {name, run}
17
+ }
18
+
19
+ let suite = (name: string, tests: array<testCase>): testSuite => {
20
+ {name, tests}
21
+ }
22
+
23
+ let assertEqual = (actual: 'a, expected: 'a, ~message: option<string>=?): testResult => {
24
+ if actual == expected {
25
+ Pass
26
+ } else {
27
+ let msg = switch message {
28
+ | Some(m) => m
29
+ | None => `Expected ${String.make(expected)}, got ${String.make(actual)}`
30
+ }
31
+ Fail(msg)
32
+ }
33
+ }
34
+
35
+ let assertNotEqual = (actual: 'a, expected: 'a, ~message: option<string>=?): testResult => {
36
+ if actual != expected {
37
+ Pass
38
+ } else {
39
+ let msg = switch message {
40
+ | Some(m) => m
41
+ | None => `Expected values to not be equal`
42
+ }
43
+ Fail(msg)
44
+ }
45
+ }
46
+
47
+ let assertTrue = (condition: bool, ~message: option<string>=?): testResult => {
48
+ if condition {
49
+ Pass
50
+ } else {
51
+ let msg = switch message {
52
+ | Some(m) => m
53
+ | None => "Expected true, got false"
54
+ }
55
+ Fail(msg)
56
+ }
57
+ }
58
+
59
+ let assertFalse = (condition: bool, ~message: option<string>=?): testResult => {
60
+ if !condition {
61
+ Pass
62
+ } else {
63
+ let msg = switch message {
64
+ | Some(m) => m
65
+ | None => "Expected false, got true"
66
+ }
67
+ Fail(msg)
68
+ }
69
+ }
70
+
71
+ let combineResults = (results: array<testResult>): testResult => {
72
+ let failures = results->Array.filter(r =>
73
+ switch r {
74
+ | Fail(_) => true
75
+ | Pass => false
76
+ }
77
+ )
78
+
79
+ if Array.length(failures) > 0 {
80
+ failures->Array.get(0)->Option.getOr(Pass)
81
+ } else {
82
+ Pass
83
+ }
84
+ }
85
+
86
+ let runSuite = (suite: testSuite): unit => {
87
+ Console.log(`\n๐Ÿ“ฆ Running test suite: ${suite.name}`)
88
+ Console.log("=" ++ String.repeat("-", String.length(suite.name) + 23))
89
+
90
+ let passed = ref(0)
91
+ let failed = ref(0)
92
+
93
+ suite.tests->Array.forEach(testCase => {
94
+ switch testCase.run() {
95
+ | Pass => {
96
+ Console.log(` โœ“ ${testCase.name}`)
97
+ passed := passed.contents + 1
98
+ }
99
+ | Fail(message) => {
100
+ Console.log(` โœ— ${testCase.name}`)
101
+ Console.log(` ${message}`)
102
+ failed := failed.contents + 1
103
+ }
104
+ }
105
+ })
106
+
107
+ Console.log("")
108
+ Console.log(
109
+ `Results: ${Int.toString(passed.contents)} passed, ${Int.toString(failed.contents)} failed`,
110
+ )
111
+
112
+ if failed.contents > 0 {
113
+ Console.log("โŒ Some tests failed")
114
+ } else {
115
+ Console.log("โœ… All tests passed!")
116
+ }
117
+ }
118
+
119
+ let runSuites = (suites: array<testSuite>): unit => {
120
+ Console.log("\n๐Ÿงช Running all test suites")
121
+ Console.log("========================\n")
122
+
123
+ let totalPassed = ref(0)
124
+ let totalFailed = ref(0)
125
+
126
+ suites->Array.forEach(suite => {
127
+ Console.log(`\n๐Ÿ“ฆ ${suite.name}`)
128
+ Console.log("-" ++ String.repeat("-", String.length(suite.name) + 3))
129
+
130
+ let suitePassed = ref(0)
131
+ let suiteFailed = ref(0)
132
+
133
+ suite.tests->Array.forEach(testCase => {
134
+ switch testCase.run() {
135
+ | Pass => {
136
+ Console.log(` โœ“ ${testCase.name}`)
137
+ suitePassed := suitePassed.contents + 1
138
+ totalPassed := totalPassed.contents + 1
139
+ }
140
+ | Fail(message) => {
141
+ Console.log(` โœ— ${testCase.name}`)
142
+ Console.log(` ${message}`)
143
+ suiteFailed := suiteFailed.contents + 1
144
+ totalFailed := totalFailed.contents + 1
145
+ }
146
+ }
147
+ })
148
+
149
+ Console.log(
150
+ ` ${Int.toString(suitePassed.contents)} passed, ${Int.toString(
151
+ suiteFailed.contents,
152
+ )} failed`,
153
+ )
154
+ })
155
+
156
+ Console.log("\n" ++ String.repeat("=", 50))
157
+ Console.log(
158
+ `Total: ${Int.toString(totalPassed.contents)} passed, ${Int.toString(
159
+ totalFailed.contents,
160
+ )} failed`,
161
+ )
162
+
163
+ if totalFailed.contents > 0 {
164
+ Console.log("โŒ Some tests failed\n")
165
+ %raw(`process.exit(1)`)
166
+ } else {
167
+ Console.log("โœ… All tests passed!\n")
168
+ %raw(`process.exit(0)`)
169
+ }
170
+ }
File without changes