Commits

Raphaël BECK  committed 0ac312a

All initial API written + tests

  • Participants
  • Parent commits e2dfdf4

Comments (0)

Files changed (2)

File js/adaptive.js

     if(length < 1){
         throw new Error("key argument is mandatory");
     }
+    else if(String(key).match(containsDotRE)){
+        throw new Error("dot character is not allowed in keys");
+    }
     else if(length === 1){//Getter
         return this._adaptations[key];
     }
          * @return {Array} Names of registered modules
          */
         this.getModules = function(){
+            //Object.keys does not return the right type but a string representation for each key
             return Object.keys(_modules);
         };
 
             return detected;
         };
 
-        this.registerAdaptationForModule = function(){
+        /**
+         * Launch adaptations for provided keys
+         * @param keys A string composed of the name of a registered module and the name of an adaptation within this
+         *             module separated by a dot
+         */
+        this.adapt = function(keys){
+            if(typeof(keys) === "string"){
+                keys = [keys];
+            }
+
+            for(var k in keys){
+                try{
+                    var moduleAdaptionName = keys[k].split(".");
+                    var module = moduleAdaptionName[0];
+                    var adaptationName = moduleAdaptionName[1];
+                    _modules[module].adaptation(adaptationName)();
+                } catch(err){
+                    //Fail silently
+                }
+            }
+        };
 
+        /**
+         * Register an extra adaptation method for a particular module.
+         * The module has to be already registered
+         * The callback has to be callable & return a boolean which indicates whether or not the detection has succeeded
+         * @param moduleName Name of a previously registered module
+         * @param adaptationName Name of the adaptation. NB: If the adaptation key already exists it will be overwritten
+         * @param adaptationCallback Adaptation callable
+         */
+        this.registerAdaptationForModule = function(moduleName, adaptationName, adaptationCallback){
+            if(arguments.length < 3){
+                throw new Error("All arguments are mandatory");
+            }
+            if(!this.hasModule(moduleName)){
+                throw new Error("Module not registered");
+            }
+            if(typeof(adaptationCallback) !== "function"){
+                throw new Error("detectionCallback has to be callable");
+            }
+            _modules[moduleName].adaptation(adaptationName, adaptationCallback);
         };
 
     };

File tests/run.js

         //Get a detection callback
         ok(m.adaptation(adaptKey) instanceof Function, "Check if it's a function");
         ok(m.adaptation(adaptKey)(), "Check if it's the right function");
+
+        raises(function(){m.adaptation("a.b", function(){return true;});}, Error, 'Dot is not accepted');
+        raises(function(){m.adaptation("a..b", function(){return true;});}, Error, 'Dot is not accepted');
+        raises(function(){m.adaptation(".b", function(){return true;});}, Error, 'Dot is not accepted');
+        raises(function(){m.adaptation("b.", function(){return true;});}, Error, 'Dot is not accepted');
+        raises(function(){m.adaptation("b...........", function(){return true;});}, Error, 'Dot is not accepted');
     });
 
     test('Methods: getDetections', function(){
         raises(function(){Adaptive.registerDetectionForModule(u._name, key1, undefined);}, Error,  'module registered, callback has to be callable');
         raises(function(){Adaptive.registerDetectionForModule(u._name, key1, 1);}, Error,  'module registered, callback has to be callable');
 
-
         raises(function(){Adaptive.registerDetectionForModule(u._name, function(){});}, Error,  'All arguments are mandatory');
 
         Adaptive.registerDetectionForModule(u._name, key2, function(){return true;});
         var result = [u._name+"."+key2, u._name+"."+key4];
         deepEqual(Adaptive.detect(), result);
 
-        //TODO:Prevent for inserting a key with a dot in detection / adaptation of all modules
         raises(function(){Adaptive.registerDetectionForModule(u._name, "a.", function(){return true;});}, Error,  'detectionName should not have a dot');
         raises(function(){Adaptive.registerDetectionForModule(u._name, "a.b", function(){return true;});}, Error,  'detectionName should not have a dot');
         raises(function(){Adaptive.registerDetectionForModule(u._name, "a..b", function(){return true;});}, Error,  'detectionName should not have a dot');
         ok(true, 'Time of '+moduleTimes+' executions of detect with '+moduleTimes*3+' modules: '+time+'ms (previous was '+previous+' ms / average: '+average+' ms)');
     });
 
