mirror of
https://github.com/lancedikson/bowser
synced 2026-03-02 03:40:27 +00:00
Updates
This commit is contained in:
21
test/acceptance/test-list-of-ua.js
Normal file
21
test/acceptance/test-list-of-ua.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import test from 'ava';
|
||||
import yaml from 'yamljs';
|
||||
import path from 'path';
|
||||
import Bowser from '../../src/bowser';
|
||||
import BowserBuild from '../../es5';
|
||||
|
||||
const listOfUA = yaml.load(path.join(__dirname, 'useragentstrings.yml'));
|
||||
|
||||
const browserNames = Object.keys(listOfUA);
|
||||
|
||||
browserNames.forEach((browserName) => {
|
||||
listOfUA[browserName].forEach((browser, index) => {
|
||||
test(`Test ${browserName} ${index}`, (t) => {
|
||||
const parsed = Bowser.parse(browser.ua);
|
||||
const parsedBuild = BowserBuild.parse(browser.ua);
|
||||
t.deepEqual(parsed, browser.spec, `${browser.ua}`);
|
||||
t.deepEqual(parsedBuild, browser.spec, `${browser.ua}`);
|
||||
t.is(parsed.browser.name, browserName, `${browser.ua}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
2611
test/acceptance/useragentstrings.yml
Normal file
2611
test/acceptance/useragentstrings.yml
Normal file
File diff suppressed because it is too large
Load Diff
18
test/unit/bowser.js
Normal file
18
test/unit/bowser.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import test from 'ava';
|
||||
import Bowser from '../../src/bowser';
|
||||
import Parser from '../../src/parser';
|
||||
|
||||
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1165';
|
||||
const browser = Bowser.getParser(UA);
|
||||
|
||||
test('Bowser`s constructor returns a Parser instance', (t) => {
|
||||
t.truthy(browser instanceof Parser);
|
||||
});
|
||||
|
||||
test('Bowser`s constructor fails if UA is empty', (t) => {
|
||||
t.throws(() => (Bowser.getParser()));
|
||||
});
|
||||
|
||||
test('Bowser.parse parses UA and returns result', (t) => {
|
||||
t.deepEqual(Bowser.parse(UA), browser.getResult());
|
||||
});
|
||||
160
test/unit/parser.js
Normal file
160
test/unit/parser.js
Normal file
@@ -0,0 +1,160 @@
|
||||
import test from 'ava';
|
||||
import sinon from 'sinon';
|
||||
import Parser from '../../src/parser';
|
||||
|
||||
const UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.1165';
|
||||
const parser = new Parser(UA, true);
|
||||
|
||||
test('constructor', (t) => {
|
||||
t.truthy(parser instanceof Parser);
|
||||
});
|
||||
|
||||
test('Parser.getUA returns a correct UA', (t) => {
|
||||
t.is(parser.getUA(), UA);
|
||||
});
|
||||
|
||||
test('Parser.test', (t) => {
|
||||
t.truthy(parser.test(/Chrome/i));
|
||||
});
|
||||
|
||||
test('Parser.parseBrowser is being called when the Parser.getBrowser() is called', (t) => {
|
||||
const spy = sinon.spy(parser, 'parseBrowser');
|
||||
const b = parser.getBrowser();
|
||||
t.truthy(spy.called);
|
||||
t.is(b.name, 'Opera');
|
||||
t.is(b.version, '43.0.2442.1165');
|
||||
parser.parseBrowser.restore();
|
||||
});
|
||||
|
||||
test('Parser.getBrowserName returns a correct result', (t) => {
|
||||
t.is(parser.getBrowserName(), 'Opera');
|
||||
});
|
||||
|
||||
test('Parser.getBrowserVersion returns a correct result', (t) => {
|
||||
t.is(parser.getBrowserVersion(), '43.0.2442.1165');
|
||||
});
|
||||
|
||||
test('Parser.parseOS is being called when getOS() called', (t) => {
|
||||
const spy = sinon.spy(parser, 'parseOS');
|
||||
parser.getOS();
|
||||
t.truthy(spy.called);
|
||||
parser.parseOS.restore();
|
||||
});
|
||||
|
||||
test('Parser.getOSName gives a name of the browser', (t) => {
|
||||
t.is(parser.getOSName(), 'macOS');
|
||||
});
|
||||
|
||||
test('Parser.getOSName gives a lower-cased name of the browser', (t) => {
|
||||
t.is(parser.getOSName(true), 'macos');
|
||||
});
|
||||
|
||||
test('Parser.getOSVersion returns a correct result', (t) => {
|
||||
t.is(parser.getOSVersion(), '10.12.4');
|
||||
});
|
||||
|
||||
test('Parser.parseEngine is being called when getEngine() called', (t) => {
|
||||
const spy = sinon.spy(parser, 'parseEngine');
|
||||
parser.getEngine();
|
||||
t.truthy(spy.called);
|
||||
parser.parseEngine.restore();
|
||||
});
|
||||
|
||||
test('Parser.getEngineName gives a name of the engine', (t) => {
|
||||
t.is(parser.getEngineName(), 'Blink');
|
||||
});
|
||||
|
||||
test('Parser.getEngineName gives a lower-cased name of the engine', (t) => {
|
||||
t.is(parser.getEngineName(true), 'blink');
|
||||
});
|
||||
|
||||
test('Skip parsing shouldn\'t parse', (t) => {
|
||||
t.deepEqual((new Parser(UA, true)).getResult(), {});
|
||||
});
|
||||
|
||||
test('Parser.satisfies should make simple comparisons', (t) => {
|
||||
// also covers Parser.compareVersion() method
|
||||
t.is(parser.satisfies({ opera: '>42' }), true);
|
||||
t.is(parser.satisfies({ opera: '<44' }), true);
|
||||
t.is(parser.satisfies({ opera: '=43.0.2442.1165' }), true);
|
||||
t.is(parser.satisfies({ opera: '~43.0' }), true);
|
||||
t.is(parser.satisfies({ opera: '>=43' }), true);
|
||||
t.is(parser.satisfies({ opera: '<=43' }), true);
|
||||
t.is(parser.satisfies({ opera: '>=43.0' }), true);
|
||||
t.is(parser.satisfies({ opera: '>=43.0.2442.1165' }), true);
|
||||
t.is(parser.satisfies({ opera: '<=43.0.2442.1165' }), true);
|
||||
t.is(parser.satisfies({ opera: '>=43.0.2443' }), false);
|
||||
t.is(parser.satisfies({ opera: '<=43.0.2443' }), true);
|
||||
t.is(parser.satisfies({ opera: '>=43.0.2441' }), true);
|
||||
t.is(parser.satisfies({ opera: '~43' }), true);
|
||||
});
|
||||
|
||||
test('Parser.satisfies should make complex comparison', (t) => {
|
||||
t.is(parser.satisfies({
|
||||
macos: {
|
||||
safari: '>11',
|
||||
},
|
||||
ios: {
|
||||
safari: '>10',
|
||||
},
|
||||
opera: '>42',
|
||||
}), true);
|
||||
});
|
||||
|
||||
test('Parser.satisfies should respect platform and OS specific declarations', (t) => {
|
||||
t.is(parser.satisfies({
|
||||
macos: {
|
||||
opera: '>45',
|
||||
},
|
||||
opera: '>42',
|
||||
}), false);
|
||||
|
||||
t.is(parser.satisfies({
|
||||
desktop: {
|
||||
opera: '>45',
|
||||
},
|
||||
opera: '>42',
|
||||
}), false);
|
||||
|
||||
t.is(parser.satisfies({
|
||||
macos: {
|
||||
opera: '>45',
|
||||
},
|
||||
desktop: {
|
||||
opera: '>42',
|
||||
},
|
||||
opera: '>42',
|
||||
}), false);
|
||||
|
||||
t.is(parser.satisfies({
|
||||
macos: {
|
||||
chrome: '>45',
|
||||
},
|
||||
desktop: {
|
||||
chrome: '>42',
|
||||
},
|
||||
firefox: '>42',
|
||||
}), void 0);
|
||||
});
|
||||
|
||||
test('Parser.satisfies for versionless UA strings', (t) => {
|
||||
const _parser = new Parser('Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15G77 [FBAN/FBIOS;FBDV/iPhone7,2;FBMD/iPhone;FBSN/iOS;FBSV/11.4.1;FBSS/2;FBCR/vfnl;FBID/phone;FBLC/nl_NL;FBOP/5;FBRV/0]');
|
||||
|
||||
t.is(_parser.satisfies({
|
||||
safari: '>9',
|
||||
}), void 0);
|
||||
});
|
||||
|
||||
test('Parser.is should pass', (t) => {
|
||||
t.is(parser.is('opera'), true);
|
||||
t.is(parser.is('desktop'), true);
|
||||
t.is(parser.is('macos'), true);
|
||||
});
|
||||
|
||||
test('Parser.some should pass', (t) => {
|
||||
t.is(parser.some(['opera', 'chrome', 'firefox']), true);
|
||||
t.is(parser.some(['macos', 'windows']), true);
|
||||
t.is(parser.some(['chrome', 'firefox']), false);
|
||||
t.is(parser.some([]), false);
|
||||
t.is(parser.some(), false);
|
||||
});
|
||||
52
test/unit/utils.js
Normal file
52
test/unit/utils.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import test from 'ava';
|
||||
import {
|
||||
getFirstMatch,
|
||||
getWindowsVersionName,
|
||||
compareVersions,
|
||||
} from '../../src/utils';
|
||||
|
||||
test('getFirstMatch', (t) => {
|
||||
const matchedVersion = getFirstMatch(/version\/(\S+)/i, 'Chrome Version/11.11.11');
|
||||
t.is(matchedVersion, '11.11.11');
|
||||
});
|
||||
|
||||
test('getWindowsVersionName', (t) => {
|
||||
t.is(getWindowsVersionName('NT 5.0'), '2000');
|
||||
t.is(getWindowsVersionName('XXX'), void 0);
|
||||
});
|
||||
|
||||
test('compareVersions', (t) => {
|
||||
const 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.10.2', 0, true],
|
||||
['1.10.2.1', '1.10', 0, true],
|
||||
['1.10.2.1', '1', 0, true],
|
||||
['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],
|
||||
];
|
||||
|
||||
comparisionsTasks.forEach((testingParams) => {
|
||||
const versionA = testingParams[0];
|
||||
const versionB = testingParams[1];
|
||||
const result = testingParams[2];
|
||||
const isLoose = testingParams.length > 3 ? testingParams[3] : false;
|
||||
let matching = isLoose ? '~' : ' == ';
|
||||
|
||||
if (result > 0) {
|
||||
matching = ' > ';
|
||||
} else if (result < 0) {
|
||||
matching = ' < ';
|
||||
}
|
||||
|
||||
t.is(compareVersions(versionA, versionB, isLoose), result, `version ${versionA} should be ${matching} version ${versionB}`);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user