Added coverage test

This commit is contained in:
Xiaoyi 2014-07-11 18:38:09 +08:00
parent 16ee21d2ab
commit fc105a23c9
4 changed files with 34 additions and 17 deletions

View file

@ -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 = {};
}
});

View file

@ -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 <ashi009@gmail.com>",
"license": "MIT",
@ -14,6 +16,16 @@
},
"devDependencies": {
"mocha": "*",
"should": "*"
"should": "*",
"blanket": "*",
"travis-cov": "*"
},
"config": {
"blanket": {
"pattern": "index.js"
},
"travis-cov": {
"threshold": 70
}
}
}

View file

@ -62,7 +62,7 @@ describe('HTML Parser', function() {
var parseHTML = HTMLParser.parse;
describe('parse', function() {
describe('parse()', function() {
it('should parse "<p id=\\"id\\"><a class=\'cls\'>Hello</a><ul><li><li></ul><span></span></p>" 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('<script>1</script><style>2</style>', {
var root = parseHTML('<script>1</script><style>2&amp;</style>', {
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&amp;')]);
root.lastChild.text.should.eql('2&');
root.lastChild.rawText.should.eql('2&amp;');
});
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() {

3
test/mocha.opts Normal file
View file

@ -0,0 +1,3 @@
--require blanket
--require should
--reporter spec