add tests

This commit is contained in:
Niccolo Borgioli 2023-11-16 15:13:19 +01:00
parent b11f94a647
commit 47627ed330
No known key found for this signature in database
GPG Key ID: D93C615F75EE4F0B
26 changed files with 304 additions and 4 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -30,12 +30,12 @@
"./dist"
],
"scripts": {
"build": "rm -rf ./dist && tsc && bun build ./src/index.ts --outfile dist/index.js --target node"
"build": "bun test && rm -rf ./dist && tsc && bun build ./src/index.ts --outfile dist/index.js --target node",
"prepublishOnly": "bun run build"
},
"devDependencies": {
"bun-types": "latest"
},
"peerDependencies": {
"bun-types": "latest",
"markdown-it": "^13.0.2",
"typescript": "^5.0.0"
},
"engines": {

View File

@ -0,0 +1,73 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP
exports[`base no imports 1`] = `
"<h1>Some title</h1>
<ul>
<li>Some</li>
<li>List</li>
<li>Three</li>
</ul>
<blockquote>
<p>Some note</p>
</blockquote>
<table>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
<th>Column C</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some</td>
<td><code>stuff</code></td>
<td></td>
</tr>
</tbody>
</table>
"
`;
exports[`base single import 1`] = `
"<p>Here is a typescript snippet</p>
<pre><code class="language-ts">export function sum(a: number, b: number): number {
return a + b
}
</code></pre>
"
`;
exports[`base multiple imports 1`] = `
"<p>Here is a typescript snippet</p>
<pre><code class="language-ts">export function sum(a: number, b: number): number {
return a + b
}
</code></pre>
<blockquote>
<p>This is some amazing quote</p>
</blockquote>
"
`;
exports[`base custom lines 1`] = `
"<h1>Bubble sort in go</h1>
<pre><code class="language-go">func bubbleSort(arr []int) {
len := len(arr)
for i := 0; i &lt; len-1; i++ {
for j := 0; j &lt; len-i-1; j++ {
if arr[j] &gt; arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
fmt.Println(&quot;\nAfter Bubble Sorting&quot;)
for _, val := range arr {
fmt.Println(val)
}
}
</code></pre>
"
`;

View File

@ -0,0 +1,40 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP
exports[`options matcher custom matcher 1`] = `
"<p>This is some amazing quote</p>
"
`;
exports[`options matcher custom matcher 2`] = `
"<p>This is some amazing quote</p>
"
`;
exports[`options empty options 1`] = `
"<h1>Some title</h1>
<ul>
<li>Some</li>
<li>List</li>
<li>Three</li>
</ul>
<blockquote>
<p>Some note</p>
</blockquote>
<table>
<thead>
<tr>
<th>Column A</th>
<th>Column B</th>
<th>Column C</th>
</tr>
</thead>
<tbody>
<tr>
<td>Some</td>
<td><code>stuff</code></td>
<td></td>
</tr>
</tbody>
</table>
"
`;

View File

@ -0,0 +1,15 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP
exports[`base import different files 1`] = `
"<h1>I am gonna import different stuff</h1>
<h2>Here is a typescript snippet</h2>
<blockquote>
<p>TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.</p>
</blockquote>
<pre><code class="language-ts">export function sum(a: number, b: number): number {
return a + b
}
</code></pre>
"
`;

24
tests/base.test.ts Normal file
View File

@ -0,0 +1,24 @@
import { describe, expect, test } from 'bun:test'
import { render } from './utils.ts'
describe('base', () => {
test('no imports', async () => {
expect(await render('simple.md')).toMatchSnapshot()
})
test('single import', async () => {
expect(await render('whole.md')).toMatchSnapshot()
})
test('multiple imports', async () => {
expect(await render('multiple.md')).toMatchSnapshot()
})
test('fail on not found', async () => {
expect(() => render('notFound.md')).toThrow(/cannot locate file.*nirvana\.md/)
})
test('custom lines', async () => {
expect(await render('partial.md')).toMatchSnapshot()
})
})

25
tests/fixtures/bubble-sort.go vendored Normal file
View File

@ -0,0 +1,25 @@
package main
import "fmt"
func main() {
sample := []int{3, 4, 5, 2, 1}
bubbleSort(sample)
sample = []int{3, 4, 5, 2, 1, 7, 8, -1, -3}
bubbleSort(sample)
}
func bubbleSort(arr []int) {
len := len(arr)
for i := 0; i < len-1; i++ {
for j := 0; j < len-i-1; j++ {
if arr[j] > arr[j+1] {
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
fmt.Println("\nAfter Bubble Sorting")
for _, val := range arr {
fmt.Println(val)
}
}

3
tests/fixtures/cycle-a.md vendored Normal file
View File

@ -0,0 +1,3 @@
# This should be impossible
@import(tests/fixtures/cycle-b.md)

3
tests/fixtures/cycle-b.md vendored Normal file
View File

@ -0,0 +1,3 @@
# This should be impossible
@import(tests/fixtures/cycle-a.md)

1
tests/fixtures/matcher-a.md vendored Normal file
View File

@ -0,0 +1 @@
foo 'tests/fixtures/quote.txt'

1
tests/fixtures/matcher-b.md vendored Normal file
View File

@ -0,0 +1 @@
$tests/fixtures/quote.txt$

7
tests/fixtures/multiple.md vendored Normal file
View File

@ -0,0 +1,7 @@
Here is a typescript snippet
```ts
@import(tests/fixtures/sum.ts)
```
> @import(tests/fixtures/quote.txt)

3
tests/fixtures/notFound.md vendored Normal file
View File

@ -0,0 +1,3 @@
# This needs to fail
@import(some/path/to/nirvana.md)

5
tests/fixtures/partial.md vendored Normal file
View File

@ -0,0 +1,5 @@
# Bubble sort in go
```go
@import(tests/fixtures/bubble-sort.go)[12-25]
```

1
tests/fixtures/quote.txt vendored Normal file
View File

@ -0,0 +1 @@
This is some amazing quote

3
tests/fixtures/rec-a.md vendored Normal file
View File

@ -0,0 +1,3 @@
# I am gonna import different stuff
@import(tests/fixtures/rec-b.md)

7
tests/fixtures/rec-b.md vendored Normal file
View File

@ -0,0 +1,7 @@
## Here is a typescript snippet
> @import(tests/fixtures/rec-c.txt)
```ts
@import(tests/fixtures/sum.ts)
```

1
tests/fixtures/rec-c.txt vendored Normal file
View File

@ -0,0 +1 @@
TypeScript is a strongly typed programming language that builds on JavaScript, giving you better tooling at any scale.

5
tests/fixtures/relative.md vendored Normal file
View File

@ -0,0 +1,5 @@
Here is a typescript snippet
```ts
@import(sum.ts)
```

3
tests/fixtures/self-reference.md vendored Normal file
View File

@ -0,0 +1,3 @@
# This should be impossible
@import(tests/fixtures/self-reference.md)

11
tests/fixtures/simple.md vendored Normal file
View File

@ -0,0 +1,11 @@
# Some title
- Some
- List
- Three
> Some note
| Column A | Column B | Column C |
| -------- | -------- | -------- |
| Some | `stuff` | |

3
tests/fixtures/sum.ts vendored Normal file
View File

@ -0,0 +1,3 @@
export function sum(a: number, b: number): number {
return a + b
}

5
tests/fixtures/whole.md vendored Normal file
View File

@ -0,0 +1,5 @@
Here is a typescript snippet
```ts
@import(tests/fixtures/sum.ts)
```

33
tests/options.test.ts Normal file
View File

@ -0,0 +1,33 @@
import { describe, expect, test } from 'bun:test'
import { render } from './utils.ts'
describe('options', () => {
test('empty options', async () => {
expect(await render('simple.md', {})).toMatchSnapshot()
})
test('set root directory', async () => {
expect(await render('relative.md', { root: 'tests/fixtures' }))
})
describe('matcher', () => {
test('custom matcher', async () => {
const matcher = /foo '(?<file>.+)'/g
expect(await render('matcher-a.md', { matcher })).toMatchSnapshot()
})
test('custom matcher', async () => {
const matcher = /\$(?<file>.+)\$/g
expect(await render('matcher-b.md', { matcher })).toMatchSnapshot()
})
test('fail for non global regexp', async () => {
const matcher = /foo '(?<file>.+)'/
expect(() => render('matcher-a.md', { matcher })).toThrow('RegExp must be global')
})
test('fail for missing "file" group', async () => {
const matcher = /foo '(.+)'/g
expect(() => render('matcher-a.md', { matcher })).toThrow('Regexp must expose a named group "file"')
})
})
})

17
tests/recursion.test.ts Normal file
View File

@ -0,0 +1,17 @@
import { describe, expect, test } from 'bun:test'
import { render } from './utils.ts'
describe('base', () => {
test('should not be able to import itself', async () => {
expect(() => render('self-reference.md')).toThrow(/cycles are not allowed, already parsed.*self-reference\.md/)
})
test('should not be able to import cycles', async () => {
expect(() => render('cycle-a.md')).toThrow(/cycles are not allowed, already parsed.*cycle-b\.md/)
expect(() => render('cycle-b.md')).toThrow(/cycles are not allowed, already parsed.*cycle-a\.md/)
})
test('import different files', async () => {
expect(await render('rec-a.md')).toMatchSnapshot()
})
})

11
tests/utils.ts Normal file
View File

@ -0,0 +1,11 @@
import { readFile } from 'fs/promises'
import { join } from 'path'
import MarkdownIt from 'markdown-it'
import { Options, importPlugin } from '../src/index.ts'
export async function render(path: string, options: Options | undefined = undefined): Promise<string> {
const filename = join('./tests/fixtures', path)
const input = await readFile(filename, 'utf-8')
const output = MarkdownIt().use(importPlugin, options).render(input)
return output
}