From f88ef45ee6e285df59c7aa5cec935de331b4b6e0 Mon Sep 17 00:00:00 2001 From: Pg Biel <9021226+PgBiel@users.noreply.github.com> Date: Wed, 3 May 2023 09:20:53 -0300 Subject: Function scopes (#1032) --- tests/ref/compiler/import.png | Bin 4446 -> 5941 bytes tests/ref/layout/enum.png | Bin 16078 -> 18946 bytes tests/typ/compiler/field.typ | 24 ++++++++++++++++++++ tests/typ/compiler/import.typ | 45 +++++++++++++++++++++++++++++++++++++- tests/typ/compute/foundations.typ | 26 ++++++++++++++++++++++ tests/typ/layout/enum.typ | 12 ++++++++++ 6 files changed, 106 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/ref/compiler/import.png b/tests/ref/compiler/import.png index bf95f45d..5c6132d2 100644 Binary files a/tests/ref/compiler/import.png and b/tests/ref/compiler/import.png differ diff --git a/tests/ref/layout/enum.png b/tests/ref/layout/enum.png index fb2c2a63..94a9ed51 100644 Binary files a/tests/ref/layout/enum.png and b/tests/ref/layout/enum.png differ diff --git a/tests/typ/compiler/field.typ b/tests/typ/compiler/field.typ index d1e4a31a..dd8499ce 100644 --- a/tests/typ/compiler/field.typ +++ b/tests/typ/compiler/field.typ @@ -22,6 +22,30 @@ - B - C +--- +// Test fields on function scopes. +#enum.item +#assert.eq +#assert.ne + +--- +// Error: 9-16 function `assert` does not contain field `invalid` +#assert.invalid + +--- +// Error: 7-14 function `enum` does not contain field `invalid` +#enum.invalid + +--- +// Error: 7-14 function `enum` does not contain field `invalid` +#enum.invalid() + +--- +// Closures cannot have fields. +#let f(x) = x +// Error: 4-11 cannot access fields on user-defined functions +#f.invalid + --- // Error: 6-13 dictionary does not contain key "invalid" and no default value was specified #(:).invalid diff --git a/tests/typ/compiler/import.typ b/tests/typ/compiler/import.typ index 68b6af2e..0f161766 100644 --- a/tests/typ/compiler/import.typ +++ b/tests/typ/compiler/import.typ @@ -1,4 +1,4 @@ -// Test module imports. +// Test function and module imports. // Ref: false --- @@ -34,6 +34,20 @@ // It exists now! #test(d, 3) +--- +// Test importing from function scopes. +// Ref: true + +#import enum: item +#import assert.with(true): * + +#enum( + item(1)[First], + item(5)[Fifth] +) +#eq(10, 10) +#ne(5, 6) + --- // A module import without items. #import "module.typ" @@ -59,6 +73,35 @@ // Allow the trailing comma. #import "module.typ": a, c, +--- +// Usual importing syntax also works for function scopes +#import enum +#let d = (e: enum) +#import d.e +#import d.e: item + +#item(2)[a] + +--- +// Can't import from closures. +#let f(x) = x +// Error: 9-10 cannot import from user-defined functions +#import f: x + +--- +// Can't import from closures, despite modifiers. +#let f(x) = x +// Error: 9-18 cannot import from user-defined functions +#import f.with(5): x + +--- +// Error: 9-18 cannot import from user-defined functions +#import () => {5}: x + +--- +// Error: 9-10 expected path, module or function, found integer +#import 5: something + --- // Error: 9-11 failed to load file (is a directory) #import "": name diff --git a/tests/typ/compute/foundations.typ b/tests/typ/compute/foundations.typ index c74a4cd6..9c7b13ca 100644 --- a/tests/typ/compute/foundations.typ +++ b/tests/typ/compute/foundations.typ @@ -40,6 +40,32 @@ // Error: 9-15 expected boolean, found string #assert("true") +--- +// Test failing assertions. +// Error: 11-19 equality assertion failed: value 10 was not equal to 11 +#assert.eq(10, 11) + +--- +// Test failing assertions. +// Error: 11-55 equality assertion failed: 10 and 12 are not equal +#assert.eq(10, 12, message: "10 and 12 are not equal") + +--- +// Test failing assertions. +// Error: 11-19 inequality assertion failed: value 11 was equal to 11 +#assert.ne(11, 11) + +--- +// Test failing assertions. +// Error: 11-57 inequality assertion failed: must be different from 11 +#assert.ne(11, 11, message: "must be different from 11") + +--- +// Test successful assertions. +#assert(5 > 3) +#assert.eq(15, 15) +#assert.ne(10, 12) + --- // Test the `type` function. #test(type(1), "integer") diff --git a/tests/typ/layout/enum.typ b/tests/typ/layout/enum.typ index 2606a64d..a90e1896 100644 --- a/tests/typ/layout/enum.typ +++ b/tests/typ/layout/enum.typ @@ -35,6 +35,18 @@ Empty \ +Nope \ a + 0. +--- +// Test item number overriding. +1. first ++ second +5. fifth + +#enum( + enum.item(1)[First], + [Second], + enum.item(5)[Fifth] +) + --- // Alignment shouldn't affect number #set align(horizon) -- cgit v1.2.3