generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add tests to cover edge cases (#30)
* test: add tests to demonstrate that using a combination of preserveNewLineIndentation and removeAllLastNewlines helper functions can handle the edge cases specified in the issue
- Loading branch information
1 parent
de13e95
commit 9386bae
Showing
4 changed files
with
61 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './preserveNewlineIndentation' | ||
export * from './removeAllLastNewLines' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Handler of double new line characters (\n\n) to preserve proper indentation in text blocks | ||
export const preserveNewlineIndentation = (textBlock: string): string => { | ||
const stringWithNewLines = /\n+\s*/g; | ||
|
||
return stringWithNewLines.test(textBlock) ? textBlock.replace(stringWithNewLines, '\n') : textBlock; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { preserveNewlineIndentation, removeAllLastNewlines } from 'src/utils' | ||
|
||
describe('representativeText', () => { | ||
test('Should remove double newline characters at the end of text', () => { | ||
const text = `reboot?\n\n` | ||
const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) | ||
const expected = `reboot?` | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
test('Should remove triple newline characters at the end of text', () => { | ||
const text = `project.\n\n\n` | ||
const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) | ||
const expected = "project." | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
test('Should remove a newline character and double tab characters at the end of text', () => { | ||
const text = `simple answers.\n\t\t` | ||
const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) | ||
const expected = "simple answers." | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
test('Should remove a newline character and triple tab characters at the end of text', () => { | ||
const text = `success.\n\t\t\t` | ||
const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) | ||
const expected = "success." | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
test('Should remove multiple newline characters and tab characters at the end of text', () => { | ||
const text = `instead.\n\t\t\t\t\n\t\t\t\t\n\t\t\t\t\t` | ||
const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) | ||
const expected = "instead." | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
test('Should remove multiple newline characters and tab characters at the end of a longer text', () => { | ||
const text = `a book\n\t\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\t\n\t\t\t\t\t\t\t` | ||
const actual = removeAllLastNewlines(preserveNewlineIndentation(text)) | ||
const expected = "a book" | ||
|
||
expect(actual).toEqual(expected) | ||
}) | ||
}) |