diff --git a/tests/coarse-user-agent-parser-test.js b/tests/coarse-user-agent-parser-test.js old mode 100644 new mode 100755 index ec9974af357a2e33e09c96af8de80b2425298cfb..613a472f4589093d4034434b092b15c24016c109 --- a/tests/coarse-user-agent-parser-test.js +++ b/tests/coarse-user-agent-parser-test.js @@ -8,6 +8,7 @@ const fs = require('fs'), // TODO: convert to vows based test (or introduce nodeunit dependency) vows = require('vows'), coarse = require('../lib/coarse_user_agent_parser'), + assert = require('assert'), path = require('path'); var suite = vows.describe('coarse-user-agent-parser'); @@ -17,26 +18,33 @@ suite.options.error = false; /* Update test data with https://gist.github.com/2590547 */ -fs.readFile(path.join(__dirname, 'data/user_agents.json'), 'utf-8', function (err, data) { - if (err) { - console.error(err); - } else { - var test_data = JSON.parse(data); - for (var i=0; i < test_data.tests.length; i++) { - var t = test_data.tests[i]; - if (t.ua) { - var actual = coarse.parse(t.ua); - if (actual.os != t.os) { - console.error('Error parsing ' + t.ua + ' expected [' + t.os + '] got [' + actual.os + ']'); - } - if (actual.browser != t.browser) { - console.error('Error parsing ' + t.ua + ' expected [' + t.browser + '] got [' + actual.browser + ']'); - } - if (actual.version != t.version) { - console.error('Error parsing ' + t.ua + ' expected [' + t.version + '] got [' + actual.version + ']'); +suite.addBatch({ + "UA parsing": { + topic: function() { + fs.readFile(path.join(__dirname, 'data/user_agents.json'), 'utf-8', this.callback); + }, + "data can be read": function(err, data) { + assert.isNull(err); + }, + "with lots of data": { + topic: function(err, data) { + this.callback(JSON.parse(data)); + }, + "demonstrates proper functioning of coarse parser": function(test_data) { + for (var i=0; i < test_data.tests.length; i++) { + var t = test_data.tests[i]; + if (t.ua) { + var actual = coarse.parse(t.ua); + assert.strictEqual(t.os, actual.os, t.ua); + assert.strictEqual(t.browser, actual.browser, t.ua); + assert.strictEqual(t.version, actual.version, t.ua); + } } } } - console.log('Finished ' + test_data.tests.length); } -}); \ No newline at end of file +}); + +// run or export the suite. +if (process.argv[1] === __filename) suite.run(); +else suite.export(module);