-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get plug URIs from Space Config #1042
- Loading branch information
Showing
3 changed files
with
310 additions
and
42 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,110 @@ | ||
import { assertEquals } from "@std/assert"; | ||
import { insertIntoPlugPage } from "./plugmanager.ts"; | ||
|
||
/** Convenience function simulating repeatedly calling `editor.replaceRange` */ | ||
function replaceRanges( | ||
pageText: string, | ||
ranges: Array<{ from: number; to: number; text: string }>, | ||
): string { | ||
let result = pageText; | ||
for (const { from, to, text } of ranges) { | ||
result = result.substring(0, from) + text + result.substring(to); | ||
} | ||
return result; | ||
} | ||
|
||
const exampleURI = "test:my.plug.js"; | ||
const exampleBlock = `\`\`\`space-config | ||
plugs: | ||
- ${exampleURI} | ||
\`\`\``; | ||
|
||
Deno.test("Updating PLUGS page", () => { | ||
// Empty page | ||
let before = ""; | ||
let after = replaceRanges(before, insertIntoPlugPage(exampleURI, before)); | ||
assertEquals(after, exampleBlock); | ||
|
||
// Page with some content and newline | ||
before = "Lorem ipsum dolor sit amet.\n"; | ||
let expected = `${before}${exampleBlock}`; | ||
after = replaceRanges(before, insertIntoPlugPage(exampleURI, before)); | ||
assertEquals(after, expected); | ||
|
||
// Page without a newline at the end | ||
before = "Lorem ipsum dolor sit amet."; | ||
expected = `${before}\n${exampleBlock}`; | ||
after = replaceRanges(before, insertIntoPlugPage(exampleURI, before)); | ||
assertEquals(after, expected); | ||
|
||
// Old PLUGS page | ||
before = `Old prelude | ||
\`\`\`yaml | ||
- test:old.plug.js | ||
- test:another.plug.js | ||
\`\`\` | ||
Some stuff below`; | ||
expected = `Old prelude | ||
\`\`\`space-config | ||
plugs: | ||
- test:old.plug.js | ||
- test:another.plug.js | ||
- test:my.plug.js | ||
\`\`\` | ||
Some stuff below`; | ||
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before)); | ||
assertEquals(after, expected); | ||
|
||
// Page with an existing space config block | ||
before = `Page content | ||
\`\`\`space-config | ||
foo: | ||
bar: baz | ||
\`\`\` | ||
Some stuff below`; | ||
expected = `Page content | ||
\`\`\`space-config | ||
foo: | ||
bar: baz | ||
plugs: | ||
- test:my.plug.js | ||
\`\`\` | ||
Some stuff below`; | ||
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before)); | ||
assertEquals(after, expected); | ||
|
||
// Append at the end of an existing plugs list | ||
before = `Page content | ||
\`\`\`space-config | ||
plugs: | ||
- test:old.plug.js | ||
# some comment | ||
\`\`\` | ||
`; | ||
expected = `Page content | ||
\`\`\`space-config | ||
plugs: | ||
- test:old.plug.js | ||
# some comment | ||
- test:my.plug.js | ||
\`\`\` | ||
`; | ||
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before)); | ||
assertEquals(after, expected); | ||
|
||
// Why would you do this to yourself? | ||
before = `I love square brackets | ||
\`\`\`space-config | ||
plugs: [ "test:old.plug.js" ] | ||
\`\`\` | ||
`; | ||
expected = `I love square brackets | ||
\`\`\`space-config | ||
plugs: [ "test:old.plug.js" , "test:my.plug.js" ] | ||
\`\`\` | ||
`; | ||
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before)); | ||
assertEquals(after, expected); | ||
}); |
Oops, something went wrong.