Skip to content

Commit

Permalink
test: improve coverage (#354)
Browse files Browse the repository at this point in the history
* test: improve coverage
* lint
* fix exit with code
  • Loading branch information
D-Sketon authored Aug 31, 2023
1 parent d090bd2 commit da3d8da
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/color.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,53 @@ describe('color', () => {
`${steelblue}`.should.eql('#4682b4');
`${mid1}`.should.eql('rgba(70, 130, 180, 0.53)');
`${mid2}`.should.eql('rgba(70, 130, 180, 0.77)');

// s=0
const grey = new Color('hsl(207, 0%, 49%)');
// h=0
const red1 = new Color('hsl(0, 100%, 50%)');
// h=360
const red2 = new Color('hsl(360, 100%, 50%)');

`${grey}`.should.eql('#7d7d7d');
`${red1}`.should.eql('#f00');
`${red2}`.should.eql('#f00');
});

it('invalid color', () => {
let color;
try {
color = new Color(200);
} catch (e) {
e.message.should.eql('color is required!');
}
try {
color = new Color('rgb(300, 130, 180)');
} catch (e) {
e.message.should.eql('{r: 300, g: 130, b: 180, a: 1} is invalid.');
}
try {
color = new Color('cmyk(0%,0%,0%,0%)');
} catch (e) {
e.message.should.eql('cmyk(0%,0%,0%,0%) is not a supported color format.');
}
(typeof color).should.eql('undefined');
});

it('mix()', () => {
const red = new Color('red');
const pink = new Color('pink');
const mid1 = red.mix(pink, 0);
const mid2 = red.mix(pink, 1);

`${mid1}`.should.eql('#f00');
`${mid2}`.should.eql('#ffc0cb');

try {
red.mix(pink, 2);
} catch (e) {
e.message.should.eql('Valid numbers is only between 0 and 1.');
}
});

});
5 changes: 5 additions & 0 deletions test/decode_url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ describe('decodeURL', () => {
const content = '#f%C3%B3o-b%C3%A1r';
decodeURL(content).should.eql('#fóo-bár');
});

it('data url', () => {
const content = 'data:image/png;base64';
decodeURL(content).should.eql('data:image/png;base64');
});
});
7 changes: 7 additions & 0 deletions test/is_external_link.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,11 @@ describe('isExternalLink', () => {
isExternalLink('https://bar.com/', ctx.config.url, ctx.config.external_link.exclude).should.eql(false);
isExternalLink('https://baz.com/', ctx.config.url, ctx.config.external_link.exclude).should.eql(true);
});

it('invalid sitehost', () => {
ctx.config.url = '';
isExternalLink('https://localhost:4000', ctx.config.url).should.eql(false);
ctx.config.url = 'https://example.com';
});

});
13 changes: 13 additions & 0 deletions test/pattern.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,23 @@ describe('Pattern', () => {
pattern.match('foo').should.eql({});
});

it('Pattern', () => {
const pattern = new Pattern('posts/:id');
new Pattern(pattern).should.eql(pattern);
});

it('rule is required', () => {
(() => {
// eslint-disable-next-line no-new
new Pattern();
}).should.throw('rule must be a function, a string or a regular expression.');
});

it('test function', () => {
const pattern = new Pattern('posts/:id');

pattern.test('/posts/89').should.eql(true);
pattern.test('/post/89').should.eql(false);
});

});
13 changes: 13 additions & 0 deletions test/permalink.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ describe('Permalink', () => {
permalink.regex.should.eql(/^(.+?)_(.+?)_(.+?)_(.+?)$/);
permalink.params.should.eql(['year', 'i_month', 'i_day', 'title']);

permalink = new Permalink(':year/:month/:day/:title', {
segments: {
year: '(\\d{4})',
month: '(\\d{2})',
day: '(\\d{2})'
}
});

permalink.rule.should.eql(':year/:month/:day/:title');
permalink.regex.should.eql(/^(\d{4})\/(\d{2})\/(\d{2})\/(.+?)$/);
permalink.params.should.eql(['year', 'month', 'day', 'title']);

permalink = new Permalink(':year/:month/:day/:title', {
segments: {
year: /(\d{4})/,
Expand Down Expand Up @@ -53,6 +65,7 @@ describe('Permalink', () => {
day: '31',
title: 'test'
});
(typeof permalink.parse('test')).should.eql('undefined');
});

it('stringify()', () => {
Expand Down
7 changes: 7 additions & 0 deletions test/spawn.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ describe('spawn', () => {
});

it('stdio = inherit', () => spawn('echo', ['something'], { stdio: 'inherit' }));

it('exit with code', () => {
if (isWindows) {
return spawn('cmd.exe', ['/c', 'exit', '1'], { stdio: 'inherit' }).should.rejectedWith('Spawn failed');
}
return spawn('sh', ['/c', 'exit', '1'], { stdio: 'inherit' }).should.rejectedWith('Spawn failed');
});
});
11 changes: 11 additions & 0 deletions test/strip_indent.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const stripIndent = require('../dist/strip_indent');

describe('stripIndent', () => {
it('default', () => {
const text = '\tunicorn\n\t\tcake';

stripIndent(text).should.eql('unicorn\n\tcake');
});
});

0 comments on commit da3d8da

Please sign in to comment.