From beca01c826ee51c9ee6d5eadd7e5ef10f7fb9f58 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 18 Mar 2022 23:36:18 +0100 Subject: Methods --- tests/typ/code/for.typ | 8 +++---- tests/typ/code/if.typ | 8 +++---- tests/typ/code/methods.typ | 50 ++++++++++++++++++++++++++++++++++++++++++ tests/typ/code/ops-invalid.typ | 12 ++-------- tests/typ/code/ops-prec.typ | 2 +- tests/typ/code/ops.typ | 18 +++++++-------- tests/typ/code/target.typ | 2 +- 7 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 tests/typ/code/methods.typ (limited to 'tests/typ/code') diff --git a/tests/typ/code/for.typ b/tests/typ/code/for.typ index e161ba84..822f7423 100644 --- a/tests/typ/code/for.typ +++ b/tests/typ/code/for.typ @@ -32,10 +32,10 @@ // Should output `2345`. #for v in (1, 2, 3, 4, 5, 6, 7) [#if v >= 2 and v <= 5 { repr(v) }] -// Loop over captured arguments. -#let f1(..args) = for v in args { (repr(v),) } -#let f2(..args) = for k, v in args { (repr(k) + ": " + repr(v),) } -#let f(..args) = join(sep: ", ", ..f1(..args), ..f2(..args)) +// Map captured arguments. +#let f1(..args) = args.positional().map(repr) +#let f2(..args) = args.named().pairs((k, v) => repr(k) + ": " + repr(v)) +#let f(..args) = (f1(..args) + f2(..args)).join(", ") #f(1, a: 2) --- diff --git a/tests/typ/code/if.typ b/tests/typ/code/if.typ index 0ab5c495..0d87c689 100644 --- a/tests/typ/code/if.typ +++ b/tests/typ/code/if.typ @@ -60,10 +60,10 @@ #let nth(n) = { str(n) - (if n == 1 { "st" } - else if n == 2 { "nd" } - else if n == 3 { "rd" } - else { "th" }) + if n == 1 { "st" } + else if n == 2 { "nd" } + else if n == 3 { "rd" } + else { "th" } } #test(nth(1), "1st") diff --git a/tests/typ/code/methods.typ b/tests/typ/code/methods.typ new file mode 100644 index 00000000..b5eff78d --- /dev/null +++ b/tests/typ/code/methods.typ @@ -0,0 +1,50 @@ +// Test method calls. +// Ref: false + +--- +// Test whitespace around dot. +#test( "Hi there" . split() , ("Hi", "there")) + +--- +// Test mutating indexed value. +{ + let matrix = (((1,), (2,)), ((3,), (4,))) + matrix(1)(0).push(5) + test(matrix, (((1,), (2,)), ((3, 5), (4,)))) +} + +--- +// Test multiline chain in code block. +{ + let rewritten = "Hello. This is a sentence. And one more." + .split(".") + .map(s => s.trim()) + .filter(s => s != "") + .map(s => s + "!") + .join([\ ]) + + test(rewritten, [Hello!\ This is a sentence!\ And one more!]) +} + +--- +// Error: 2:3-2:16 type array has no method `fun` +#let numbers = () +{ numbers.fun() } + +--- +// Error: 2:3-2:44 cannot mutate a temporary value +#let numbers = (1, 2, 3) +{ numbers.map(v => v / 2).sorted().map(str).remove(4) } + +--- +// Error: 2:3-2:19 cannot mutate a temporary value +#let numbers = (1, 2, 3) +{ numbers.sorted() = 1 } + +--- +// Error: 3-6 cannot mutate a constant +{ box = 1 } + +--- +// Error: 3-6 cannot mutate a constant +{ box.push(1) } diff --git a/tests/typ/code/ops-invalid.typ b/tests/typ/code/ops-invalid.typ index 184e20cf..68bce4af 100644 --- a/tests/typ/code/ops-invalid.typ +++ b/tests/typ/code/ops-invalid.typ @@ -65,19 +65,11 @@ { let x = 1; x += "2" } --- -// Error: 13-14 expected argument list, found integer -{ test with 2 } - ---- -// Error: 3-4 expected function, found integer -{ 1 with () } - ---- -// Error: 3-6 cannot access this expression mutably +// Error: 3-6 cannot mutate a temporary value { (x) = "" } --- -// Error: 3-8 cannot access this expression mutably +// Error: 3-8 cannot mutate a temporary value { 1 + 2 += 3 } --- diff --git a/tests/typ/code/ops-prec.typ b/tests/typ/code/ops-prec.typ index 2cec0d04..23afcc5f 100644 --- a/tests/typ/code/ops-prec.typ +++ b/tests/typ/code/ops-prec.typ @@ -13,7 +13,7 @@ #test(not "b" == "b", false) // Assignment binds stronger than boolean operations. -// Error: 2-7 cannot access this expression mutably +// Error: 2-7 cannot mutate a temporary value {not x = "a"} --- diff --git a/tests/typ/code/ops.typ b/tests/typ/code/ops.typ index 899ee71c..79743f5d 100644 --- a/tests/typ/code/ops.typ +++ b/tests/typ/code/ops.typ @@ -184,21 +184,19 @@ {"a" not} --- -// Test `with` operator. +// Test `with` method. // Apply positional arguments. #let add(x, y) = x + y -#test((add with (2))(4), 6) - -// Let .. with .. syntax. -#let f = add -#let f with (2) -#test(f(4), 6) +#test(add.with(2)(3), 5) +#test(add.with(2).with(3)(), 5) +#test((add.with(2))(4), 6) +#test((add.with(2).with(3))(), 5) // Make sure that named arguments are overridable. #let inc(x, y: 1) = x + y #test(inc(1), 2) -#let inc with (y: 2) -#test(inc(2), 4) -#test(inc(2, y: 4), 6) +#let inc2 = inc.with(y: 2) +#test(inc2(2), 4) +#test(inc2(2, y: 4), 6) diff --git a/tests/typ/code/target.typ b/tests/typ/code/target.typ index 73516817..6c321592 100644 --- a/tests/typ/code/target.typ +++ b/tests/typ/code/target.typ @@ -7,6 +7,6 @@ #let d = 3 #let value = [hi] #let item(a, b) = a + b -#let fn = rect with (fill: conifer, padding: 5pt) +#let fn = rect.with(fill: conifer, padding: 5pt) Some _includable_ text. -- cgit v1.2.3