Skip to content

Commit

Permalink
Working on the test suite, only failing 45 out of the 11329 test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Sep 28, 2011
1 parent bb46eb5 commit 898c06d
Show file tree
Hide file tree
Showing 9 changed files with 64,199 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ALL_TESTS = $(shell find tests/ -name '*.test.js')
ALL_QA = $(shell find tests/ -name '*.qa.js')

run-tests:
@./node_modules/.bin/expresso \
Expand All @@ -11,6 +12,9 @@ run-tests:
test:
@$(MAKE) TESTS="$(ALL_TESTS)" run-tests

qa:
@$(MAKE) TESTS="$(ALL_QA)" run-tests

test-cov:
@TESTFLAGS=--cov $(MAKE) test

Expand Down
24 changes: 24 additions & 0 deletions bin/testfiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

var request = require('request')
, path = require('path')
, fs = require('fs');

var files = {
'pgts.yaml': 'http://ua-parser.googlecode.com/svn/trunk/test/resources/pgts_browser_list.yaml'
, 'testcases.yaml': 'http://ua-parser.googlecode.com/svn/trunk/test/resources/test_user_agent_parser.yaml'
, 'firefoxes.yaml': 'http://ua-parser.googlecode.com/svn/trunk/test/resources/firefox_user_agent_strings.yaml'
};

/**
* Update the fixtures
*/

Object.keys(files).forEach(function (key) {
request(files[key], function response (err, res, data) {
if (err || res.statusCode !== 200) return console.error('failed to update');

console.log('downloaded', files[key]);
fs.writeFileSync(path.join(__dirname, '..', 'tests', 'fixtures', key), data);
});
});
5 changes: 4 additions & 1 deletion lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ module.exports = function update (fn) {
});
};

// defaults
/**
* Constants
*/

exports.version = '0.0.1';
exports.remote = 'http://ua-parser.googlecode.com/svn/trunk/resources/user_agent_parser.yaml';
exports.output = path.join(__dirname, '..', 'lib', 'agents.js');
89 changes: 69 additions & 20 deletions lib/useragent.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/**
* Library dependencies
* Library dependencies.
*/

var semver = require('semver')
, update = require('./update')
, agents = require('./agents');

/**
* Quick lookup references
* Quick lookup references.
*/

var ua_length = agents.browser.length
, ua_length = agents.os.length
, os_length = agents.os.length
, browsers = agents.browser
, oss = agents.os;

/**
* The representation of a parsed user agent
* The representation of a parsed user agent.
*
* @constructor
* @param {String} family The name of the browser
Expand All @@ -28,28 +28,42 @@ var ua_length = agents.browser.length
*/

function Agent (family, major, minor, patch, os) {
this.family = family || 'Unknown';
this.major = major || 0;
this.minor = minor || 0;
this.patch = patch || 0;
this.os = os || 'Unknown';
this.family = family || 'Other';
this.major = major || '0';
this.minor = minor || '0';
this.patch = patch || '0';
this.os = os || 'Other';
}

/**
* Generates a string output of the parsed user agent
* Generates a string output of the parsed user agent.
*
* @returns {String}
* @api public
*/

Agent.prototype.toString = function toString () {
Agent.prototype.toAgent = function toAgent () {
var output = this.family
, version = this.toVersion();

if (version) output += ' ' + version;
return output;
};

/**
* Generates a string output of the parser user agent and operating system.
*
* @returns {String} "UserAgent 0.0.0 / OS"
* @api public
*/

Agent.prototype.toString = function toString () {
var agent = this.toAgent()
, os = this.os !== 'Other' ? this.os : false;

return agent + (os ? ' / ' + os : '');
};

/**
* Outputs a compiled veersion number of the user agent.
*
Expand All @@ -75,6 +89,23 @@ Agent.prototype.toVersion = function toVersion () {
return version;
};

/**
* Outputs a JSON string of the Agent
*
* @returns {String}
* @api public
*/

Agent.prototype.toJSON = function toJSON () {
return JSON.stringify({
family: this.family
, major: this.major
, minor: this.minor
, patch: this.patch
, os: this.os
});
};

/**
* Checks if the user agent's version can be satisfied agents the give
* ranged argument. This uses the semver libraries range construction.
Expand All @@ -98,7 +129,7 @@ Agent.prototype.satisfies = function satisfies (range) {
* @api public
*/

function parser (useragent, jsagent) {
function parse (useragent, jsagent) {
var ua, os, i;

// find the user agent
Expand All @@ -111,21 +142,39 @@ function parser (useragent, jsagent) {

// find the os
for (i = 0; i < os_length; i++) {
if (os = useragent.match(oss[i])) {
if (os = useragent.match(oss[i].r)) {
os.parser = oss[i];
break;
}
}

// default to nothing
ua = ua || [];
os = os || [];

// check to see if we need to some replacements
var family = ua.parser.family ? ua.parser.family.replace('$1', ua[1]) : ua[1]
, major = ua.parser.major || ua[2]
var parser = ua.parser || {}
, family = parser && parser.family ? parser.family.replace('$1', ua[1]) : ua[1]
, major = parser.major || ua[2]
, minor = ua[3]
, patch = ua[4]
, osys = os.parser.os || os[1];

if (jsagent && jsagent.indexOf('chromeframe') !== -1)
family = 'Chrome Frame(' + family + ' ' + major + ')';
, osys = os.parser && os.parser.os ? os.parser.os : os[1];

// detect chrome frame, but make sure it's enabled! So we need to check for
// the Chrome/ so we know that it's actually using Chrome under the hood
if (jsagent && (
jsagent.indexOf('Chrome/') !== -1
&& jsagent.indexOf('chromeframe') !== -1
)
) {
family = 'Chrome Frame (' + family + ' ' + major + ')';
parser = parse(jsagent);

// update to the new correct version numbers of chrome frame
major = parser.major;
minor = parser.minor;
patch = parser.patch;
}

return new Agent(family, major, minor, patch, osys);
};
Expand Down Expand Up @@ -179,7 +228,7 @@ updater.Agent = Agent;
* @api public
*/

updater.parser = parser;
updater.parse = parse;

/**
* Expose the updater.
Expand Down
Loading

0 comments on commit 898c06d

Please sign in to comment.