1
|
|
|
/* eslint-env mocha */ |
2
|
|
|
/* eslint-disable no-unused-expressions */ |
3
|
|
|
|
4
|
|
|
var Sinon = require('sinon') |
5
|
|
|
var Chai = require('chai') |
6
|
|
|
var expect = Chai.expect |
7
|
|
|
var Commons = require('../../../../lib/logger/_common') |
8
|
|
|
var Context = Commons.Context |
9
|
|
|
var Level = Commons.Level |
10
|
|
|
|
11
|
|
|
describe('Unit', function () { |
12
|
|
|
describe('/logger', function () { |
13
|
|
|
describe('/_common.js', function () { |
14
|
|
|
var context |
15
|
|
|
var writer |
16
|
|
|
var level |
17
|
|
|
|
18
|
|
|
beforeEach(function () { |
19
|
|
|
writer = { |
20
|
|
|
write: Sinon.spy(function (message) { |
21
|
|
|
return message |
22
|
|
|
}) |
23
|
|
|
} |
24
|
|
|
level = Level.All |
25
|
|
|
context = new Context(level, writer) |
26
|
|
|
}) |
27
|
|
|
|
28
|
|
|
describe('.Context', function () { |
29
|
|
|
describe('#getWriter', function () { |
30
|
|
|
it('returns default writer by default', function () { |
31
|
|
|
expect(context.getWriter()).to.eq(writer) |
32
|
|
|
expect(context.getWriter('package')).to.eq(writer) |
33
|
|
|
}) |
34
|
|
|
}) |
35
|
|
|
|
36
|
|
|
describe('#setWriter', function () { |
37
|
|
|
it('sets provided writer as default if path is omitted', function () { |
38
|
|
|
var writer = {} |
39
|
|
|
context.setWriter(writer) |
40
|
|
|
expect(context.getWriter()).to.eq(writer) |
41
|
|
|
}) |
42
|
|
|
|
43
|
|
|
it('sets writer provided during construction as default', function () { |
44
|
|
|
expect(context.getWriter()).to.eq(writer) |
45
|
|
|
expect(context.getWriter('package.package')).to.eq(writer) |
46
|
|
|
}) |
47
|
|
|
|
48
|
|
|
it('returns writer set for package for direct and child paths', function () { |
49
|
|
|
var parent = 'package' |
50
|
|
|
var path = parent += '.package' |
51
|
|
|
var child = path += '.package' |
52
|
|
|
var overwriter = {} |
53
|
|
|
context.setWriter(path, overwriter) |
54
|
|
|
expect(context.getWriter()).to.eq(writer) |
55
|
|
|
expect(context.getWriter(parent)).to.eq(writer) |
56
|
|
|
expect(context.getWriter(path)).to.eq(overwriter) |
57
|
|
|
expect(context.getWriter(child)).to.eq(overwriter) |
58
|
|
|
}) |
59
|
|
|
}) |
60
|
|
|
|
61
|
|
|
describe('#removeWriter', function () { |
62
|
|
|
it('allows to remove previously-set writer', function () { |
63
|
|
|
var path = 'package' |
64
|
|
|
var writer = {} |
65
|
|
|
context.setWriter(path, writer) |
66
|
|
|
expect(context.getWriter(path)).to.eq(writer) |
67
|
|
|
expect(context.removeWriter(path)).to.eq(writer) |
68
|
|
|
expect(context.getWriter(path)).not.to.eq(writer) |
69
|
|
|
}) |
70
|
|
|
|
71
|
|
|
it('returns null on non-existing writer', function () { |
72
|
|
|
expect(context.removeWriter('package')).to.be.null |
73
|
|
|
}) |
74
|
|
|
|
75
|
|
|
it('throws if path is omitted', function () { |
76
|
|
|
var lambda = function () { |
77
|
|
|
context.removeWriter() |
78
|
|
|
} |
79
|
|
|
expect(lambda).to.throw() |
80
|
|
|
}) |
81
|
|
|
}) |
82
|
|
|
|
83
|
|
|
describe('#getLevel', function () { |
84
|
|
|
it('returns default level if no path is specified', function () { |
85
|
|
|
expect(context.getLevel()).to.eq(level) |
86
|
|
|
}) |
87
|
|
|
|
88
|
|
|
it('returns default level if no overrides are in effect', function () { |
89
|
|
|
expect(context.getLevel('package')).to.eq(level) |
90
|
|
|
}) |
91
|
|
|
}) |
92
|
|
|
|
93
|
|
|
describe('#setLevel', function () { |
94
|
|
|
it('overrides level for specified path and all child paths', function () { |
95
|
|
|
var parent = 'package' |
96
|
|
|
var path = parent += '.package' |
97
|
|
|
var child = path += '.package' |
98
|
|
|
var override = Level.Warn |
99
|
|
|
context.setLevel(path, override) |
100
|
|
|
expect(context.getLevel()).to.eq(level) |
101
|
|
|
expect(context.getLevel(parent)).to.eq(level) |
102
|
|
|
expect(context.getLevel(path)).to.eq(override) |
103
|
|
|
expect(context.getLevel(child)).to.eq(override) |
104
|
|
|
}) |
105
|
|
|
|
106
|
|
|
it('sets default level if no path provided', function () { |
107
|
|
|
var override = Level.Warn |
108
|
|
|
context.setLevel(override) |
109
|
|
|
expect(context.getLevel()).to.eq(override) |
110
|
|
|
}) |
111
|
|
|
}) |
112
|
|
|
|
113
|
|
|
describe('#removeLevel', function () { |
114
|
|
|
it('allows to remove preinstalled level', function () { |
115
|
|
|
var path = 'package' |
116
|
|
|
var override = Level.Warn |
117
|
|
|
context.setLevel(path, override) |
118
|
|
|
expect(context.removeLevel(path)).to.eq(override) |
119
|
|
|
expect(context.getLevel(path)).to.eq(level) |
120
|
|
|
}) |
121
|
|
|
|
122
|
|
|
it('returns null for nonexisting path', function () { |
123
|
|
|
var path = 'package' |
124
|
|
|
expect(context.removeLevel(path)).to.be.null |
125
|
|
|
}) |
126
|
|
|
|
127
|
|
|
it('throws if called without path', function () { |
128
|
|
|
var lambda = function () { |
129
|
|
|
context.removeLevel() |
130
|
|
|
} |
131
|
|
|
expect(lambda).to.throw() |
132
|
|
|
}) |
133
|
|
|
}) |
134
|
|
|
}) |
135
|
|
|
}) |
136
|
|
|
}) |
137
|
|
|
}) |
138
|
|
|
|