You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2012/11/22 21:47:47 UTC

[5/9] js commit: [all] Use argscheck in compass.js.

[all] Use argscheck in compass.js.

https://issues.apache.org/jira/browse/CB-1892


Project: http://git-wip-us.apache.org/repos/asf/cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-js/commit/f400233b
Tree: http://git-wip-us.apache.org/repos/asf/cordova-js/tree/f400233b
Diff: http://git-wip-us.apache.org/repos/asf/cordova-js/diff/f400233b

Branch: refs/heads/master
Commit: f400233bef00da6d3678d95e4abf745a1f62e07f
Parents: 9ba65d4
Author: Andrew Grieve <ag...@chromium.org>
Authored: Thu Nov 22 15:36:52 2012 -0500
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Thu Nov 22 15:45:06 2012 -0500

----------------------------------------------------------------------
 lib/common/plugin/compass.js |   34 ++++++-----------------------
 test/test.compass.js         |   42 ++++++++++--------------------------
 2 files changed, 19 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f400233b/lib/common/plugin/compass.js
----------------------------------------------------------------------
diff --git a/lib/common/plugin/compass.js b/lib/common/plugin/compass.js
index c97c158..896c706 100644
--- a/lib/common/plugin/compass.js
+++ b/lib/common/plugin/compass.js
@@ -19,7 +19,8 @@
  *
 */
 
-var exec = require('cordova/exec'),
+var argscheck = require('cordova/argscheck'),
+    exec = require('cordova/exec'),
     utils = require('cordova/utils'),
     CompassHeading = require('cordova/plugin/CompassHeading'),
     CompassError = require('cordova/plugin/CompassError'),
@@ -34,23 +35,13 @@ var exec = require('cordova/exec'),
          * @param {CompassOptions} options The options for getting the heading data (not used).
          */
         getCurrentHeading:function(successCallback, errorCallback, options) {
-            // successCallback required
-            if (typeof successCallback !== "function") {
-              console.log("Compass Error: successCallback is not a function");
-              return;
-            }
-
-            // errorCallback optional
-            if (errorCallback && (typeof errorCallback !== "function")) {
-              console.log("Compass Error: errorCallback is not a function");
-              return;
-            }
+            argscheck.checkArgs('fFO', 'compass.getCurrentHeading', arguments);
 
             var win = function(result) {
                 var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp);
                 successCallback(ch);
             };
-            var fail = function(code) {
+            var fail = errorCallback && function(code) {
                 var ce = new CompassError(code);
                 errorCallback(ce);
             };
@@ -70,22 +61,11 @@ var exec = require('cordova/exec'),
          * specifies to watch via a distance filter rather than time.
          */
         watchHeading:function(successCallback, errorCallback, options) {
+            argscheck.checkArgs('fFO', 'compass.watchHeading', arguments);
             // Default interval (100 msec)
             var frequency = (options !== undefined && options.frequency !== undefined) ? options.frequency : 100;
             var filter = (options !== undefined && options.filter !== undefined) ? options.filter : 0;
 
-            // successCallback required
-            if (typeof successCallback !== "function") {
-              console.log("Compass Error: successCallback is not a function");
-              return;
-            }
-
-            // errorCallback optional
-            if (errorCallback && (typeof errorCallback !== "function")) {
-              console.log("Compass Error: errorCallback is not a function");
-              return;
-            }
-
             var id = utils.createUUID();
             if (filter > 0) {
                 // is an iOS request for watch by filter, no timer needed
@@ -109,8 +89,8 @@ var exec = require('cordova/exec'),
             // Stop javascript timer & remove from timer list
             if (id && timers[id]) {
                 if (timers[id] != "iOS") {
-                      clearInterval(timers[id]);
-                  } else {
+                    clearInterval(timers[id]);
+                } else {
                     // is iOS watch by filter so call into device to stop
                     exec(null, null, "Compass", "stopHeading", []);
                 }

http://git-wip-us.apache.org/repos/asf/cordova-js/blob/f400233b/test/test.compass.js
----------------------------------------------------------------------
diff --git a/test/test.compass.js b/test/test.compass.js
index 30a7ff9..2d9bff3 100644
--- a/test/test.compass.js
+++ b/test/test.compass.js
@@ -29,26 +29,17 @@ describe("compass", function () {
     });
 
     describe("when getting the current heading", function () {
-        it("logs an error and doesn't call exec when no success callback given", function () {
-            spyOn(console, "log");
-            compass.getCurrentHeading();
-            expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
-            expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+        it("throws an error and doesn't call exec when no success callback given", function () {
+            expect(function() { compass.getCurrentHeading() }).toThrow();
         });
 
-        it("logs an error and doesn't call exec when success isn't a function", function () {
-            spyOn(console, "log");
-            compass.getCurrentHeading(12);
-            expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
-            expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+        it("throws an error and doesn't call exec when success isn't a function", function () {
+            expect(function() { compass.getCurrentHeading(12) }).toThrow();
         });
 
-        it("logs an error and doesn't call exec when error isn't a function", function () {
+        it("throws an error and doesn't call exec when error isn't a function", function () {
             var func = function () {};
-            spyOn(console, "log");
-            compass.getCurrentHeading(func, 12);
-            expect(console.log).toHaveBeenCalledWith("Compass Error: errorCallback is not a function");
-            expect(exec).not.toHaveBeenCalledWith(func, 12, "Compass", "getHeading", []);
+            expect(function() { compass.getCurrentHeading(func, 12) }).toThrow();
         });
 
         it("calls exec", function () {
@@ -65,26 +56,17 @@ describe("compass", function () {
             spyOn(window, "setInterval").andReturn("def");
         });
 
-        it("logs an error and doesn't call exec when no success callback given", function () {
-            spyOn(console, "log");
-            compass.watchHeading();
-            expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
-            expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+        it("throws an error and doesn't call exec when no success callback given", function () {
+            expect(function() { compass.watchHeading() }).toThrow();
         });
 
-        it("logs an error and doesn't call exec when success isn't a function", function () {
-            spyOn(console, "log");
-            compass.watchHeading(12);
-            expect(console.log).toHaveBeenCalledWith("Compass Error: successCallback is not a function");
-            expect(exec).not.toHaveBeenCalledWith(jasmine.any(Function), undefined, "Compass", "getHeading", []);
+        it("throws an error and doesn't call exec when success isn't a function", function () {
+            expect(function() { compass.watchHeading(12) }).toThrow();
         });
 
-        it("logs an error and doesn't call exec when error isn't a function", function () {
+        it("throws an error and doesn't call exec when error isn't a function", function () {
             var func = function () {};
-            spyOn(console, "log");
-            compass.watchHeading(func, 12);
-            expect(console.log).toHaveBeenCalledWith("Compass Error: errorCallback is not a function");
-            expect(exec).not.toHaveBeenCalledWith(func, 12, "Compass", "getHeading", []);
+            expect(function() { compass.watchHeading(func, 12) }).toThrow();
         });
 
         it("generates and returns a uuid for the watch", function () {