aboutsummaryrefslogtreecommitdiffstats
path: root/spec/tag_spec.moon
blob: f92dca039d7e5b2c9fcf23d556da6c936b4980c2 (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
88
89
90
91
import do_setup from require 'spec.test_setup'
import Tag from require 'alv.tag'
import Registry from require 'alv.registry'

setup do_setup

describe 'Tag', ->
  describe 'should be constructable', ->
    it 'by parsing', ->
      tag = Tag.parse '2'
      assert tag
      assert.is.equal 2, tag.value
      assert.is.equal '[2]', tag\stringify!
      assert.is.equal '2', tostring tag

    it 'as blank Tags', ->
      tag = Tag.blank!
      assert tag
      assert.is.nil tag.value
      assert.is.equal '', tag\stringify!
      assert.is.equal '?', tostring tag

  describe 'should be clonable', ->
    do_asserts = (tag, expect) ->
      assert tag
      assert.is.nil tag.value
      assert.is.equal expect, tostring tag
      assert.has.error tag\stringify

    it 'from parsed tags', ->
      parent = Tag.parse '1'
      original = Tag.parse '2'
      tag = original\clone parent
      do_asserts tag, '1.2'

    it 'but not from blank tags', ->
      parent = Tag.parse '1'
      original = Tag.blank!
      tag = original\clone parent
      do_asserts tag, '1.?'

    it 'with blank parent', ->
      parent = Tag.blank!
      original = Tag.parse '2'
      tag = original\clone parent
      do_asserts tag, '?.2'

    it 'completely blank', ->
      parent = Tag.blank!
      original = Tag.blank!
      tag = original\clone parent
      do_asserts tag, '?.?'

  describe 'should be set-able', ->
    it 'only if blank',  ->
      tag = Tag.parse '42'
      assert.has.error -> tag\set 43

      clone = tag\clone Tag.parse '3'
      assert.has.error -> clone\set 42

      clone = tag\clone Tag.blank!
      assert.has.error -> clone\set 42

    it 'and stores the value', ->
      blank = Tag.blank!
      blank\set 12

      assert.is.equal blank.value, 12

    it 'sets the original if cloned', ->
      original = Tag.blank!
      parent = Tag.parse '7'

      o_set = spy.on original, 'set'
      p_set = spy.on parent, 'set'

      clone = original\clone parent
      clone\set 11

      assert.spy(o_set).was_called_with (match.is_ref original), 11
      assert.spy(p_set).was_not_called!

      assert.is.equal original.value, 11
 
    it 'requires the parent to be registered if cloned', ->
      original = Tag.blank!
      parent = Tag.blank!

      clone = original\clone parent
      assert.has.error -> clone\set 11