From a175f7c8bd5ebad241724c64217b44a69c008341 Mon Sep 17 00:00:00 2001 From: Denis Demchenko Date: Sat, 7 Jul 2018 18:35:18 +0300 Subject: [PATCH] Rename Parser.compare to Parser.satisfies --- src/parser.js | 8 +-- test.old.js | 146 -------------------------------------------- test/unit/parser.js | 13 +++- 3 files changed, 15 insertions(+), 152 deletions(-) delete mode 100644 test.old.js diff --git a/src/parser.js b/src/parser.js index 7c34f3d..d0b3c6c 100644 --- a/src/parser.js +++ b/src/parser.js @@ -331,17 +331,17 @@ class Parser { * // or with platforms * if (browser.check({desktop: { chrome: '>118.01.1322' } })) */ - compare(checkTree) { + satisfies(checkTree) { const keysToProcess = Object.keys(checkTree); return keysToProcess.some((browserAttribute) => { const objectOrVersion = checkTree[browserAttribute]; if (typeof objectOrVersion === 'object') { return (this.isOs(browserAttribute) || this.isPlatform(browserAttribute)) - && this.compare(objectOrVersion); + && this.satisfies(objectOrVersion); } - return this.isBrowser(browserAttribute) && this.satisfies(objectOrVersion); + return this.isBrowser(browserAttribute) && this.compareVersion(objectOrVersion); }); } @@ -349,7 +349,7 @@ class Parser { return this.getBrowserName(true) === String(browserName).toLowerCase(); } - satisfies(version) { + compareVersion(version) { let expectedResult = 0; let comparableVersion = version; diff --git a/test.old.js b/test.old.js deleted file mode 100644 index 8992638..0000000 --- a/test.old.js +++ /dev/null @@ -1,146 +0,0 @@ -/** - * 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') - , browser = require('../src/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 -} - -function objKeys(obj) { - var keys = [] - , key - for (key in obj) { - if (obj.hasOwnProperty(key)) { - keys.push(key) - } - } - keys.sort(); - return keys.join(', ') -} - -/* Groups */ -for (g in allUserAgents) { (function(group, userAgents) { - describe(group, function() { - - /* User Agents */ - for (ua in userAgents) { (function(userAgent, expections) { - describe('user agent "' + userAgent + '"', function() { - - expections.name = group - - /* Get the result from bowser. */ - var result = browser._detect(userAgent) - - /* At first, check if the result has the correct length. */ - it('should have ' + objLength(expections) + ' properties', function() { - assert.equal(objKeys(result), objKeys(expections)) - }) - - /* 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])} - -var comparisionsTasks = [ - ['9.0', '10', -1], - ['11', '10', 1], - ['1.10.2.1', '1.8.2.1.90', 1], - ['1.010.2.1', '1.08.2.1.90', 1], - ['1.10.2.1', '1.10.2.1', 0], - ['1.10.2.1', '1.0800.2', -1], - ['1.0.0-alpha', '1.0.0-alpha.1', -1], - ['1.0.0-alpha.1', '1.0.0-alpha.beta', -1], - ['1.0.0-alpha.beta', '1.0.0-beta', -1], - ['1.0.0-beta', '1.0.0-beta.2', -1], - ['1.0.0-beta.11', '1.0.0-rc.1', -1], - ['1.0.0-rc.1', '1.0.0', -1] -]; - -describe('Browser versions comparision', function() { - for(g in comparisionsTasks) { - var task = comparisionsTasks[g], - version = task[0], - version2 = task[1], - matching = task[2] === 0 ? ' == ' : (task[2] > 0) ? ' > ' : ' < '; - it('version ' + version + ' should be' + matching + 'version ' + version2, function(){ - assert.equal(browser.compareVersions([version, version2]), task[2]); - }); - } -}); - -describe('Unsupported browser check', function() { - - before(function() { - this.ie10_6 = "Mozilla/5.0 (compatible; MSIE 10.6; Windows NT 6.1; Trident/5.0; InfoPath.2; SLCC1; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 2.0.50727) 3gpp-gba UNTRUSTED/1.0"; - }); - - it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified', function() { - var unsupported = browser.isUnsupportedBrowser({msie: "10"}, this.ie10_6); - assert.equal(unsupported, false); - }); - - it('should be passed by #isUnsupportedBrowser for IE10.6 and for IE10 miminal version specified in strict mode', function() { - var unsupported = browser.isUnsupportedBrowser({msie: "10"}, true, this.ie10_6); - assert.equal(unsupported, false); - }); - - it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified', function() { - var supported = browser.compare({msie: "11"}, this.ie10_6); - assert.equal(supported, false); - }); - - it('should NOT be passed by #check for IE10.6 and for IE11 miminal version specified in strict mode', function() { - var supported = browser.compare({msie: "11"}, true, this.ie10_6); - assert.equal(supported, false); - }); - - it('should throw an error when minVersion map has a number, but not a string', function() { - assert.throws(() => { - browser.compare({msie: 11}, this.ie10_6); - }, /Browser version in the minVersion map should be a string/); - }); - - it('should be passed by #check for IE10.6 when version was not specified', function() { - var supported = browser.compare({}, this.ie10_6); - assert.equal(supported, true); - }); - - it('should NOT be passed by #check for IE10.6 when version was not specified in strict mode', function() { - var supported = browser.compare({}, true, this.ie10_6); - assert.equal(supported, false); - }); - -}) diff --git a/test/unit/parser.js b/test/unit/parser.js index 09004cd..2169eff 100644 --- a/test/unit/parser.js +++ b/test/unit/parser.js @@ -58,11 +58,11 @@ test('Skip parsing shouldn\'t parse', (t) => { }); test('Parser.check should make simple comparison', (t) => { - t.is(parser.compare({ opera: '>42' }), true); + t.is(parser.satisfies({ opera: '>42' }), true); }); test('Parser.check should make complex comparison', (t) => { - t.is(parser.compare({ + t.is(parser.satisfies({ macos: { safari: '>11', }, @@ -73,6 +73,15 @@ test('Parser.check should make complex comparison', (t) => { }), true); }); +test('Parser.check should respect platform and OS specific declarations', (t) => { + t.is(parser.satisfies({ + macos: { + safari: '>45', + }, + opera: '>42', + }), false); +}); + test('Parser.is should pass', (t) => { t.is(parser.is('opera'), true); t.is(parser.is('desktop'), true);