-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
131 lines (91 loc) · 2.88 KB
/
test.js
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
var createPlugins = require('./');
var test = require('tape');
function FakeGame() {
}
function createPluginFoo(game, opts) {
function PluginFoo(game, opts) {
console.log('PluginFoo loading');
}
return new PluginFoo(game, opts);
}
function createPluginBar(game, opts) {
function PluginBar(game, opts) {
console.log('PluginBar loading');
this.myfoo = game.plugins.get('foo');
}
return new PluginBar(game, opts);
}
createPluginBar.pluginInfo = {
loadAfter: ['foo']
};
function fakeRequire(name) {
console.log('require '+name);
return {foo: createPluginFoo, bar: createPluginBar}[name];
}
test('game.plugins set', function(t) {
var game = new FakeGame();
var plugins = createPlugins(game, {require:fakeRequire});
t.equals(game.plugins, plugins);
t.end();
});
test('plugin add simple', function(t) {
var plugins = createPlugins(new FakeGame(), {require:fakeRequire});
plugins.add('foo', {});
plugins.loadAll();
t.equals(plugins.list()[0], 'foo');
t.end();
});
test('plugin add simple loaders', function(t) {
var plugins = createPlugins(new FakeGame(), {loaders: {
foo: createPluginFoo,
bar: createPluginBar}});
plugins.add('foo', {});
plugins.loadAll();
t.equals(plugins.list()[0], 'foo');
t.end();
});
test('plugin add fail missing opts', function(t) {
var plugins = createPlugins(new FakeGame(), {require:fakeRequire});
t.throws(function() {
plugins.add('foo'); // options are mandatory
});
t.end();
});
test('enable/disable', function(t) {
var plugins = createPlugins(new FakeGame(), {require:fakeRequire});
plugins.add('foo', {});
plugins.loadAll();
t.equals(plugins.isEnabled('foo'), true); // starts enabled
plugins.disable('foo');
t.equals(plugins.isEnabled('foo'), false);
plugins.enable('foo');
t.equals(plugins.isEnabled('foo'), true);
plugins.disable('foo');
t.equals(plugins.isEnabled('foo'), false);
t.end();
});
test('on demand', function(t) {
var plugins = createPlugins(new FakeGame(), {require:fakeRequire});
plugins.add('foo', {onDemand: true});
plugins.loadAll();
t.equals(plugins.get('foo') === undefined, true); // doesn't exist yet
plugins.enable('foo');
t.equals(plugins.get('foo') !== undefined, true);
t.equals(plugins.isEnabled('foo'), true);
t.end();
});
test('loadAfter', function(t) {
var plugins = createPlugins(new FakeGame(), {require:fakeRequire});
plugins.add('foo', {});
plugins.add('bar', {});
plugins.loadAll();
// try reversed too, should have same order because of loadAfter
var rplugins = createPlugins(new FakeGame(), {require:fakeRequire});
rplugins.add('bar', {});
rplugins.add('foo', {});
rplugins.loadAll();
// ensure PluginBar was able to get its PluginFoo instance, as foo was loaded before bar
t.equal(plugins.get('bar').myfoo, plugins.get('foo'));
t.equal(rplugins.get('bar').myfoo, rplugins.get('foo'));
t.end();
});