diff --git a/Makefile b/Makefile index 09b69fe..d5cd37f 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,16 @@ boosh: - node make/build.js \ No newline at end of file + node make/build.js + +REPORTER = dot + +test: + @NODE_ENV=test ./node_modules/.bin/mocha \ + --reporter $(REPORTER) \ + +test-w: + @NODE_ENV=test ./node_modules/.bin/mocha \ + --reporter $(REPORTER) \ + --growl \ + --watch + +.PHONY: test test-w diff --git a/src/bowser.js b/src/bowser.js index fb8bd7f..8c211fd 100644 --- a/src/bowser.js +++ b/src/bowser.js @@ -71,7 +71,7 @@ o = { webkit: t , phantom: t - , version: versionTest(/phantomjs\/(\d+(\.\d+)+)/i) + , version: versionTest(/phantomjs\/(\d+(\.\d+)?)/i) }; } else if (uaTest(/touchpad/i)) { o = { @@ -84,11 +84,14 @@ webkit: t , mobile: t , ios: t - , iphone: iphone - , ipad: ipad // WTF: version is not part of user agent in web apps , version: versionTest(webkitVersion) }; + if (iphone) { + o.iphone = t; + } else if (ipad) { + o.ipad = t; + } } else if (uaTest(/android/i)) { o = { webkit: t diff --git a/src/useragents.js b/src/useragents.js new file mode 100644 index 0000000..574cc16 --- /dev/null +++ b/src/useragents.js @@ -0,0 +1,93 @@ +/** + * Example User Agents and thair expected bowser objects. + * + * @see test/test.js + * @author hannes.diercks@jimdo.com + */ + +module.exports.useragents = { + Chrome: { + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.57 Safari/534.24': { + chrome: true, + version: '11.0', + webkit: true, + a: true + } + }, + Opera: { + 'Opera/9.80 (Macintosh; Intel Mac OS X 10.6.7; U; en) Presto/2.7.62 Version/11.01': { + opera: true, + version: '11.01', + a: true + } + }, + Safari: { + 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1': { + safari: true, + version: '5.0', + webkit: true, + a: true + } + }, + 'Internet Explorer': { + 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)': { + msie: true, + version: '8.0', + a: true + } + }, + Firefox: { + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0': { + mozilla: true, + gecko: true, + firefox: true, + version: '4.0', + a: true + } + }, + iPhone: { + 'Mozilla/5.0 (iPhone Simulator; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5': { + ios: true, + version: '5.0', + iphone: true, + mobile: true, + webkit: true, + x: true + } + }, + iPad: { + 'Mozilla/5.0 (iPad; U; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5': { + ios: true, + version: '5.0', + ipad: true, + mobile: true, + webkit: true, + x: true + } + }, + Android: { + 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; T-Mobile G2 Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1': { + android: true, + webkit: true, + version: 4.0, + mobile: true, + x: true + } + }, + Touchpad: { + 'Mozilla/5.0 (hp-tabled;Linux;hpwOS/3.0.5; U; en-US)) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/234.83 Safari/534.6 TouchPad/1.0': { + touchpad: true, + version: '1.0', + webkit: true, + x: true + } + }, + PhantomJS: { + 'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.5.0 Safari/534.34': { + phantom: true, + webkit: true, + version: '1.5', + x: true + } + } +}; diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..963dfa8 --- /dev/null +++ b/test/test.js @@ -0,0 +1,62 @@ +/** + * Loop through all entries in our user agents object and test everything. + * + * @see src/useragents.js + * @author hannes.diercks@jimdo.com + */ + +var g, ua, p, + assert = require('assert'), + bowser = require('../src/bowser').bowser, + allUserAgents = require('../src/useragents').useragents; + + +/** + * Get the length of an object. + * http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array + * + * @param {Object} obj + * @return {Number} + */ +function objLength(obj) { + var size = 0, key; + for (key in obj) { + if (obj.hasOwnProperty(key)) { + size++; + } + } + return size; +} + +/* Groups */ +for (g in allUserAgents) { (function(group, userAgents) { + describe(group, function() { + + /* User Agents */ + for (ua in userAgents) { (function(userAgent, expections) { + describe('#' + userAgent, function() { + + /* Get the result from bowser. */ + var result = bowser._detect(userAgent), + expectionLength = objLength(expections); + + /* At first, check if the result has the correct length. */ + it('Should have ' + expectionLength + ' properties', function() { + assert.equal(objLength(result), expectionLength); + }); + + /* Properties */ + for (p in expections) { (function(property, value, resultValue) { + + /* Now ensure correctness of every property. */ + it('\'s Property "' + property + '" should be ' + value, function() { + assert.equal(resultValue, value); + }); + + })(p, expections[p], result[p]); } + + }); + })(ua, userAgents[ua]); } + + }); +})(g, allUserAgents[g]); }