/
config.test.js
91 lines (73 loc) · 2.34 KB
/
config.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
/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */
var should = require('should');
var config = require(__dirname + '/../lib/config.js');
var _storer = function (data) {
var _me = {};
_me.get = function (key, callback) {
key = key.replace(/\/{2,}/g, '/');
if (key.indexOf('error') > -1) {
return null;
} else {
return {
data : data[key],
meta : 'fake meta'
};
}
};
_me.watch = function (key, timeout, callback) {
setTimeout(function () {
callback(key.indexOf('watcherror') > -1 ? new Error('WatchError') : null, Date.now());
}, timeout || 10);
};
_me.sync = function (prefix, callback) {
callback(prefix.indexOf('syncerror') > -1 ? new Error('SyncError') : null);
};
return _me;
};
describe('config interface', function () {
/* {{{ should_config_get_value_works_fine() */
it('should_config_get_value_works_fine', function (done) {
var num = 5;
var cfg = {
'timeout' : 10,
};
var _me = config.create('/app1/app2/', cfg, _storer({
'/config/app1/app2/key1' : 'AbCd1我asd',
'/config/app1/app2/key2' : '-123.3123',
'/config/app1/app2/key3' : '; this is comment\na = "b"\n[section1]\n\n\n\na = -1231.3 \r\nbb\r\nc="\\\'12"',
'/config/app3/app2/key1' : 'abcd',
}));
_me.setEventHandle('change', function (rev) {
should.ok(rev);
if ((--num) === 0) {
done();
}
});
var data = _me.get('key1');
data.should.eql('AbCd1我asd');
data = _me.get('key2', 'number');
data.should.eql(-123.3123);
data = _me.get('key3', 'ini');
JSON.stringify(data).should.eql(JSON.stringify({
'a' : 'b',
'section1' : {'a' : -1231.3, 'c' : '\'12'}
}));
data = _me.get('error');
should.ok(!data);
done();
});
/* }}} */
/* {{{ should_config_set_event_handle_works_fine() */
it('should_config_set_event_handle_works_fine', function (done) {
var _me = config.create('/syncerror', {'timeout' : 5}, _storer({}));
_me.setEventHandle('error', function (error) {
error.toString().should.include('SyncError');
var _me = config.create('/watcherror', {'timeout' : 5}, _storer({}));
_me.setEventHandle('error', function (error) {
error.toString().should.include('WatchError');
done();
});
});
});
/* }}} */
});