From 7e170233f02e2e3ca0d0031d4470fc14c100ac1e Mon Sep 17 00:00:00 2001 From: Jason Allan Date: Mon, 2 Aug 2021 21:01:42 +0100 Subject: [PATCH 1/2] Tests: DRY out Indention tests --- tests/indents/indents.test.ts | 52 ++++++++++++++++------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/tests/indents/indents.test.ts b/tests/indents/indents.test.ts index e7099312..2ffe8a64 100644 --- a/tests/indents/indents.test.ts +++ b/tests/indents/indents.test.ts @@ -1,42 +1,38 @@ +/* eslint-disable jsdoc/require-jsdoc */ import { readFileSync } from 'fs'; import { resolve } from 'path'; -import { format } from 'prettier'; +import { format, Options } from 'prettier'; import { plugin } from './../../src/index'; -describe('Indents', () => { - test('should indent by default with 2 spaces', () => { - const expected: string = readFileSync(resolve(__dirname, 'formatted-2-spaces.pug'), 'utf8'); - const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8'); - const actual: string = format(code, { parser: 'pug', plugins: [plugin] }); +const unformatted: string = readFile('unformatted'); - expect(actual).toBe(expected); - }); - test('should indent with 2 spaces', () => { - const expected: string = readFileSync(resolve(__dirname, 'formatted-2-spaces.pug'), 'utf8'); - const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8'); - const actual: string = format(code, { parser: 'pug', plugins: [plugin], tabWidth: 2 }); +function comparisonTest(message: string, { output, settings }: { output: string; settings?: Options }): void { + test(message, () => { + const expected: string = readFile(output); + const actual: string = format(unformatted, { parser: 'pug', plugins: [plugin], ...settings }); expect(actual).toBe(expected); }); - test('should indent with 3 spaces', () => { - const expected: string = readFileSync(resolve(__dirname, 'formatted-3-spaces.pug'), 'utf8'); - const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8'); - const actual: string = format(code, { parser: 'pug', plugins: [plugin], tabWidth: 3 }); +} - expect(actual).toBe(expected); - }); - test('should indent with 4 spaces', () => { - const expected: string = readFileSync(resolve(__dirname, 'formatted-4-spaces.pug'), 'utf8'); - const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8'); - const actual: string = format(code, { parser: 'pug', plugins: [plugin], tabWidth: 4 }); +function readFile(fileName: string): string { + return readFileSync(resolve(__dirname, fileName + '.pug'), 'utf8'); +} - expect(actual).toBe(expected); +describe('Indents', () => { + // Spaces + comparisonTest('should indent by default with 2 spaces', { + output: 'formatted-2-spaces', + settings: { tabWidth: undefined } }); - test('should indent with tabs', () => { - const expected: string = readFileSync(resolve(__dirname, 'formatted-tabs.pug'), 'utf8'); - const code: string = readFileSync(resolve(__dirname, 'unformatted.pug'), 'utf8'); - const actual: string = format(code, { parser: 'pug', plugins: [plugin], useTabs: true }); + comparisonTest('should indent with 2 spaces', { output: 'formatted-2-spaces', settings: { tabWidth: 2 } }); + comparisonTest('should indent with 3 spaces', { output: 'formatted-3-spaces', settings: { tabWidth: 3 } }); + comparisonTest('should indent with 4 spaces', { output: 'formatted-4-spaces', settings: { tabWidth: 4 } }); + comparisonTest('should indent with 8 spaces', { output: 'formatted-8-spaces', settings: { tabWidth: 8 } }); - expect(actual).toBe(expected); + // Tabs + comparisonTest('should indent with tabs', { + output: 'formatted-tabs-1-width', + settings: { useTabs: true, tabWidth: 1 } }); }); From 6f58955c2606238440f0b0b4c083217aaca9b81f Mon Sep 17 00:00:00 2001 From: Jason Allan Date: Thu, 5 Aug 2021 21:12:46 +0100 Subject: [PATCH 2/2] Assert formatting parity between tabs and spaces --- tests/indents/formatted-2-spaces.pug | 53 ++++++++++++ tests/indents/formatted-3-spaces.pug | 53 ++++++++++++ tests/indents/formatted-4-spaces.pug | 56 +++++++++++++ tests/indents/formatted-8-spaces.pug | 115 +++++++++++++++++++++++++++ tests/indents/formatted-tabs.pug | 48 +++++++++++ tests/indents/indents.test.ts | 27 ++++++- tests/indents/unformatted.pug | 32 ++++++++ 7 files changed, 382 insertions(+), 2 deletions(-) create mode 100644 tests/indents/formatted-8-spaces.pug diff --git a/tests/indents/formatted-2-spaces.pug b/tests/indents/formatted-2-spaces.pug index 7cb4ebb7..61978fd6 100644 --- a/tests/indents/formatted-2-spaces.pug +++ b/tests/indents/formatted-2-spaces.pug @@ -46,3 +46,56 @@ html p text text | | text + + #man-indentation-tests + .short(style="display: none") Hello World + .getting-longer(style="display: none") I can't just say "Hello World" here + .i-am-not-being-creative-with-these-names( + style="display: none", + color="red" + ). + There is surely more I could write here + .short-again(style="display: none", color="red", font="Sans Serif") + | I could surely more I could write here + + .hi(data-wrap="false") This should be wrapped + + section#contact + .container + h3 Contact + form(name="contact", method="POST") + .row.gtr-uniform + .col-6.col-12-x-small + input#name( + type="text", + name="name", + placeholder="Name *", + required + ) + .col-6.col-12-x-small + input#email( + type="email", + name="email", + placeholder="Email *", + required + ) + .col-12 + input#subject( + type="text", + name="subject", + placeholder="Subject *", + required + ) + .col-12 + textarea#message( + name="message", + placeholder="Message *", + rows="6", + required + ) + .col-12 + ul.actions + li + input.primary(type="submit", value="Send Message") + li + input(type="reset", value="Reset Form") diff --git a/tests/indents/formatted-3-spaces.pug b/tests/indents/formatted-3-spaces.pug index 4221dc09..54d78fa2 100644 --- a/tests/indents/formatted-3-spaces.pug +++ b/tests/indents/formatted-3-spaces.pug @@ -46,3 +46,56 @@ html p text text | | text + + #man-indentation-tests + .short(style="display: none") Hello World + .getting-longer(style="display: none") I can't just say "Hello World" here + .i-am-not-being-creative-with-these-names( + style="display: none", + color="red" + ). + There is surely more I could write here + .short-again(style="display: none", color="red", font="Sans Serif") + | I could surely more I could write here + + .hi(data-wrap="false") This should be wrapped + + section#contact + .container + h3 Contact + form(name="contact", method="POST") + .row.gtr-uniform + .col-6.col-12-x-small + input#name( + type="text", + name="name", + placeholder="Name *", + required + ) + .col-6.col-12-x-small + input#email( + type="email", + name="email", + placeholder="Email *", + required + ) + .col-12 + input#subject( + type="text", + name="subject", + placeholder="Subject *", + required + ) + .col-12 + textarea#message( + name="message", + placeholder="Message *", + rows="6", + required + ) + .col-12 + ul.actions + li + input.primary(type="submit", value="Send Message") + li + input(type="reset", value="Reset Form") diff --git a/tests/indents/formatted-4-spaces.pug b/tests/indents/formatted-4-spaces.pug index d1620991..8d89148c 100644 --- a/tests/indents/formatted-4-spaces.pug +++ b/tests/indents/formatted-4-spaces.pug @@ -46,3 +46,59 @@ html p text text | | text + + #man-indentation-tests + .short(style="display: none") Hello World + .getting-longer(style="display: none") I can't just say "Hello World" here + .i-am-not-being-creative-with-these-names( + style="display: none", + color="red" + ). + There is surely more I could write here + .short-again(style="display: none", color="red", font="Sans Serif") + | I could surely more I could write here + + .hi(data-wrap="false") This should be wrapped + + section#contact + .container + h3 Contact + form(name="contact", method="POST") + .row.gtr-uniform + .col-6.col-12-x-small + input#name( + type="text", + name="name", + placeholder="Name *", + required + ) + .col-6.col-12-x-small + input#email( + type="email", + name="email", + placeholder="Email *", + required + ) + .col-12 + input#subject( + type="text", + name="subject", + placeholder="Subject *", + required + ) + .col-12 + textarea#message( + name="message", + placeholder="Message *", + rows="6", + required + ) + .col-12 + ul.actions + li + input.primary( + type="submit", + value="Send Message" + ) + li + input(type="reset", value="Reset Form") diff --git a/tests/indents/formatted-8-spaces.pug b/tests/indents/formatted-8-spaces.pug new file mode 100644 index 00000000..eb33aa88 --- /dev/null +++ b/tests/indents/formatted-8-spaces.pug @@ -0,0 +1,115 @@ +doctype html +html + head + body + h1#title Lets get pugging! + p I'm a paragraph + p. + I'm a multi-line paragraph! + And this is the second line. + p.para. + This paragraph has class! + .firstDiv A div with a class. + #secondDiv A div with an id. + + // Things + div(v-if="things") + .box.flex-box + .flex-main-wrapper + .flex-box.header {{ $t('mylangkey.things') }} + + .flex-box.select( + v-if="things[0].visible" + ) + v-autocomplete( + name="things[0].name", + v-model="things[0].value", + item-text="name", + item-value="id", + :items="mythings" + ) + + .flex-box.select( + v-if="things[1].visible" + ) + v-autocomplete( + name="things[1].name", + v-model="things[1].value", + item-text="name", + item-value="id", + :items="mythings" + ) + + #use-of-id(test) + span Hello + + p text text + | + | text + + p text text + | + | text + + #man-indentation-tests + .short(style="display: none") Hello World + .getting-longer(style="display: none") I can't just say "Hello World" here + .i-am-not-being-creative-with-these-names( + style="display: none", + color="red" + ). + There is surely more I could write here + .short-again( + style="display: none", + color="red", + font="Sans Serif" + ) + | I could surely more I could write here + + .hi(data-wrap="false") This should be wrapped + + section#contact + .container + h3 Contact + form(name="contact", method="POST") + .row.gtr-uniform + .col-6.col-12-x-small + input#name( + type="text", + name="name", + placeholder="Name *", + required + ) + .col-6.col-12-x-small + input#email( + type="email", + name="email", + placeholder="Email *", + required + ) + .col-12 + input#subject( + type="text", + name="subject", + placeholder="Subject *", + required + ) + .col-12 + textarea#message( + name="message", + placeholder="Message *", + rows="6", + required + ) + .col-12 + ul.actions + li + input.primary( + type="submit", + value="Send Message" + ) + li + input( + type="reset", + value="Reset Form" + ) diff --git a/tests/indents/formatted-tabs.pug b/tests/indents/formatted-tabs.pug index b9074cd7..6e062749 100644 --- a/tests/indents/formatted-tabs.pug +++ b/tests/indents/formatted-tabs.pug @@ -46,3 +46,51 @@ html p text text | | text + + #man-indentation-tests + .short(style="display: none") Hello World + .getting-longer(style="display: none") I can't just say "Hello World" here + .i-am-not-being-creative-with-these-names( + style="display: none", + color="red" + ). + There is surely more I could write here + .short-again(style="display: none", color="red", font="Sans Serif") + | I could surely more I could write here + + .hi(data-wrap="false") This should be wrapped + + section#contact + .container + h3 Contact + form(name="contact", method="POST") + .row.gtr-uniform + .col-6.col-12-x-small + input#name(type="text", name="name", placeholder="Name *", required) + .col-6.col-12-x-small + input#email( + type="email", + name="email", + placeholder="Email *", + required + ) + .col-12 + input#subject( + type="text", + name="subject", + placeholder="Subject *", + required + ) + .col-12 + textarea#message( + name="message", + placeholder="Message *", + rows="6", + required + ) + .col-12 + ul.actions + li + input.primary(type="submit", value="Send Message") + li + input(type="reset", value="Reset Form") diff --git a/tests/indents/indents.test.ts b/tests/indents/indents.test.ts index 2ffe8a64..fe2e4644 100644 --- a/tests/indents/indents.test.ts +++ b/tests/indents/indents.test.ts @@ -15,6 +15,22 @@ function comparisonTest(message: string, { output, settings }: { output: string; }); } +const tabIndentRe: RegExp = /\t/g; + +function comparisonTabTest(message: string, { tabWidth }: { tabWidth: number }): void { + test(message, () => { + const spaces: string = format(unformatted, { parser: 'pug', plugins: [plugin], tabWidth, useTabs: false }); + const tabs: string = format(unformatted, { + parser: 'pug', + plugins: [plugin], + tabWidth, + useTabs: true + }).replace(tabIndentRe, ' '.repeat(tabWidth)); + + expect(tabs).toBe(spaces); + }); +} + function readFile(fileName: string): string { return readFileSync(resolve(__dirname, fileName + '.pug'), 'utf8'); } @@ -31,8 +47,15 @@ describe('Indents', () => { comparisonTest('should indent with 8 spaces', { output: 'formatted-8-spaces', settings: { tabWidth: 8 } }); // Tabs + // These tests compare the formatting between tab and space indents and + // asserts they should format the same comparisonTest('should indent with tabs', { - output: 'formatted-tabs-1-width', - settings: { useTabs: true, tabWidth: 1 } + output: 'formatted-tabs', + settings: { useTabs: true, tabWidth: 4 } }); + comparisonTabTest('should format correctly with 1 width tabs', { tabWidth: 1 }); + comparisonTabTest('should format correctly with 2 width tabs', { tabWidth: 2 }); + comparisonTabTest('should format correctly with 4 width tabs', { tabWidth: 4 }); + comparisonTabTest('should format correctly with 8 width tabs', { tabWidth: 8 }); + comparisonTabTest('should format correctly with 16 width tabs', { tabWidth: 16 }); }); diff --git a/tests/indents/unformatted.pug b/tests/indents/unformatted.pug index ddd1f204..82f44bd8 100644 --- a/tests/indents/unformatted.pug +++ b/tests/indents/unformatted.pug @@ -35,3 +35,35 @@ html p text text | | text + + #man-indentation-tests + .short(style="display: none") Hello World + .getting-longer(style="display: none") I can't just say "Hello World" here + .i-am-not-being-creative-with-these-names(style="display: none", color="red"). + There is surely more I could write here + .short-again(style="display: none", color="red", font="Sans Serif") + | I could surely more I could write here + + .hi( + data-wrap="false" + ) This should be wrapped + + section#contact + .container + h3 Contact + form(name="contact", method="POST") + .row.gtr-uniform + .col-6.col-12-x-small + input#name(type="text", name="name", placeholder="Name *", required ) + .col-6.col-12-x-small + input#email( type="email", name="email", placeholder="Email *", required ) + .col-12 + input#subject( type="text", name="subject", placeholder="Subject *", required ) + .col-12 + textarea#message( name="message", placeholder="Message *", rows="6", required ) + .col-12 + ul.actions + li + input.primary( type="submit", value="Send Message" ) + li + input( type="reset", value="Reset Form" )