From fc105a23c98283e4d90d93d5dab41620807264c5 Mon Sep 17 00:00:00 2001 From: Xiaoyi Date: Fri, 11 Jul 2014 18:38:09 +0800 Subject: [PATCH] Added coverage test --- index.js | 10 +++++----- package.json | 16 ++++++++++++++-- test/html.js | 22 ++++++++++++---------- test/mocha.opts | 3 +++ 4 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 test/mocha.opts diff --git a/index.js b/index.js index 8417a86..01a1c61 100644 --- a/index.js +++ b/index.js @@ -396,7 +396,7 @@ $define(HTMLElement, { * Cache to store generated match functions * @type {Object} */ -var gMatchFunctionCache = {}; +var pMatchFunctionCache = {}; /** * Matcher class to make CSS match @@ -404,8 +404,8 @@ var gMatchFunctionCache = {}; */ function Matcher(selector) { this.matchers = selector.split(' ').map(function(matcher) { - if (gMatchFunctionCache[matcher]) - return gMatchFunctionCache[matcher]; + if (pMatchFunctionCache[matcher]) + return pMatchFunctionCache[matcher]; var parts = matcher.split('.'); var tagName = parts[0]; var classes = parts.slice(1).sort(); @@ -419,7 +419,7 @@ function Matcher(selector) { if (classes.length > 0) source += 'for (var cls = ' + JSON.stringify(classes) + ', i = 0; i < cls.length; i++) if (el.classNames.indexOf(cls[i]) === -1) return false;'; source += 'return true;'; - return gMatchFunctionCache[matcher] = new Function('el', source); + return pMatchFunctionCache[matcher] = new Function('el', source); }); this.nextMatch = 0; } @@ -463,7 +463,7 @@ $define(Matcher, { * flush cache to free memory */ flushCache: function() { - gMatchFunctionCache = {}; + pMatchFunctionCache = {}; } }); diff --git a/package.json b/package.json index 778bf21..4af2b61 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,9 @@ "description": "A very fast HTML parser, generating a simplified DOM, with basic element query support.", "main": "index.js", "scripts": { - "test": "mocha" + "test": "mocha", + "posttest": "mocha -R travis-cov", + "coverage": "mocha -R html-cov > coverage.html" }, "author": "Xiaoyi Shi ", "license": "MIT", @@ -14,6 +16,16 @@ }, "devDependencies": { "mocha": "*", - "should": "*" + "should": "*", + "blanket": "*", + "travis-cov": "*" + }, + "config": { + "blanket": { + "pattern": "index.js" + }, + "travis-cov": { + "threshold": 70 + } } } diff --git a/test/html.js b/test/html.js index 1bf1a69..eaa22df 100644 --- a/test/html.js +++ b/test/html.js @@ -62,7 +62,7 @@ describe('HTML Parser', function() { var parseHTML = HTMLParser.parse; - describe('parse', function() { + describe('parse()', function() { it('should parse "

Hello

" and return root element', function() { @@ -119,16 +119,18 @@ describe('HTML Parser', function() { it('should extract text in script and style when ask so', function() { - var root = parseHTML('', { + var root = parseHTML('', { script: true, style: true }); root.firstChild.childNodes.should.not.be.empty; root.firstChild.childNodes.should.eql([new TextNode('1')]); + root.firstChild.text.should.eql('1'); root.lastChild.childNodes.should.not.be.empty; - root.lastChild.childNodes.should.eql([new TextNode('2')]); - + root.lastChild.childNodes.should.eql([new TextNode('2&')]); + root.lastChild.text.should.eql('2&'); + root.lastChild.rawText.should.eql('2&'); }); it('should be able to parse "html/incomplete-script" file', function() { @@ -159,7 +161,7 @@ describe('HTML Parser', function() { describe('TextNode', function() { - describe('isWhitespace', function() { + describe('#isWhitespace', function() { var node = new TextNode(''); node.isWhitespace.should.be.ok; node = new TextNode(' \t'); @@ -172,7 +174,7 @@ describe('HTML Parser', function() { describe('HTMLElement', function() { - describe('removeWhitespace', function() { + describe('#removeWhitespace()', function() { it('should remove whitespaces while preserving nodes with content', function() { @@ -188,7 +190,7 @@ describe('HTML Parser', function() { }); - describe('rawAttributes', function() { + describe('#rawAttributes', function() { it('should return escaped attributes of the element', function() { @@ -204,7 +206,7 @@ describe('HTML Parser', function() { }); - describe('attributes', function() { + describe('#attributes', function() { it('should return attributes of the element', function() { @@ -220,7 +222,7 @@ describe('HTML Parser', function() { }); - describe('querySelectorAll', function() { + describe('#querySelectorAll()', function() { it('should return correct elements in DOM tree', function() { @@ -237,7 +239,7 @@ describe('HTML Parser', function() { }); - describe('structuredText', function() { + describe('#structuredText', function() { it('should return correct structured text', function() { diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..dc7a122 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,3 @@ +--require blanket +--require should +--reporter spec