typst/tests/typ/compiler/dict.typ
2023-01-27 11:54:30 +01:00

91 lines
1.8 KiB
Typst

// Test dictionaries.
// Ref: false
---
// Ref: true
// Empty
#{(:)}
// Two pairs and string key.
#let dict = (normal: 1, "spacy key": 2)
#dict
#test(dict.normal, 1)
#test(dict.at("spacy key"), 2)
---
// Test lvalue and rvalue access.
#{
let dict = (a: 1, "b b": 1)
dict.at("b b") += 1
dict.state = (ok: true, err: false)
test(dict, (a: 1, "b b": 2, state: (ok: true, err: false)))
test(dict.state.ok, true)
dict.at("state").ok = false
test(dict.state.ok, false)
test(dict.state.err, false)
}
---
// Test rvalue missing key.
#{
let dict = (a: 1, b: 2)
// Error: 11-23 dictionary does not contain key "c"
let x = dict.at("c")
}
---
// Missing lvalue is not automatically none-initialized.
#{
let dict = (:)
// Error: 3-9 dictionary does not contain key "b"
dict.b += 1
}
---
// Test dictionary methods.
#let dict = (a: 3, c: 2, b: 1)
#test("c" in dict, true)
#test(dict.len(), 3)
#test(dict.values(), (3, 1, 2))
#test(dict.pairs((k, v) => k + str(v)).join(), "a3b1c2")
#{ dict.remove("c") }
#test("c" in dict, false)
#test(dict, (a: 3, b: 1))
---
// Error: 25-30 duplicate key
#{(first: 1, second: 2, first: 3)}
---
// Error: 18-21 duplicate key
#{(a: 1, "b": 2, "a": 3)}
---
// Simple expression after already being identified as a dictionary.
// Error: 10-11 expected named or keyed pair, found identifier
#{(a: 1, b)}
// Identified as dictionary due to initial colon.
// Error: 5-6 expected named or keyed pair, found integer
// Error: 6 expected comma
// Error: 13-17 expected identifier or string, found boolean
// Error: 18 expected expression
#{(:1 b:"", true:)}
// Error: 4-9 expected identifier or string, found binary expression
#{(a + b: "hey")}
---
// Error: 4-16 cannot mutate a temporary value
#{ (key: "val").other = "some" }
---
#{
let object = none
// Error: 3-9 expected dictionary, found none
object.property = "value"
}