-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtestKernel.js
More file actions
184 lines (157 loc) · 4.75 KB
/
testKernel.js
File metadata and controls
184 lines (157 loc) · 4.75 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @license Copyright (c) 2011 Cello Software, LLC.
* All rights reserved.
* Available via the new BSD License.
*/
/*jshint
asi: false, bitwise: false, boss: false, curly: true, eqeqeq: true, eqnull: false, es5: true,
evil: false, expr: true, forin: true, globalstrict: false, immed: true, indent: 4, latedef: true,
laxbreak: false, loopfunc: true, maxlen: 100, newcap: true, noarg: true, noempty: true,
nonew: true, nomen: false, onevar: true, passfail: false, plusplus: false, shadow: false,
strict: false, sub: false, trailing: true, undef: true, white: true
*/
/*global define: false, require: false*/
define([
'test/promiseTestCase',
'assert',
'twine/Kernel',
'twine/support/promise',
'twine/util/error',
'twine/model/Registry',
'twine/model/Builder'
], function (testCase, assert, Kernel, promise, error, ModelRegistry, ModelBuilder) {
'use strict';
return testCase({
setUp: function () {
this.builder = new ModelBuilder();
this.registry = new ModelRegistry();
this.k = new Kernel({
modelBuilder: this.builder,
modelRegistry: this.registry
});
},
tearDown: function () {
this.k.destroy();
},
'test kernel builds a default model registry': function () {
var k = new Kernel();
assert.ok(k.modelRegistry instanceof ModelRegistry);
},
'test kernel builds a default model builder': function () {
var k = new Kernel();
assert.ok(k.modelBuilder instanceof ModelBuilder);
},
'test kernel accepts an alternative model registry': function () {
var r = {},
k = new Kernel({
modelRegistry: r
});
assert.equal(k.modelRegistry, r);
},
'test kernel accepts an alternative model builder': function () {
var b = {},
k = new Kernel({
modelBuilder: b
});
assert.equal(k.modelBuilder, b);
},
'test fiber must have an id': function () {
var fiber = {},
k = this.k;
assert.throws(function () {
k.addFiber(fiber);
}, error.MissingId);
},
'test fiber must have a unique id': function () {
var f1 = {
id: 'dup',
init: function () {}
},
f2 = {
id: 'dup',
init: function () {}
},
k = this.k;
k.addFiber(f1);
assert.throws(function () {
k.addFiber(f2);
}, error.DuplicateFiber);
},
'test fiber is initialized when added': function () {
var expected = {},
actual,
init = this.mock().once().withExactArgs(this.k).returns(expected),
fiber = {
id: 'fiber',
init: init
};
actual = this.k.addFiber(fiber);
assert.ok(init.verify());
assert.equal(actual, expected);
},
'test addComponentModel processes config and adds model to the registry': function () {
var config = {},
model = {},
process = this.mock(this.builder).expects('process').once()
.withExactArgs(config).returns(model),
addModel = this.mock(this.registry).expects('addModel').once()
.withExactArgs(model).returns(model);
return promise.when(this.k.addComponentModel(config), function (m) {
assert.ok(process.verify());
assert.ok(addModel.verify());
assert.equal(m, model, 'model should be resolved');
});
},
'test resolve gets model from registry and resolves it': function () {
var spec = {},
args = {},
instance = {},
model = {
resolve: this.mock().once().withExactArgs(args).returns(instance)
},
getModel = this.mock(this.registry).expects('getModel').once()
.withExactArgs(spec).returns(model);
return promise.when(this.k.resolve(spec, args), function (component) {
assert.ok(model.resolve.verify());
assert.ok(getModel.verify());
assert.equal(component, instance);
});
},
'test release gets model from registry and releases it': function () {
var instance = {},
expected = {},
model = {
release: this.mock().once().returns(expected)
},
getModel = this.mock(this.registry).expects('getModel').once().returns(model);
return promise.when(this.k.release(instance), function (actual) {
assert.ok(model.release.verify());
assert.ok(getModel.verify());
assert.equal(getModel.getCall(0).args[0].instance, instance);
assert.equal(actual, expected);
});
},
'test destroy terminates fibers': function () {
var fiber = {
id: 'terminate',
init: this.spy(),
terminate: this.spy()
};
this.k.addFiber(fiber);
this.k.destroy();
assert.ok(fiber.terminate.called);
},
'test destroys model registry': function () {
var destroy = this.mock(this.registry).expects('destroy').once();
this.k.destroy();
assert.ok(destroy.verify());
this.registry.destroy.restore();
},
'test destroys model builder': function () {
var destroy = this.mock(this.builder).expects('destroy').once();
this.k.destroy();
assert.ok(destroy.verify());
this.builder.destroy.restore();
}
});
});