From c6f8c0939e91f26b0c19c317a935b85c37adaf36 Mon Sep 17 00:00:00 2001 From: Thomas Lynch Date: Wed, 6 Apr 2022 23:31:47 +1000 Subject: [PATCH] add more unit tests to helpers/posting --- helpers/files/formatsize.test.js | 2 +- helpers/posting/diceroll.test.js | 33 +++++++++++ helpers/posting/escape.test.js | 19 +++++++ helpers/posting/fortune.test.js | 12 ++++ helpers/posting/linkmatch.test.js | 42 ++++++++++++++ helpers/posting/name.test.js | 31 +++++++++++ test/pages.test.js | 73 ++++++++++++++++++++++++ test/posting.test.js | 93 +++++++++++++++++++++++++++---- 8 files changed, 292 insertions(+), 13 deletions(-) create mode 100644 helpers/posting/diceroll.test.js create mode 100644 helpers/posting/escape.test.js create mode 100644 helpers/posting/fortune.test.js create mode 100644 helpers/posting/linkmatch.test.js create mode 100644 helpers/posting/name.test.js create mode 100644 test/pages.test.js diff --git a/helpers/files/formatsize.test.js b/helpers/files/formatsize.test.js index 3ab046d8..905caec6 100644 --- a/helpers/files/formatsize.test.js +++ b/helpers/files/formatsize.test.js @@ -1,6 +1,6 @@ const formatSize = require('./formatsize.js'); -describe('helpers/files/formatsize.js', () => { +describe('formatSize() - convert bytes to human readable file size', () => { const cases = [ {in: 1024, out: "1KB"}, {in: Math.pow(1024, 2), out: "1MB"}, diff --git a/helpers/posting/diceroll.test.js b/helpers/posting/diceroll.test.js new file mode 100644 index 00000000..23a3c256 --- /dev/null +++ b/helpers/posting/diceroll.test.js @@ -0,0 +1,33 @@ +const diceroll = require('./diceroll.js'); + +describe('diceroll markdown', () => { + + const prepareCases = [ + { in: '##3d6', out: '##3d6=' }, + { in: '##99d99', out: '##99d99=' }, + { in: '##999d999', out: '##999d999' }, + { in: '##3d8+5', out: '##3d8+5=' }, + { in: '##3d8-5', out: '##3d8-5=' }, + { in: '##0d0', out: '##0d0' }, + ]; + for(let i in prepareCases) { + test(`should contain ${prepareCases[i].out} for an input of ${prepareCases[i].in}`, () => { + const output = prepareCases[i].in.replace(diceroll.regexPrepare, diceroll.prepare.bind(null, false)); + expect(output).toContain(prepareCases[i].out); + }); + } + + const markdownCases = [ + { in: '##3d6=10', out: 'Rolled 3 dice with 6 sides =' }, + { in: '##99d99=5138', out: 'Rolled 99 dice with 99 sides =' }, + { in: '##999d999=10000', out: '##999d999=10000' }, + { in: '##0d0', out: '##0d0' }, + ]; + for(let i in markdownCases) { + test(`should contain ${markdownCases[i].out} for an input of ${markdownCases[i].in}`, () => { + const output = markdownCases[i].in.replace(diceroll.regexMarkdown, diceroll.markdown.bind(null, false)); + expect(output).toContain(markdownCases[i].out); + }); + } + +}); diff --git a/helpers/posting/escape.test.js b/helpers/posting/escape.test.js new file mode 100644 index 00000000..5e050520 --- /dev/null +++ b/helpers/posting/escape.test.js @@ -0,0 +1,19 @@ +const escape = require('./escape.js'); + +describe('escape() - convert some characters to html entities', () => { + const cases = [ + { in: "'", out: ''' }, + { in: '/', out: '/' }, + { in: '`', out: '`' }, + { in: '=', out: '=' }, + { in: '&', out: '&' }, + { in: '<', out: '<' }, + { in: '>', out: '>' }, + { in: '"', out: '"' }, + ]; + for(let i in cases) { + test(`should output ${cases[i].out} for an input of ${cases[i].in}`, () => { + expect(escape(cases[i].in)).toBe(cases[i].out); + }); + } +}); diff --git a/helpers/posting/fortune.test.js b/helpers/posting/fortune.test.js new file mode 100644 index 00000000..a40f8b55 --- /dev/null +++ b/helpers/posting/fortune.test.js @@ -0,0 +1,12 @@ +const fortune = require('./fortune.js'); + +describe('fortune markdown', () => { + const cases = [ + { in: '##fortune', out: "" }, + ]; + for(let i in cases) { + test(`should contain ${cases[i].out} for an input of ${cases[i].in}`, () => { + expect(cases[i].in.replace(fortune.regex, fortune.markdown.bind(null, false))).toContain(cases[i].out) + }); + } +}); diff --git a/helpers/posting/linkmatch.test.js b/helpers/posting/linkmatch.test.js new file mode 100644 index 00000000..334f515a --- /dev/null +++ b/helpers/posting/linkmatch.test.js @@ -0,0 +1,42 @@ +const linkmatch = require('./linkmatch.js'); +const linkRegex = /\[(?