Skip to content

Commit fb69a69

Browse files
committed
Test link format and duplicated content
1 parent 7589e58 commit fb69a69

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

glide.lock

+12-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ package: github.com/victorshinya/awesome-ibmcloud
22
import:
33
- package: github.com/gorilla/mux
44
- package: github.com/shurcooL/github_flavored_markdown
5+
- package: github.com/gomarkdown/markdown
6+
- package: github.com/PuerkitoBio/goquery
7+
version: v1.5.1
8+
- package: github.com/andybalholm/cascadia
9+
version: ~1.2.0

main_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"io/ioutil"
7+
"regexp"
8+
"strings"
9+
"testing"
10+
11+
"github.com/PuerkitoBio/goquery"
12+
"github.com/gomarkdown/markdown"
13+
"github.com/gomarkdown/markdown/parser"
14+
)
15+
16+
var (
17+
regexContainLink = regexp.MustCompile(`\* \[.*\]\(.*\)`)
18+
regexWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!\?]`)
19+
)
20+
21+
func TestDuplicatedContent(t *testing.T) {
22+
html := readMarkdown()
23+
doc, err := goquery.NewDocumentFromReader(html)
24+
if err != nil {
25+
panic(err)
26+
}
27+
list := make(map[string]bool, 0)
28+
doc.Find("ul>li a").Each(func(i int, s *goquery.Selection) {
29+
t.Run(s.Text(), func(t *testing.T) {
30+
href, exist := s.Attr("href")
31+
if !exist {
32+
t.Error("Expected to have a href")
33+
}
34+
if list[href] {
35+
t.Fatalf("Duplicated link %s", href)
36+
}
37+
list[href] = true
38+
})
39+
})
40+
}
41+
42+
func TestLinkFormat(t *testing.T) {
43+
content, err := ioutil.ReadFile("README.md")
44+
if err != nil {
45+
panic(err)
46+
}
47+
list := strings.Split(string(content), "\n")
48+
for _, item := range list {
49+
containLink := regexContainLink.MatchString(item)
50+
if containLink {
51+
isCorrect := regexWithDescription.MatchString(item)
52+
if !isCorrect {
53+
t.Errorf("Expected the correct usage for new links (`* [repo name](repo url) - repo description`) on %s", item)
54+
}
55+
}
56+
}
57+
}
58+
59+
func readMarkdown() io.Reader {
60+
content, err := ioutil.ReadFile("README.md")
61+
if err != nil {
62+
panic(err)
63+
}
64+
65+
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
66+
parser := parser.NewWithExtensions(extensions)
67+
68+
md := []byte(content)
69+
html := markdown.ToHTML(md, parser, nil)
70+
r := bytes.NewReader(html)
71+
return r
72+
}

0 commit comments

Comments
 (0)