diff options
| author | Pg Biel <9021226+PgBiel@users.noreply.github.com> | 2023-07-11 11:11:18 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-11 16:11:18 +0200 |
| commit | 9b1a2b41f0bb2d62106e029a5a0174dcf07ae0d2 (patch) | |
| tree | 6f0ae44b3dfffd1c5b3e44535716d382ffc41c46 /tests | |
| parent | 507efc3a1c14e8487705c7424d605a1663d3fe6e (diff) | |
Add fields and methods to several primitives (#790)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/typ/compiler/field.typ | 86 | ||||
| -rw-r--r-- | tests/typ/compiler/methods.typ | 145 |
2 files changed, 231 insertions, 0 deletions
diff --git a/tests/typ/compiler/field.typ b/tests/typ/compiler/field.typ index dd8499ce..5c28a92c 100644 --- a/tests/typ/compiler/field.typ +++ b/tests/typ/compiler/field.typ @@ -62,3 +62,89 @@ --- // Error: 9-13 cannot access fields on type boolean #{false.true} + +--- +// Test relative length fields. +#test((100% + 2em + 2pt).ratio, 100%) +#test((100% + 2em + 2pt).length, 2em + 2pt) +#test((100% + 2pt).length, 2pt) +#test((100% + 2pt - 2pt).length, 0pt) +#test((56% + 2pt - 56%).ratio, 0%) + +--- +// Test length fields. +#test((1pt).em, 0em) +#test((1pt).abs, 1pt) +#test((3em).em, 3em) +#test((3em).abs, 0pt) +#test((2em + 2pt).em, 2em) +#test((2em + 2pt).abs, 2pt) + +--- +// Test stroke fields for simple strokes. +#test((1em + blue).paint, blue) +#test((1em + blue).thickness, 1em) +#test((1em + blue).cap, "butt") +#test((1em + blue).join, "miter") +#test((1em + blue).dash, none) +#test((1em + blue).miter-limit, 4.0) + +--- +// Test complex stroke fields. +#let r1 = rect(stroke: (paint: cmyk(1%, 2%, 3%, 4%), thickness: 4em + 2pt, cap: "round", join: "bevel", miter-limit: 5.0, dash: none)) +#let r2 = rect(stroke: (paint: cmyk(1%, 2%, 3%, 4%), thickness: 4em + 2pt, cap: "round", join: "bevel", miter-limit: 5.0, dash: (3pt, "dot", 4em))) +#let r3 = rect(stroke: (paint: cmyk(1%, 2%, 3%, 4%), thickness: 4em + 2pt, cap: "round", join: "bevel", dash: (array: (3pt, "dot", 4em), phase: 5em))) +#let s1 = r1.stroke +#let s2 = r2.stroke +#let s3 = r3.stroke +#test(s1.paint, cmyk(1%, 2%, 3%, 4%)) +#test(s1.thickness, 4em + 2pt) +#test(s1.cap, "round") +#test(s1.join, "bevel") +#test(s1.miter-limit, 5.0) +#test(s3.miter-limit, 4.0) +#test(s1.dash, none) +#test(s2.dash, (array: (3pt, "dot", 4em), phase: 0pt)) +#test(s3.dash, (array: (3pt, "dot", 4em), phase: 5em)) + +--- +// Test 2d alignment 'horizontal' field. +#test((start + top).x, start) +#test((end + top).x, end) +#test((left + top).x, left) +#test((right + top).x, right) +#test((center + top).x, center) +#test((start + bottom).x, start) +#test((end + bottom).x, end) +#test((left + bottom).x, left) +#test((right + bottom).x, right) +#test((center + bottom).x, center) +#test((start + horizon).x, start) +#test((end + horizon).x, end) +#test((left + horizon).x, left) +#test((right + horizon).x, right) +#test((center + horizon).x, center) +#test((top + start).x, start) +#test((bottom + end).x, end) +#test((horizon + center).x, center) + +--- +// Test 2d alignment 'vertical' field. +#test((start + top).y, top) +#test((end + top).y, top) +#test((left + top).y, top) +#test((right + top).y, top) +#test((center + top).y, top) +#test((start + bottom).y, bottom) +#test((end + bottom).y, bottom) +#test((left + bottom).y, bottom) +#test((right + bottom).y, bottom) +#test((center + bottom).y, bottom) +#test((start + horizon).y, horizon) +#test((end + horizon).y, horizon) +#test((left + horizon).y, horizon) +#test((right + horizon).y, horizon) +#test((center + horizon).y, horizon) +#test((top + start).y, top) +#test((bottom + end).y, bottom) +#test((horizon + center).y, horizon) diff --git a/tests/typ/compiler/methods.typ b/tests/typ/compiler/methods.typ index 864ed8ad..c0ad5b1d 100644 --- a/tests/typ/compiler/methods.typ +++ b/tests/typ/compiler/methods.typ @@ -53,3 +53,148 @@ // Test content fields method. #test([a].fields(), (text: "a")) #test([a *b*].fields(), (children: ([a], [ ], strong[b]))) + +--- +// Test length unit conversions. +#test((500.934pt).pt(), 500.934) +#test((3.3453cm).cm(), 3.3453) +#test((4.3452mm).mm(), 4.3452) +#test((5.345in).inches(), 5.345) +#test((500.333666999pt).pt(), 500.333666999) +#test((3.5234354cm).cm(), 3.5234354) +#test((4.12345678mm).mm(), 4.12345678) +#test((5.333666999in).inches(), 5.333666999) +#test((4.123456789123456mm).mm(), 4.123456789123456) +#test((254cm).mm(), 2540.0) +#test(calc.round((254cm).inches(), digits: 2), 100.0) +#test((2540mm).cm(), 254.0) +#test(calc.round((2540mm).inches(), digits: 2), 100.0) +#test((100in).pt(), 7200.0) +#test(calc.round((100in).cm(), digits: 2), 254.0) +#test(calc.round((100in).mm(), digits: 2), 2540.0) +#test(5em.abs.cm(), 0.0) +#test((5em + 6in).abs.inches(), 6.0) + +--- +// Error: 2-21 cannot convert a length with non-zero em units (-6pt + 10.5em) to pt +// Hint: 2-21 use 'length.abs.pt()' instead to ignore its em component +#(10.5em - 6pt).pt() + +--- +// Error: 2-12 cannot convert a length with non-zero em units (3em) to cm +// Hint: 2-12 use 'length.abs.cm()' instead to ignore its em component +#(3em).cm() + +--- +// Error: 2-20 cannot convert a length with non-zero em units (-226.77pt + 93em) to mm +// Hint: 2-20 use 'length.abs.mm()' instead to ignore its em component +#(93em - 80mm).mm() + +--- +// Error: 2-24 cannot convert a length with non-zero em units (432pt + 4.5em) to inches +// Hint: 2-24 use 'length.abs.inches()' instead to ignore its em component +#(4.5em + 6in).inches() + +--- +// Test color kind method. +#test(rgb(1, 2, 3, 4).kind(), rgb) +#test(cmyk(4%, 5%, 6%, 7%).kind(), cmyk) +#test(luma(40).kind(), luma) +#test(rgb(1, 2, 3, 4).kind() != luma, true) + +--- +// Test color '.rgba()', '.cmyk()' and '.luma()' without conversions +#test(rgb(1, 2, 3, 4).rgba(), (1, 2, 3, 4)) +#test(rgb(1, 2, 3).rgba(), (1, 2, 3, 255)) +#test(cmyk(20%, 20%, 40%, 20%).cmyk(), (20%, 20%, 40%, 20%)) +#test(luma(40).luma(), 40) + +--- +// Test color conversions. +#test(rgb(1, 2, 3).hex(), "#010203") +#test(rgb(1, 2, 3, 4).hex(), "#01020304") +#test(cmyk(4%, 5%, 6%, 7%).rgba(), (228, 225, 223, 255)) +#test(cmyk(4%, 5%, 6%, 7%).hex(), "#e4e1df") +#test(luma(40).rgba(), (40, 40, 40, 255)) +#test(luma(40).hex(), "#282828") +#test(repr(luma(40).cmyk()), repr((11.76%, 10.59%, 10.59%, 14.12%))) + +--- +// Error: 2-24 cannot obtain cmyk values from rgba color +#rgb(1, 2, 3, 4).cmyk() + +--- +// Error: 2-24 cannot obtain the luma value of rgba color +#rgb(1, 2, 3, 4).luma() + +--- +// Error: 2-29 cannot obtain the luma value of cmyk color +#cmyk(4%, 5%, 6%, 7%).luma() + +--- +// Test alignment methods. +#test(start.axis(), "horizontal") +#test(end.axis(), "horizontal") +#test(left.axis(), "horizontal") +#test(right.axis(), "horizontal") +#test(center.axis(), "horizontal") +#test(top.axis(), "vertical") +#test(bottom.axis(), "vertical") +#test(horizon.axis(), "vertical") +#test(start.inv(), end) +#test(end.inv(), start) +#test(left.inv(), right) +#test(right.inv(), left) +#test(center.inv(), center) +#test(top.inv(), bottom) +#test(bottom.inv(), top) +#test(horizon.inv(), horizon) + +--- +// Test 2d alignment methods. +#test((start + top).inv(), (end + bottom)) +#test((end + top).inv(), (start + bottom)) +#test((left + top).inv(), (right + bottom)) +#test((right + top).inv(), (left + bottom)) +#test((center + top).inv(), (center + bottom)) +#test((start + bottom).inv(), (end + top)) +#test((end + bottom).inv(), (start + top)) +#test((left + bottom).inv(), (right + top)) +#test((right + bottom).inv(), (left + top)) +#test((center + bottom).inv(), (center + top)) +#test((start + horizon).inv(), (end + horizon)) +#test((end + horizon).inv(), (start + horizon)) +#test((left + horizon).inv(), (right + horizon)) +#test((right + horizon).inv(), (left + horizon)) +#test((center + horizon).inv(), (center + horizon)) +#test((top + start).inv(), (end + bottom)) +#test((bottom + end).inv(), (start + top)) +#test((horizon + center).inv(), (center + horizon)) + +--- +// Test direction methods. +#test(ltr.axis(), "horizontal") +#test(rtl.axis(), "horizontal") +#test(ttb.axis(), "vertical") +#test(btt.axis(), "vertical") +#test(ltr.start(), left) +#test(rtl.start(), right) +#test(ttb.start(), top) +#test(btt.start(), bottom) +#test(ltr.end(), right) +#test(rtl.end(), left) +#test(ttb.end(), bottom) +#test(btt.end(), top) +#test(ltr.inv(), rtl) +#test(rtl.inv(), ltr) +#test(ttb.inv(), btt) +#test(btt.inv(), ttb) + +--- +// Test angle methods. +#test(1rad.rad(), 1.0) +#test(1.23rad.rad(), 1.23) +#test(0deg.rad(), 0.0) +#test(2deg.deg(), 2.0) +#test(2.94deg.deg(), 2.94) +#test(0rad.deg(), 0.0) |
