Skip to content

Commit

Permalink
Fix tests to have 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Dec 16, 2015
1 parent 31a767e commit a91cf00
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 47 deletions.
11 changes: 6 additions & 5 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ gulp.task('coverage:instrument', () => {
gulp.task('coverage:report', () => {
let istanbul = require('gulp-istanbul');
return gulp.src('test/*.es6', { read: false })
.pipe(istanbul.writeReports())
.pipe(istanbul.writeReports({
reporters: ['lcov', 'text-summary']
}))
.pipe(istanbul.enforceThresholds({
thresholds: {
global: {
statements: 98,
functions: 98,
branches: 95,
lines: 97
statements: 100,
functions: 100,
lines: 100
}
}
}));
Expand Down
3 changes: 3 additions & 0 deletions lib/container.es6
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export default class Container extends Node {
}

remove(child) {
/* istanbul ignore if */
if ( typeof child !== 'undefined' ) {
warnOnce('Container#remove is deprecated. ' +
'Use Container#removeChild');
Expand Down Expand Up @@ -300,7 +301,9 @@ export default class Container extends Node {
}

let processed = nodes.map( (i) => {
/* istanbul ignore if */
if ( typeof i.raws === 'undefined' ) i = this.rebuild(i);

if ( i.parent ) i = i.clone();
if ( typeof i.raws.before === 'undefined' ) {
if ( sample && typeof sample.raws.before !== 'undefined' ) {
Expand Down
1 change: 1 addition & 0 deletions lib/lazy-result.es6
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export default class LazyResult {
}
}
} catch (err) {
/* istanbul ignore next */
if ( console && console.error ) console.error(err);
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/map-generator.es6
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export default class {
}

file = path.relative(from, file);
/* istanbul ignore next */
if ( path.sep === '\\' ) {
return file.replace(/\\/g, '/');
} else {
Expand Down
22 changes: 12 additions & 10 deletions lib/node.es6
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ export default class Node {
return result.warn(message, { node: this });
}

removeSelf() {
warnOnce('Node#removeSelf is deprecated. Use Node#remove.');
return this.remove();
}

remove() {
if ( this.parent ) {
this.parent.removeChild(this);
Expand All @@ -63,11 +58,6 @@ export default class Node {
return this;
}

replace(nodes) {
warnOnce('Node#replace is deprecated. Use Node#replaceWith');
return this.replaceWith(nodes);
}

toString(stringifier = stringify) {
if ( stringifier.stringify ) stringifier = stringifier.stringify;
let result = '';
Expand Down Expand Up @@ -209,6 +199,18 @@ export default class Node {
return pos;
}

/* istanbul ignore next */
removeSelf() {
warnOnce('Node#removeSelf is deprecated. Use Node#remove.');
return this.remove();
}

/* istanbul ignore next */
replace(nodes) {
warnOnce('Node#replace is deprecated. Use Node#replaceWith');
return this.replaceWith(nodes);
}

/* istanbul ignore next */
style(own, detect) {
warnOnce('Node#style() is deprecated. Use Node#raw()');
Expand Down
18 changes: 2 additions & 16 deletions lib/parser.es6
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ export default class Parser {
if ( token[0] === ':' ) {
node.raws.between += token[1];
break;
} else if ( token[0] !== 'space' && token[0] !== 'comment' ) {
this.unknownDecl(node, token);
} else {
node.raws.between += token[1];
}
Expand Down Expand Up @@ -328,16 +326,10 @@ export default class Parser {

endFile() {
if ( this.current.parent ) this.unclosedBlock();

if ( this.current.nodes && this.current.nodes.length ) {
this.current.raws.semicolon = this.semicolon;
}
this.current.raws.after = (this.current.raws.after || '') + this.spaces;

while ( this.current.parent ) {
this.current = this.current.parent;
this.current.raws.after = '';
}
}

// Helpers
Expand Down Expand Up @@ -366,8 +358,7 @@ export default class Parser {
}
}
if ( !clean ) {
let raw = '';
for ( token of tokens ) raw += token[1];
let raw = tokens.reduce( (all, i) => all + i[1], '');
node.raws[prop] = { value, raw };
}
node[prop] = value;
Expand Down Expand Up @@ -414,11 +405,10 @@ export default class Parser {
if ( type === '(' ) {
brackets += 1;
} else if ( type === ')' ) {
brackets -= 0;
brackets -= 1;
} else if ( brackets === 0 && type === ':' ) {
if ( !prev ) {
this.doubleColon(token);
continue;
} else if ( prev[0] === 'word' && prev[1] === 'progid' ) {
continue;
} else {
Expand All @@ -433,10 +423,6 @@ export default class Parser {

// Errors

unknownDecl(node, token) {
throw this.input.error('Unknown word', token[2], token[3]);
}

unclosedBracket(bracket) {
throw this.input.error('Unclosed bracket', bracket[2], bracket[3]);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/stringifier.es6
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ export default class Stringifier {
}

body(node) {
if ( !node.nodes ) return;

let last = node.nodes.length - 1;
while ( last > 0 ) {
if ( node.nodes[last].type !== 'comment' ) break;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"devDependencies": {
"concat-with-sourcemaps": "1.0.4",
"postcss-parser-tests": "5.0.4",
"postcss-parser-tests": "5.0.5",
"gulp-json-editor": "2.2.1",
"gulp-istanbul": "0.10.3",
"run-sequence": "1.1.5",
Expand Down
9 changes: 9 additions & 0 deletions test/at-rule.es6
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe('AtRule', () => {
expect(rule.toString()).to.eql('@encoding "utf-8"');
});

describe('each()', () => {

it('does not fall on childless at-rule', () => {
let rule = new AtRule();
expect(rule.each( i => i )).to.be.an('undefined');
});

});

describe('prepend()', () => {

it('creates nodes property', () => {
Expand Down
80 changes: 79 additions & 1 deletion test/container.es6
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ describe('Container', () => {
expect(size).to.eql(2);
});

it('breaks declarations filter by name', () => {
let css = parse('@page{a{one:1}}b{one:1;two:2}');
let size = 0;

css.walkDecls('one', () => {
size += 1;
return false;
});

expect(size).to.eql(1);
});

it('filters declarations by property regexp', () => {
let css = parse('@page{a{one:1}}b{one-x:1;two:2}');
let size = 0;
Expand All @@ -259,6 +271,18 @@ describe('Container', () => {
expect(size).to.eql(2);
});

it('breaks declarations filters by regexp', () => {
let css = parse('@page{a{one:1}}b{one-x:1;two:2}');
let size = 0;

css.walkDecls(/one(-x)?/, () => {
size += 1;
return false;
});

expect(size).to.eql(1);
});

});

describe('walkComments()', () => {
Expand Down Expand Up @@ -337,6 +361,42 @@ describe('Container', () => {
expect(indexes).to.eql([0]);
});

it('filters by selector', () => {
let size = 0;
parse('a{}b{}a{}').walkRules('a', (rule) => {
expect(rule.selector).to.eql('a');
size += 1;
});
expect(size).to.eql(2);
});

it('breaks selector filters', () => {
let size = 0;
parse('a{}b{}a{}').walkRules('a', () => {
size += 1;
return false;
});
expect(size).to.eql(1);
});

it('filters by regexp', () => {
let size = 0;
parse('a{}a b{}b a{}').walkRules(/^a/, (rule) => {
expect(rule.selector).to.match(/^a/);
size += 1;
});
expect(size).to.eql(2);
});

it('breaks selector regexp', () => {
let size = 0;
parse('a{}b a{}b a{}').walkRules(/^a/, () => {
size += 1;
return false;
});
expect(size).to.eql(1);
});

});

describe('walkAtRules()', () => {
Expand Down Expand Up @@ -388,15 +448,33 @@ describe('Container', () => {
expect(size).to.eql(3);
});

it('breaks name filter', () => {
let size = 0;
parse('@page{@page{@page{}}}').walkAtRules('page', () => {
size += 1;
return false;
});
expect(size).to.eql(1);
});

it('filters at-rules by name regexp', () => {
let css = parse('@page{@page 2{}}@media print{@page{}}');
let css = parse('@page{@page 2{}}@media print{@pages{}}');
let size = 0;

css.walkAtRules(/page/, () => size += 1 );

expect(size).to.eql(3);
});

it('breaks regexp filter', () => {
let size = 0;
parse('@page{@pages{@page{}}}').walkAtRules(/page/, () => {
size += 1;
return false;
});
expect(size).to.eql(1);
});

});

describe('append()', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/css-syntax-error.es6
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('CssSyntaxError', () => {
let error = parseError('a{', {
from: '/b.css',
to: '/c.css',
map: { prev: input.map }
map: { prev: input.map }
});
expect(error.source).to.eql('a{}');
});
Expand Down
9 changes: 9 additions & 0 deletions test/lazy-result.es6
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ describe('LazyResult', () => {

});

describe('opts', () => {

it('contains AST', () => {
let result = new LazyResult(processor, 'a {}', { to: 'a.css' });
expect(result.opts).to.eql({ to: 'a.css' });
});

});

describe('warnings()', () => {

it('contains warnings', () => {
Expand Down
18 changes: 18 additions & 0 deletions test/node.es6
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,24 @@ describe('Node', () => {
']}');
});

it('converts custom properties', () => {
let root = new Root();
root._cache = [1];
root._hack = {
toJSON() {
return 'hack';
}
};

expect(root.toJSON()).to.eql({
type: 'root',
nodes: [],
raws: { },
_hack: 'hack',
_cache: [1]
});
});

});

describe('raw()', () => {
Expand Down
9 changes: 7 additions & 2 deletions test/parse.es6
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ describe('postcss.parse()', () => {
expect(root.first.first.value).to.eql('())');
});

it('ignores symbols before declaration', () => {
let root = parse('a { :one: 1 }');
expect(root.first.first.raws.before).to.eql(' :');
});

describe('errors', () => {

it('throws on unclosed blocks', () => {
Expand Down Expand Up @@ -96,8 +101,8 @@ describe('postcss.parse()', () => {
});

it('throws on property without semicolon', () => {
expect( () => parse('a { one: 1 two: 2 }') )
.to.throw(/:1:10: Missed semicolon/);
expect( () => parse('a { one: filter(a:"") two: 2 }') )
.to.throw(/:1:21: Missed semicolon/);
});

it('throws on double colon', () => {
Expand Down
Loading

0 comments on commit a91cf00

Please sign in to comment.