vile / examples / context_host / main_test.v
34 lines · 29 sloc · 996 bytes · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module main
2
3import scheme
4
5pub struct HostContext {
6mut:
7 signal chan int
8 reason string
9}
10
11pub fn (mut ctx HostContext) done() chan int { return ctx.signal }
12
13pub fn (mut ctx HostContext) err() IError { return error(ctx.reason) }
14
15fn test_external_custom_context_adapter_borrows_the_original_host_object() {
16 mut ctx := &HostContext{ reason: 'old host reason' }
17 mut vm := scheme.new()
18 vm.register('stop-host', fn [mut ctx] (mut _vm scheme.VM, _args []scheme.Value) !scheme.Value {
19 ctx.reason = 'new host reason'
20 ctx.signal.close()
21 return scheme.unspecified()
22 }) or { panic(err) }
23 if _ := vm.with_context(mut ctx, fn (mut vm scheme.VM) !scheme.Value {
24 return vm.eval('(stop-host) 99')
25 }) {
26 assert false
27 } else {
28 assert err is scheme.CancellationRequest
29 assert scheme.is_control_transfer(err)
30 assert err.msg() == 'evaluation canceled: new host reason'
31 }
32 assert ctx.reason == 'new host reason'
33 assert vm.eval('42') or { panic(err) }.as_integer() or { panic(err) } == 42
34}
35