vile / scheme / reader_identifier_test.v
74 lines · 68 sloc · 2.54 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn identifier_diagnostic_eval(mut vm VM, source string, space Value, namespace bool) !Value {
6 if namespace { return vm.eval_in(source, space) }
7 return vm.eval(source)
8}
9
10fn test_external_identifier_failures_display_public_spelling_without_encoded_keys() {
11 mut vm := new()
12 space := vm.new_environment() or { panic(err.msg()) }
13 for source, expected in {
14 '||': 'unbound variable ||'
15 r'|\x0;missing|': r'unbound variable |\x0;missing|'
16 r'|\x0;source/name|': r'unbound variable |\x0;source/name|'
17 '|name with spaces|': 'unbound variable |name with spaces|'
18 } {
19 for namespace in [false, true] {
20 if _ := identifier_diagnostic_eval(mut vm, source, space, namespace) {
21 assert false, source
22 } else {
23 assert err is EvalError
24 if err is EvalError {
25 assert err.message == expected, err.message
26 assert !err.message.contains('\x00source/'), err.message
27 }
28 }
29 }
30 }
31}
32
33fn test_external_nul_identifiers_do_not_resolve_or_overwrite_private_bindings() {
34 mut vm := new()
35 space := vm.interaction_environment() or { panic(err.msg()) }
36 for raw in ['\x00123.4/temporary', '\x00source/\x00123.4/temporary', '\x00'] {
37 vm.bind_global(raw, integer(99))
38 name := text_value(.symbol, raw)
39 if _ := vm.eval_value(name, space) {
40 assert false, 'external identifier resolved a private key'
41 }
42 definition := list([text_value(.symbol, 'define'), name, integer(42)])
43 vm.eval_value(definition, space) or { panic(err.msg()) }
44 assert vm.eval_value(name, space) or { panic(err.msg()) }.whole() == 42
45 assert vm.global_lookup(raw) or { panic(err.msg()) }.whole() == 99
46 literal := vm.eval_value(list([text_value(.symbol, 'quote'), name]), space) or { panic(err.msg()) }
47 assert literal.tag == .symbol && literal.text() == raw
48 }
49 assert vm.active.len == 0
50}
51
52fn check_identifier_views(raw string, encoded string) {
53 mut region := arena.new()
54 defer { region.free() }
55 region.push()
56 before := region.used()
57 for _ in 0 .. 1000 {
58 normal := 'normal-name'
59 assert external_identifier_key(normal).str == normal.str
60 spelling := external_identifier_spelling(encoded)
61 assert spelling == raw
62 assert spelling.str == unsafe { encoded.str + external_identifier_prefix.len }
63 }
64 assert region.used() == before
65 region.pop()
66}
67
68fn test_frozen_macro_spelling_and_normal_identifier_views_do_not_copy() {
69 raw := '\x0051.2/name'
70 encoded := external_identifier_key(raw)
71 check_identifier_views(raw, encoded)
72 // The owning key survives the allocation region used by the probe.
73 assert external_identifier_spelling(encoded) == raw
74}
75