aboutsummaryrefslogtreecommitdiffstats
path: root/spec/core/cell_spec.moon
blob: d16c9d870a1f16cb1d890d075316bb676f1d57c7 (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
import Cell, RootCell from require 'core.cell'
import ValueStream, Scope, Tag, SimpleRegistry, globals from require 'core'
import Logger from require 'logger'
Logger.init 'silent'

hello_world = Cell.parse (Tag.parse '2'), { '', (ValueStream.sym 'hello'), ' ', (ValueStream.str 'world'), '' }
two_plus_two = Cell.parse (Tag.parse '3'), { '', (ValueStream.sym '+'), ' ', (ValueStream.num 2), ' ', (ValueStream.num 2), '' }

reg = SimpleRegistry!
setup -> reg\grab!
teardown -> reg\release!

describe 'Cell', ->
  describe 'when quoted', ->
    with hello_world\quote!
      it 'stays equal', ->
        assert.is.equal Cell, .__class
        assert.is.equal (ValueStream.sym 'hello'), \head!
        assert.is.same { ValueStream.str 'world' }, \tail!

      it 'shares the tag', ->
        assert.is.equal hello_world.tag, .tag

    with two_plus_two\quote!
      it 'stays equal', ->
        assert.is.equal Cell, .__class
        assert.is.equal (ValueStream.sym '+'), \head!
        assert.is.same { (ValueStream.num 2), (ValueStream.num 2) }, \tail!

      it 'shares the tag', ->
        assert.is.equal two_plus_two.tag, .tag

  describe 'when cloned', ->
    parent = Tag.blank '1'
    with hello_world\clone parent
      it 'keeps children', ->
        assert.is.equal Cell, .__class
        assert.is.equal (ValueStream.sym 'hello'), \head!
        assert.is.same { ValueStream.str 'world' }, \tail!

      it 'clones the tag', ->
        assert.is.equal hello_world.tag, .tag.original
        assert.is.equal parent, .tag.parent

  describe 'when evaluated', ->
    it 'errors when empty', ->
      cell = Cell.parse {''}
      assert.has.error -> cell\eval globals

    it 'evaluates its head', ->
      head = ValueStream.sym 'trace'
      cell = Cell.parse { '', head, ' ', (ValueStream.sym 'true'), '' }

      s = spy.on head, 'eval'
      cell\eval globals
      assert.spy(s).was_called_with (match.is_ref head), (match.is_ref globals)

describe 'RootCell', ->
  test 'tag is always [0]', ->
    cell = Cell.parse_root {}
    assert.is.equal '[0]', cell.tag\stringify!

  test 'head is always "do"', ->
    cell = Cell.parse_root {}
    assert.is.equal (ValueStream.sym 'do'), cell\head!

    cell = RootCell nil, { hello_world, two_plus_two }
    assert.is.equal (ValueStream.sym 'do'), cell\head!

  test 'tail is all children', ->
    cell = Cell.parse_root {}
    assert.is.same {}, cell\tail!

    cell = RootCell nil, { hello_world, two_plus_two }
    assert.is.same { hello_world, two_plus_two },
                   cell\tail!