+    test('Adaptive: adapt', function(){
+        ok(Adaptive.adapt instanceof Function, 'adapt is a method');
+    });
 
+    module("Tests 'Adaptive' adaptations", {
+            setup: function(){
+                var d = document.createElement('div');
+                d.setAttribute("id", "test-module-adapt3");
+                var d2 = document.createElement('div');
+                d2.setAttribute("id", "test-module-adapt4");
+                d.appendChild(d2);
+                document.body.appendChild(d);
+                this.div3 = d;
+                this.div4 = d2;
+                equal(this.div3.firstChild, this.div4, 'Div4 is a child of Div');
+            },
+            teardown: function(){
+                this.div4.parentNode.removeChild(this.div4);
+                this.div3.parentNode.removeChild(this.div3);
+                delete this.div3;
+                delete this.div4;
+            }}
+    );
+    test('Adaptive: registerAdaptationForModule', function(){
+        expect(21);
+        ok(Adaptive.registerAdaptationForModule instanceof Function, 'registerDetectionForModule is a method');
+        var u = new User();
+        var key1 = "1", key2 = "2", key3= "3", key4 = "4";
+        var _this = this;
+        Adaptive.registerModule(u);
+
+        raises(function(){Adaptive.registerAdaptationForModule(null, null, null);}, Error,  'module not registered');
+        raises(function(){Adaptive.registerAdaptationForModule(undefined, undefined, undefined);}, Error,  'module not registered');
+        raises(function(){Adaptive.registerAdaptationForModule("", "", "");}, Error,  'module not registered');
+
+        Adaptive.registerAdaptationForModule(u._name, key1, function(){});
+
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, key1, null);}, Error,  'module registered, callback has to be callable');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, key1, "");}, Error,  'module registered, callback has to be callable');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, key1, undefined);}, Error,  'module registered, callback has to be callable');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, key1, 1);}, Error,  'module registered, callback has to be callable');
+
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, function(){});}, Error,  'All arguments are mandatory');
+
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, "a.", function(){return true;});}, Error,  'adaptationName should not have a dot');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, "a.b", function(){return true;});}, Error,  'adaptationName should not have a dot');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, "a..b", function(){return true;});}, Error,  'adaptationName should not have a dot');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, "a.....b", function(){return true;});}, Error,  'adaptationName should not have a dot');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, ".....", function(){return true;});}, Error,  'adaptationName should not have a dot');
+        raises(function(){Adaptive.registerAdaptationForModule(u._name, ".....b", function(){return true;});}, Error,  'adaptationName should not have a dot');
+
+        var cb1 = function(){
+            _this.div3.style.backgroundColor = "rgb(0, 0, 0)";
+        };
+
+        var cb2 = function(){
+            _this.div3.style.borderColor = "#000000";
+            _this.div3.setAttribute("data-test", 12345);
+        };
+
+        var cb3 = function(){
+            _this.div4 = _this.div3.removeChild(_this.div4);
+            document.body.appendChild(_this.div4);
+        };
+
+        Adaptive.registerAdaptationForModule(u._name, key2, cb1);
+        Adaptive.registerAdaptationForModule(u._name, key3, cb2);
+        Adaptive.registerAdaptationForModule(u._name, key4, cb3);
+
+        Adaptive.adapt(u._name+'.'+key2);
+        equal(_this.div3.style.backgroundColor, "rgb(0, 0, 0)", 'Adaptation 2 well applied');
+
+        Adaptive.adapt([u._name+'.'+key3, u._name+'.'+key4]);
+        equal(_this.div3.style.borderColor, "rgb(0, 0, 0)", 'Adaptation 3.1 well applied');
+        equal(_this.div3.getAttribute("data-test"), 12345, 'Adaptation 3.2 well applied');
+
+        notEqual(_this.div3.firstChild, _this.div4, 'Adaptation 4.1 well applied');
+        equal(_this.div4.parentNode, document.body, 'Adaptation 4.2 well applied');
+
+        Adaptive.adapt('moduleNotRegistered'+'.'+key2);//Try with a module not registered
+        Adaptive.adapt(u._name+'.'+'adaptationNotRegistered');//Try with an adaptation not registered
+        Adaptive.adapt('weird string');
+        Adaptive.adapt(undefined);
+        Adaptive.adapt(null);
+
+    });
 
 
 });