diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-11-29 13:37:25 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-11-29 14:18:13 +0100 |
| commit | 0efe669278a5e1c3f2985eba2f3360e91159c54a (patch) | |
| tree | 502712857c48f0decb5e698257c0a96d358a436e /tests/typ/compute | |
| parent | 836692e73cff0356e409a9ba5b4887b86809d4ca (diff) | |
Reorganize library and tests
Diffstat (limited to 'tests/typ/compute')
| -rw-r--r-- | tests/typ/compute/calc.typ | 117 | ||||
| -rw-r--r-- | tests/typ/compute/create.typ | 55 | ||||
| -rw-r--r-- | tests/typ/compute/data.typ | 58 | ||||
| -rw-r--r-- | tests/typ/compute/foundations.typ | 76 | ||||
| -rw-r--r-- | tests/typ/compute/utility.typ | 44 |
5 files changed, 350 insertions, 0 deletions
diff --git a/tests/typ/compute/calc.typ b/tests/typ/compute/calc.typ new file mode 100644 index 00000000..4ccefa22 --- /dev/null +++ b/tests/typ/compute/calc.typ @@ -0,0 +1,117 @@ +// Test math functions. +// Ref: false + +--- +// Test conversion to numbers. +#test(int(false), 0) +#test(int(true), 1) +#test(int(10), 10) +#test(int("150"), 150) +#test(int(10 / 3), 3) +#test(float(10), 10.0) +#test(float("31.4e-1"), 3.14) +#test(type(float(10)), "float") + +--- +// Error: 6-10 cannot convert length to integer +#int(10pt) + +--- +// Error: 8-13 cannot convert function to float +#float(float) + +--- +// Error: 6-12 invalid integer +#int("nope") + +--- +// Error: 8-15 invalid float +#float("1.2.3") + +--- +// Test the `abs` function. +#test(abs(-3), 3) +#test(abs(3), 3) +#test(abs(-0.0), 0.0) +#test(abs(0.0), -0.0) +#test(abs(-3.14), 3.14) +#test(abs(50%), 50%) +#test(abs(-25%), 25%) + +--- +// Error: 6-17 expected numeric value, found string +#abs("no number") + +--- +// Error: 6-11 cannot take absolute value of a length +#abs(-12pt) + +--- +// Error: 6-16 cannot take absolute value of a length +#abs(50% - 12pt) + +--- +// Test the `even` and `odd` functions. +#test(even(2), true) +#test(odd(2), false) +#test(odd(-1), true) +#test(even(-11), false) + +--- +// Test the `mod` function. +#test(mod(1, 1), 0) +#test(mod(5, 3), 2) +#test(mod(5, -3), 2) +#test(mod(22.5, 10), 2.5) +#test(mod(9, 4.5), 0) + +--- +// Error: 9-10 divisor must not be zero +#mod(5, 0) + +--- +// Error: 11-14 divisor must not be zero +#mod(3.0, 0.0) + +--- +// Test the `min` and `max` functions. +#test(min(2, -4), -4) +#test(min(3.5, 1e2, -0.1, 3), -0.1) +#test(max(-3, 11), 11) +#test(min("hi"), "hi") + +--- +// Error: 5-7 missing argument: value +#min() + +--- +// Error: 9-13 cannot compare integer and string +#min(1, "hi") + +--- +// Test the `range` function. +#test(range(4), (0, 1, 2, 3)) +#test(range(1, 4), (1, 2, 3)) +#test(range(-4, 2), (-4, -3, -2, -1, 0, 1)) +#test(range(10, 5), ()) +#test(range(10, step: 3), (0, 3, 6, 9)) +#test(range(1, 4, step: 1), (1, 2, 3)) +#test(range(1, 8, step: 2), (1, 3, 5, 7)) +#test(range(5, 2, step: -1), (5, 4, 3)) +#test(range(10, 0, step: -3), (10, 7, 4, 1)) + +--- +// Error: 7-9 missing argument: end +#range() + +--- +// Error: 11-14 expected integer, found float +#range(1, 2.0) + +--- +// Error: 17-22 expected integer, found string +#range(4, step: "one") + +--- +// Error: 18-19 step must not be zero +#range(10, step: 0) diff --git a/tests/typ/compute/create.typ b/tests/typ/compute/create.typ new file mode 100644 index 00000000..462f06e9 --- /dev/null +++ b/tests/typ/compute/create.typ @@ -0,0 +1,55 @@ +// Test creation and conversion functions. +// Ref: false + +--- +// Compare both ways. +#test(rgb(0%, 30%, 70%), rgb("004db3")) + +// Alpha channel. +#test(rgb(255, 0, 0, 50%), rgb("ff000080")) + +// Test color modification methods. +#test(rgb(25, 35, 45).lighten(10%), rgb(48, 57, 66)) +#test(rgb(40, 30, 20).darken(10%), rgb(36, 27, 18)) +#test(rgb("#133337").negate(), rgb(236, 204, 200)) +#test(white.lighten(100%), white) + +--- +// Test gray color conversion. +// Ref: true +#rect(fill: luma(0)) +#rect(fill: luma(80%)) + +--- +// Error for values that are out of range. +// Error: 11-14 must be between 0 and 255 +#test(rgb(-30, 15, 50)) + +--- +// Error: 6-11 string contains non-hexadecimal letters +#rgb("lol") + +--- +// Error: 5-7 missing argument: red component +#rgb() + +--- +// Error: 5-11 missing argument: blue component +#rgb(0, 1) + +--- +// Error: 21-26 expected integer or ratio, found boolean +#rgb(10%, 20%, 30%, false) + +--- +// Test conversion to string. +#test(str(123), "123") +#test(str(50.14), "50.14") +#test(str(10 / 3).len() > 10, true) + +--- +// Error: 6-8 cannot convert content to string +#str([]) + +--- +#assert(range(2, 5) == (2, 3, 4)) diff --git a/tests/typ/compute/data.typ b/tests/typ/compute/data.typ new file mode 100644 index 00000000..c1def7d5 --- /dev/null +++ b/tests/typ/compute/data.typ @@ -0,0 +1,58 @@ +// Test reading structured data. +// Ref: false + +--- +// Test reading CSV data. +// Ref: true +#set page(width: auto) +#let data = csv("/res/zoo.csv") +#let cells = data(0).map(strong) + data.slice(1).flatten() +#table(columns: data(0).len(), ..cells) + +--- +// Error: 6-16 file not found (searched at typ/compute/nope.csv) +#csv("nope.csv") + +--- +// Error: 6-20 failed to parse csv file: found 3 instead of 2 fields in line 3 +#csv("/res/bad.csv") + +--- +// Test reading JSON data. +#let data = json("/res/zoo.json") +#test(data.len(), 3) +#test(data(0).name, "Debby") +#test(data(2).weight, 150) + +--- +// Error: 7-22 failed to parse json file: syntax error in line 3 +#json("/res/bad.json") + +--- +// Test reading XML data. +#let data = xml("/res/data.xml") +#test(data, (( + tag: "data", + attrs: (:), + children: ( + "\n ", + (tag: "hello", attrs: (name: "hi"), children: ("1",)), + "\n ", + ( + tag: "data", + attrs: (:), + children: ( + "\n ", + (tag: "hello", attrs: (:), children: ("World",)), + "\n ", + (tag: "hello", attrs: (:), children: ("World",)), + "\n ", + ), + ), + "\n", + ), +),)) + +--- +// Error: 6-20 failed to parse xml file: found closing tag 'data' instead of 'hello' in line 3 +#xml("/res/bad.xml") diff --git a/tests/typ/compute/foundations.typ b/tests/typ/compute/foundations.typ new file mode 100644 index 00000000..6fc93a75 --- /dev/null +++ b/tests/typ/compute/foundations.typ @@ -0,0 +1,76 @@ +// Test foundational functions. +// Ref: false + +--- +#test(type(1), "integer") +#test(type(ltr), "direction") +#test(type(10 / 3), "float") + +--- +#test(repr(ltr), "ltr") +#test(repr((1, 2, false, )), "(1, 2, false)") + +--- +// Test failing assertions. +// Error: 9-15 assertion failed +#assert(1 == 2) + +--- +// Test failing assertions. +// Error: 9-15 expected boolean, found string +#assert("true") + +--- +// Test the `type` function. +#test(type(1), "integer") +#test(type(ltr), "direction") +#test(type(10 / 3), "float") + +--- +#eval("_Hello" + " World!_") + +--- +// Error: 7-13 expected identifier +#eval("#let") + +--- +#show raw: it => text("IBM Plex Sans", eval(it.text)) + +Interacting +``` +#set text(blue) +Blue #move(dy: -0.15em)[🌊] +``` + +--- +// Error: 7-19 cannot continue outside of loop +#eval("{continue}") + +--- +// Error: 7-33 cannot access file system from here +#eval("#include \"../coma.typ\"") + +--- +// Error: 7-35 cannot access file system from here +#eval("#image(\"/res/tiger.jpg\")") + +--- +// Error: 23-30 cannot access file system from here +#show raw: it => eval(it.text) + +``` +#image("/res/tiger.jpg") +``` + +--- +// Error: 23-30 cannot access file system from here +#show raw: it => eval(it.text) + +``` +#show emph: _ => image("../../res/giraffe.jpg") +_No relative giraffe!_ +``` + +--- +// Error: 7-16 expected comma +#eval("{(1 2)}") diff --git a/tests/typ/compute/utility.typ b/tests/typ/compute/utility.typ new file mode 100644 index 00000000..f042c769 --- /dev/null +++ b/tests/typ/compute/utility.typ @@ -0,0 +1,44 @@ +// Test integrated numbering patterns. + +--- +// Test basic call. +#lorem(19) + +--- +// Test custom paragraphs with user code. +#set text(8pt) + +{ + let sentences = lorem(59) + .split(".") + .filter(s => s != "") + .map(s => s + ".") + + let used = 0 + for s in sentences { + if used < 2 { + used += 1 + } else { + parbreak() + used = 0 + } + s.trim() + [ ] + } +} + +--- +// Error: 7-9 missing argument: number of words +#lorem() + +--- +#for i in range(9) { + numbering(i, "* and ") + numbering(i, "I") + [ for #i] + parbreak() +} + +--- +// Error: 12-14 must be at least zero +#numbering(-1, "1") |
