Completed
Push — dev ( d30456...4fd03f )
by Fike
33s
created

test/suites/unit/logger/_common.TreeNode.spec.js   A

Complexity

Total Complexity 35
Complexity/F 1.09

Size

Lines of Code 172
Function Count 32

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 172
rs 9
wmc 35
mnd 0
bc 32
fnc 32
bpm 1
cpm 1.0936
noi 3
1
/* eslint-env mocha */
2
/* eslint-disable no-unused-expressions */
3
4
var Chai = require('chai')
5
var expect = Chai.expect
6
var Commons = require('../../../../lib/logger/_common')
7
var TreeNode = Commons.TreeNode
8
9
describe('Unit', function () {
10
  describe('/logger', function () {
11
    describe('/_common.js', function () {
12
      describe('.TreeNode', function () {
13
        describe('#get', function () {
14
          it('returns stored value', function () {
15
            var tree = new TreeNode(1)
16
            expect(tree.get()).to.eq(1)
17
          })
18
          it('returns null if value is not set', function () {
19
            var tree = new TreeNode()
20
            expect(tree.get()).to.eq(null)
21
          })
22
        })
23
24
        describe('#set', function () {
25
          it('overrides stored value', function () {
26
            var tree = new TreeNode(1)
27
            tree.set(2)
28
            expect(tree.get()).to.eq(2)
29
          })
30
        })
31
32
        describe('#child', function () {
33
          it('returns added child', function () {
34
            var tree = new TreeNode(1)
35
            var child = new TreeNode(2)
36
            expect(tree.addChild('two', child)).to.eq(child)
37
            expect(tree.child('two')).to.eq(child)
38
          })
39
        })
40
41
        describe('#removeChild', function () {
42
          it('returns null for nonexisting child', function () {
43
            var tree = new TreeNode(1)
44
            expect(tree.removeChild('package')).to.be.null
45
          })
46
47
          it('deletes and returns existing child', function () {
48
            var tree = new TreeNode(1)
49
            var child = tree.addChild('package', new TreeNode(2))
50
            expect(tree.removeChild('package')).to.eq(child)
51
          })
52
        })
53
54
        describe('#branch', function () {
55
          it('returns full branch if it exists', function () {
56
            var path = ['alpha', 'beta']
57
            var root = new TreeNode()
58
            var child = root.addChild('alpha', new TreeNode())
59
            var grandchild = child.addChild('beta', new TreeNode())
60
            var result = root.branch(path)
61
62
            expect(result).to.have.lengthOf(3)
63
            expect(result[0]).to.eq(root)
64
            expect(result[1]).to.eq(child)
65
            expect(result[2]).to.eq(grandchild)
66
          })
67
68
          it('returns as much as present if not every part of path exists', function () {
69
            var path = ['alpha', 'beta']
70
            var root = new TreeNode()
71
            var child = root.addChild('alpha', new TreeNode())
72
            child.addChild('delta', new TreeNode())
73
            var result = root.branch(path)
74
75
            expect(result).to.have.lengthOf(2)
76
            expect(result[0]).to.eq(root)
77
            expect(result[1]).to.eq(child)
78
          })
79
80
          it('returns just root node for non-existing path', function () {
81
            var path = ['alpha', 'beta']
82
            var root = new TreeNode()
83
            var child = root.addChild('gamma', new TreeNode())
84
            child.addChild('delta', new TreeNode())
85
            var result = root.branch(path)
86
87
            expect(result).to.deep.eq([root])
88
          })
89
        })
90
91
        describe('#traverse', function () {
92
          it('returns root for empty path', function () {
93
            var root = new TreeNode(1)
94
            expect(root.traverse([])).to.eq(root)
95
          })
96
97
          it('returns null for nonexisting path', function () {
98
            var root = new TreeNode(1)
99
            expect(root.traverse(['package'])).to.be.null
100
          })
101
102
          it('returns target node for existing path', function () {
103
            var root = new TreeNode(1)
104
            var child = root.addChild('package', new TreeNode(2))
105
            child.addChild('subpackage', new TreeNode(3))
106
            expect(root.traverse(['package'])).to.eq(child)
107
          })
108
        })
109
110
        describe('#put', function () {
111
          it('puts value at target path', function () {
112
            var tree = new TreeNode(1)
113
            tree.addChild('segment', new TreeNode(2))
114
            var value = 4
115
            tree.put(['segment', 'segment', 'segment'], value)
116
            var middleware = tree.child('segment')
117
            expect(middleware).to.be.instanceOf(TreeNode)
118
            middleware = middleware.child('segment')
119
            expect(middleware).to.be.instanceOf(TreeNode)
120
            var target = middleware.child('segment')
121
            expect(target).to.be.instanceOf(TreeNode)
122
            expect(target.get()).to.eq(value)
123
          })
124
        })
125
126
        describe('#retrieve', function () {
127
          it('retrieves the value for existing branch', function () {
128
            var root = new TreeNode(1)
129
            var child = root.addChild('alpha', new TreeNode(2))
130
            var grandchild = child.addChild('beta', new TreeNode(3))
131
            var path = ['alpha', 'beta']
132
            var result = root.retrieve(path)
133
134
            expect(result).to.eq(grandchild.get())
135
          })
136
137
          it('retrieves far-most value for partially existing branch', function () {
138
            var root = new TreeNode(1)
139
            var child = root.addChild('alpha', new TreeNode(2))
140
            child.addChild('delta', new TreeNode(3))
141
            var path = ['alpha', 'beta']
142
            var result = root.retrieve(path)
143
144
            expect(result).to.eq(child.get())
145
          })
146
        })
147
148
        describe('#remove', function () {
149
          it('removes and returns value at path', function () {
150
            var tree = new TreeNode(1)
151
            var middleware = tree.addChild('package', new TreeNode(2))
152
            var target = middleware.addChild('package', new TreeNode(3))
153
154
            expect(tree.remove(['package', 'package'])).to.eq(target.get())
155
          })
156
157
          it('returns null for nonexisting path', function () {
158
            var tree = new TreeNode(1)
159
160
            expect(tree.remove(['package', 'package'])).to.be.null
161
          })
162
163
          it('disallows root node removal', function () {
164
            var tree = new TreeNode(1)
165
            var lambda = function () {
166
              tree.remove([])
167
            }
168
169
            expect(lambda).to.throw()
170
          })
171
        })
172
      })
173
    })
174
  })
175
})
176