Skip to content

Commit

Permalink
首版測試 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonSlovoka committed Oct 1, 2022
1 parent fa41260 commit 89d8ef7
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 管理checkout的eol
# 確保test的文本內容,不會因為eol的問題導致不相等
* text eol=lf
# *.html text eol=lf
*.exe binary
45 changes: 45 additions & 0 deletions v2/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package highlighting_test

import (
"bytes"
"errors"
"fmt"
highlighting "github.com/CarsonSlovoka/goldmark-highlighting/v2"
"github.com/yuin/goldmark"
"log"
"os"
"strings"
"testing"
)

func startTest(markdown goldmark.Markdown, testDir string) error {
content, err := os.ReadFile(fmt.Sprintf("testData/%s/input.md", testDir))
if err != nil {
return err
}
got := bytes.NewBuffer(make([]byte, 0))
if err = markdown.Convert(content, got); err != nil {
return err
}
want, err := os.ReadFile(fmt.Sprintf("testData/%s/expected.html", testDir))
if err != nil {
return err
}
if got.String() != strings.TrimRight(string(want), "\n") { // 移除結尾多出的空行,避免editorconfig中的insert_final_newline與其衝突
log.Printf("[error]\ngot:\n%s\nwant:\n%s", got, want)
return errors.New("got != want")
}
return nil
}

func Test_NewHighlightingExtender(t *testing.T) {
markdown := goldmark.New(
goldmark.WithExtensions(
highlighting.NewHighlightingExtender(),
),
)

if err := startTest(markdown, "basic"); err != nil {
t.Fatal(err)
}
}
21 changes: 21 additions & 0 deletions v2/testData/basic/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h2>Demo</h2>
<pre>
<code>package main

import "fmt"

func main() {
fmt.Println("Hello World")
}
</code>
</pre>
<div class="highlight Go">
<pre tabindex="0" style="color:#f8f8f2;background-color:#272822;"><code><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> <span style="color:#e6db74">&#34;fmt&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span> <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">&#34;Hello World&#34;</span>)
</span></span><span style="display:flex;"><span>}
</span></span></code></pre>
</div>
21 changes: 21 additions & 0 deletions v2/testData/basic/input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Demo

```
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
```

```go
package main

import "fmt"

func main() {
fmt.Println("Hello World")
}
```

0 comments on commit 89d8ef7

Please sign in to comment.