Permalink
Browse files

Type registration functions return their status

Reports back whether register/merge/remove was successful.
  • Loading branch information...
1 parent 6c61d0f commit 9ddc37cd69f2afcec933954c552b6037e74daa3c @DarthJDG committed Sep 4, 2016
Showing with 11 additions and 6 deletions.
  1. +3 −0 mangler.js
  2. +8 −6 test/test.js
View
@@ -197,13 +197,15 @@ var Mangler = (function(global) {
if(index !== -1) {
resolveHandlers(fn.merge(handlers[index], handler));
}
+ return index !== -1;
},
registerType: function(obj, handler) {
var index = resolveType(obj, true);
if(index !== -1) {
resolveHandlers(handlers[index] = handler);
}
+ return index !== -1;
},
removeType: function(obj) {
@@ -212,6 +214,7 @@ var Mangler = (function(global) {
constructors.splice(index, 1);
handlers.splice(index, 1);
}
+ return index !== -1;
},
compareType: function(a, b) {
View
@@ -36,45 +36,47 @@
QUnit.module('Utility');
QUnit.test('Mangler.registerType, Mangler.mergeType, Mangler.removeType', function(assert) {
- assert.expect(7);
+ assert.expect(11);
function MyObject() { }
var o = new MyObject();
assert.strictEqual(typeof Mangler.get(o, 'a'), 'undefined', 'not registered, get fails');
- Mangler.registerType(MyObject, {
+ assert.ok(Mangler.registerType(MyObject, {
get: function(obj, k) {
if(k === 'a') return 1;
}
- });
+ }), 'registration ok');
assert.strictEqual(Mangler.get(o, 'a'), 1, 'registered, got value');
var result = '';
Mangler.each(o, function(k, v) { result += v; });
assert.strictEqual(result, '', 'no iterator yet, fail');
- Mangler.mergeType(MyObject, {
+ assert.ok(Mangler.mergeType(MyObject, {
each: function(obj, callback) {
if(callback(0, 'A') === false) return;
if(callback(1, 'B') === false) return;
if(callback(2, 'C') === false) return;
}
- });
+ }), 'merge ok');
result = '';
Mangler.each(o, function(k, v) { result += v; });
assert.strictEqual(result, 'ABC', 'iterator merged, ok');
assert.strictEqual(Mangler.get(o, 'a'), 1, 'get is still registered');
- Mangler.removeType(MyObject);
+ assert.ok(Mangler.removeType(MyObject), 'remove ok');
result = '';
Mangler.each(o, function(k, v) { result += v; });
assert.strictEqual(result, '', 'unregistered');
assert.strictEqual(typeof Mangler.get(o, 'a'), 'undefined', 'unregistered');
+
+ assert.ok(!Mangler.removeType(MyObject), 'already removed');
});
QUnit.test('Mangler.compareType', function(assert) {

0 comments on commit 9ddc37c

Please sign in to comment.