From 89d8ef7a4967f2f505a2e86f7e88620439ea371a Mon Sep 17 00:00:00 2001 From: Carson Date: Sat, 1 Oct 2022 22:31:52 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E7=89=88=E6=B8=AC=E8=A9=A6=20(#3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 5 ++++ v2/main_test.go | 45 +++++++++++++++++++++++++++++++++ v2/testData/basic/expected.html | 21 +++++++++++++++ v2/testData/basic/input.md | 21 +++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 .gitattributes create mode 100644 v2/main_test.go create mode 100644 v2/testData/basic/expected.html create mode 100644 v2/testData/basic/input.md diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..a759715 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +# 管理checkout的eol +# 確保test的文本內容,不會因為eol的問題導致不相等 +* text eol=lf +# *.html text eol=lf +*.exe binary diff --git a/v2/main_test.go b/v2/main_test.go new file mode 100644 index 0000000..10f7974 --- /dev/null +++ b/v2/main_test.go @@ -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) + } +} diff --git a/v2/testData/basic/expected.html b/v2/testData/basic/expected.html new file mode 100644 index 0000000..6434aa6 --- /dev/null +++ b/v2/testData/basic/expected.html @@ -0,0 +1,21 @@ +

Demo

+
+package main
+
+import "fmt"
+
+func main() {
+	fmt.Println("Hello World")
+}
+
+
+
+
package main
+
+import "fmt"
+
+func main() {
+	fmt.Println("Hello World")
+}
+
+
diff --git a/v2/testData/basic/input.md b/v2/testData/basic/input.md new file mode 100644 index 0000000..dd8c135 --- /dev/null +++ b/v2/testData/basic/input.md @@ -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") +} +```