aboutsummaryrefslogtreecommitdiffstats
path: root/spec/test_setup.moon
blob: 8e1efeec96a65d94372b88aaf39c486aef26830e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import Constant, Scope, Op, Tag from require 'alv'
import Copilot from require 'alv.copilot.base'
import Module, StringModule from require 'alv.module'
import Logger from require 'alv.logger'
import Error from require 'alv.error'
import RTNode from require 'alv.rtnode'

Logger\init 'error'
os.time = do
  t = 0
  ->
    t += 1
    t

export COPILOT

class TestPilot extends Copilot
  new: (code, @preamble='') =>
    super!

    COPILOT = @

    if code
      @active_module = StringModule 'main', @preamble .. code
      @last_modules.__root = @active_module
      @tick!
    else
      @active_module = Module!
      @last_modules.__root = @active_module

  begin_eval: => @active_module.registry\begin_eval!
  end_eval: => @active_module.registry\end_eval!
  next_tick: => @T += 1

  eval_once: (code) =>
    @active_module\spit @preamble .. code
    @tick!
    @active_module.root

  --- poll for changes and tick.
  tick: =>
    return unless @last_modules.__root

    @T += 1

    ok, err = @poll!
    if not ok
      error err

    root = @last_modules.__root
    if root and root.root
      L\set_time 'run'
      ok, error = Error.try "updating", ->
        root.root\poll_io!
        root.root\tick!
      if not ok
        error

  require: (name) =>
    Error.wrap "loading module '#{name}'", ->
      ok, result = pcall require, "alv-lib.#{name}"
      if ok
        result = RTNode :result unless result.__class == RTNode
        result
      else
        error Error 'import', "module not found"

{
  :TestPilot

  do_setup: ->
    TestPilot!
    COPILOT\begin_eval!

  do_teardown: ->
    COPILOT\end_eval!

  invoke_op: (op, tail, scope=Scope!) ->
    import op_invoke from require 'alv.invoke'

    fake_cell =
      head: -> 'test_op'
      tail: -> tail
      tag: Tag.blank!

    op_invoke\eval_cell fake_cell, Scope!, Constant.wrap op
}