From da3d8dafe95f77a9d724d7f4aac2fdd9721bb237 Mon Sep 17 00:00:00 2001 From: D-Sketon <2055272094@qq.com> Date: Fri, 1 Sep 2023 00:53:57 +0800 Subject: [PATCH] test: improve coverage (#354) * test: improve coverage * lint * fix exit with code --- test/color.spec.js | 48 +++++++++++++++++++++++++++++++++++ test/decode_url.spec.js | 5 ++++ test/is_external_link.spec.js | 7 +++++ test/pattern.spec.js | 13 ++++++++++ test/permalink.spec.js | 13 ++++++++++ test/spawn.spec.js | 7 +++++ test/strip_indent.spec.js | 11 ++++++++ 7 files changed, 104 insertions(+) create mode 100644 test/strip_indent.spec.js diff --git a/test/color.spec.js b/test/color.spec.js index be5ae0e7..093c385c 100644 --- a/test/color.spec.js +++ b/test/color.spec.js @@ -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.'); + } + }); + }); diff --git a/test/decode_url.spec.js b/test/decode_url.spec.js index f3fa942f..d6bc3c3e 100644 --- a/test/decode_url.spec.js +++ b/test/decode_url.spec.js @@ -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'); + }); }); diff --git a/test/is_external_link.spec.js b/test/is_external_link.spec.js index d72e0a0e..2b06821a 100644 --- a/test/is_external_link.spec.js +++ b/test/is_external_link.spec.js @@ -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'; + }); + }); diff --git a/test/pattern.spec.js b/test/pattern.spec.js index f3c8476b..75fa3d5a 100644 --- a/test/pattern.spec.js +++ b/test/pattern.spec.js @@ -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); + }); + }); diff --git a/test/permalink.spec.js b/test/permalink.spec.js index b84d14a2..d30b5db4 100644 --- a/test/permalink.spec.js +++ b/test/permalink.spec.js @@ -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})/, @@ -53,6 +65,7 @@ describe('Permalink', () => { day: '31', title: 'test' }); + (typeof permalink.parse('test')).should.eql('undefined'); }); it('stringify()', () => { diff --git a/test/spawn.spec.js b/test/spawn.spec.js index 372f805a..0a6476b9 100644 --- a/test/spawn.spec.js +++ b/test/spawn.spec.js @@ -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'); + }); }); diff --git a/test/strip_indent.spec.js b/test/strip_indent.spec.js new file mode 100644 index 00000000..90d3065b --- /dev/null +++ b/test/strip_indent.spec.js @@ -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'); + }); +});