From eaa3cbaa9c2b1564a4b0db013672245a1893314a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 12 Aug 2021 13:39:33 +0200 Subject: Array and dictionary indexing --- tests/typ/code/array.typ | 36 ++++++++++++++++++++++++++++++++++++ tests/typ/code/call.typ | 8 ++++---- tests/typ/code/dict.typ | 28 ++++++++++++++++++++++++++++ tests/typ/code/ops-invalid.typ | 20 +++++++++++++++++--- tests/typ/code/ops-prec.typ | 2 +- 5 files changed, 86 insertions(+), 8 deletions(-) (limited to 'tests/typ/code') diff --git a/tests/typ/code/array.typ b/tests/typ/code/array.typ index fc8795c2..df37dd45 100644 --- a/tests/typ/code/array.typ +++ b/tests/typ/code/array.typ @@ -1,6 +1,9 @@ // Test arrays. +// Ref: false --- +// Ref: true + // Empty. {()} @@ -18,6 +21,39 @@ , rgb("002") ,)} +--- +// Test lvalue and rvalue access. +{ + let array = (1, 2) + array(1) += 5 + array(0) + test(array, (1, 8)) +} + +--- +// Test rvalue out of bounds. +{ + let array = (1, 2, 3) + // Error: 3-11 array index out of bounds (index: 5, len: 3) + array(5) +} + +--- +// Test lvalue out of bounds. +{ + let array = (1, 2, 3) + // Error: 3-12 array index out of bounds (index: -1, len: 3) + array(-1) = 5 +} + +--- +// Test non-collection indexing. + +{ + let x = 10pt + // Error: 3-4 expected collection, found length + x() = 1 +} + --- // Error: 3 expected closing paren {(} diff --git a/tests/typ/code/call.typ b/tests/typ/code/call.typ index 118382e4..92ac17ae 100644 --- a/tests/typ/code/call.typ +++ b/tests/typ/code/call.typ @@ -68,25 +68,25 @@ C } --- -// Error: 2-6 expected function, found boolean +// Error: 2-6 expected function or collection, found boolean {true()} --- #let x = "x" -// Error: 1-3 expected function, found string +// Error: 1-3 expected function or collection, found string #x() --- #let f(x) = x -// Error: 1-6 expected function, found integer +// Error: 1-6 expected function or collection, found integer #f(1)(2) --- #let f(x) = x -// Error: 1-6 expected function, found template +// Error: 1-6 expected function or collection, found template #f[1](2) --- diff --git a/tests/typ/code/dict.typ b/tests/typ/code/dict.typ index b775f4af..b369b8b6 100644 --- a/tests/typ/code/dict.typ +++ b/tests/typ/code/dict.typ @@ -1,12 +1,40 @@ // Test dictionaries. +// Ref: false --- +// Ref: true + // Empty {(:)} // Two pairs. {(a1: 1, a2: 2)} +--- +// Test lvalue and rvalue access. +{ + let dict = (a: 1, b: 1) + dict("b") += 1 + dict("c") = 3 + test(dict, (a: 1, b: 2, c: 3)) +} + +--- +// Test rvalue missing key. +{ + let dict = (a: 1, b: 2) + // Error: 11-20 dictionary does not contain key: "c" + let x = dict("c") +} + +--- +// Missing lvalue is automatically none-initialized. +{ + let dict = (:) + // Error: 3-17 cannot add none and integer + dict("b") += 1 +} + --- // Simple expression after already being identified as a dictionary. // Error: 9-10 expected named pair, found expression diff --git a/tests/typ/code/ops-invalid.typ b/tests/typ/code/ops-invalid.typ index 5e56ff98..e1662ddd 100644 --- a/tests/typ/code/ops-invalid.typ +++ b/tests/typ/code/ops-invalid.typ @@ -46,6 +46,20 @@ // Error: 2-10 cannot divide integer by length {3 / 12pt} +--- +// Error: 2-9 cannot repeat this string -1 times +{-1 * ""} + +--- +{ + let x = 2 + for _ in 0..60 { + x *= 2 + } + // Error: 4-18 cannot repeat this string 4611686018427387904 times + {x * "abcdefgh"} +} + --- // Error: 14-22 cannot add integer and string { let x = 1; x += "2" } @@ -63,11 +77,11 @@ { 1 .. "" } --- -// Error: 3-6 cannot assign to this expression +// Error: 3-6 cannot access this expression mutably { (x) = "" } --- -// Error: 3-8 cannot assign to this expression +// Error: 3-8 cannot access this expression mutably { 1 + 2 += 3 } --- @@ -75,7 +89,7 @@ { z = 1 } --- -// Error: 3-7 cannot assign to a constant +// Error: 3-7 cannot mutate a constant { rect = "hi" } --- diff --git a/tests/typ/code/ops-prec.typ b/tests/typ/code/ops-prec.typ index e64e583c..2cec0d04 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 assign to this expression +// Error: 2-7 cannot access this expression mutably {not x = "a"} --- -- cgit v1.2.3