2016-10-10 20:21:46 -04:00
|
|
|
package main
|
2015-04-14 00:02:29 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2019-03-23 06:33:11 -04:00
|
|
|
"os"
|
2017-08-31 00:18:01 -04:00
|
|
|
"regexp"
|
2015-04-14 00:02:29 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-03-23 06:33:11 -04:00
|
|
|
"text/template"
|
2015-04-14 00:02:29 -04:00
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
2019-03-23 06:33:11 -04:00
|
|
|
gfm "github.com/shurcooL/github_flavored_markdown"
|
2015-04-14 00:02:29 -04:00
|
|
|
)
|
|
|
|
|
2019-03-23 06:33:11 -04:00
|
|
|
type content struct {
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2015-04-14 00:02:29 -04:00
|
|
|
func TestAlpha(t *testing.T) {
|
|
|
|
query := startQuery()
|
|
|
|
|
|
|
|
query.Find("body > ul").Each(func(_ int, s *goquery.Selection) {
|
|
|
|
testList(t, s)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:28:54 -04:00
|
|
|
func TestDuplicatedLinks(t *testing.T) {
|
|
|
|
query := startQuery()
|
|
|
|
links := make(map[string]bool, 0)
|
|
|
|
|
2017-04-08 16:22:12 -04:00
|
|
|
query.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) {
|
2016-12-13 21:53:38 -05:00
|
|
|
t.Run(s.Text(), func(t *testing.T) {
|
|
|
|
href, ok := s.Attr("href")
|
|
|
|
if !ok {
|
|
|
|
t.Error("expected to have href")
|
|
|
|
}
|
2015-04-23 15:28:54 -04:00
|
|
|
|
2016-12-13 21:53:38 -05:00
|
|
|
if links[href] {
|
|
|
|
t.Fatalf("duplicated link '%s'", href)
|
|
|
|
}
|
2015-04-23 15:28:54 -04:00
|
|
|
|
2016-12-13 21:53:38 -05:00
|
|
|
links[href] = true
|
|
|
|
})
|
2015-04-23 15:28:54 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-31 00:18:01 -04:00
|
|
|
var (
|
|
|
|
reContainsLink = regexp.MustCompile(`\* \[.*\]\(.*\)`)
|
2019-04-02 19:25:56 -04:00
|
|
|
reOnlyLink = regexp.MustCompile(`\* \[.*\]\([^()]*\)$`)
|
2018-12-14 06:45:07 -05:00
|
|
|
reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!]`)
|
2017-08-31 00:18:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test if an entry has description, it must be separated from link with ` - `
|
|
|
|
func TestSeparator(t *testing.T) {
|
|
|
|
var matched, containsLink, noDescription bool
|
|
|
|
input, err := ioutil.ReadFile("./README.md")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
lines := strings.Split(string(input), "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
line = strings.Trim(line, " ")
|
|
|
|
containsLink = reContainsLink.MatchString(line)
|
|
|
|
if containsLink {
|
|
|
|
noDescription = reOnlyLink.MatchString(line)
|
|
|
|
if noDescription {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matched = reLinkWithDescription.MatchString(line)
|
|
|
|
if !matched {
|
2018-12-14 06:45:07 -05:00
|
|
|
t.Errorf("expected entry to be in form of `* [link] - description.`, got '%s'", line)
|
2017-08-31 00:18:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 06:33:11 -04:00
|
|
|
func TestGenerateHTML(t *testing.T) {
|
|
|
|
err := generateHTML()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("html generate error '%s'", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2017-08-31 00:18:01 -04:00
|
|
|
|
2015-04-14 00:02:29 -04:00
|
|
|
func testList(t *testing.T, list *goquery.Selection) {
|
|
|
|
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
|
|
|
|
testList(t, items)
|
|
|
|
items.RemoveFiltered("ul")
|
|
|
|
})
|
2016-12-13 21:53:38 -05:00
|
|
|
|
|
|
|
category := list.Prev().Text()
|
|
|
|
|
|
|
|
t.Run(category, func(t *testing.T) {
|
|
|
|
checkAlphabeticOrder(t, list)
|
|
|
|
})
|
2015-04-14 00:02:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkAlphabeticOrder(t *testing.T, s *goquery.Selection) {
|
|
|
|
items := s.Find("li > a:first-child").Map(func(_ int, li *goquery.Selection) string {
|
|
|
|
return strings.ToLower(li.Text())
|
|
|
|
})
|
|
|
|
|
|
|
|
sorted := make([]string, len(items))
|
|
|
|
copy(sorted, items)
|
|
|
|
sort.Strings(sorted)
|
|
|
|
|
|
|
|
for k, item := range items {
|
|
|
|
if item != sorted[k] {
|
2016-12-13 21:53:38 -05:00
|
|
|
t.Errorf("expected '%s' but actual is '%s'", sorted[k], item)
|
2015-04-14 00:02:29 -04:00
|
|
|
}
|
|
|
|
}
|
2016-12-13 21:53:38 -05:00
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("expected order is:\n%s", strings.Join(sorted, "\n"))
|
|
|
|
}
|
2015-04-14 00:02:29 -04:00
|
|
|
}
|
2019-03-23 06:33:11 -04:00
|
|
|
|
|
|
|
func generateHTML() (err error) {
|
|
|
|
// options
|
|
|
|
readmePath := "./README.md"
|
|
|
|
tplPath := "tmpl/tmpl.html"
|
|
|
|
idxPath := "tmpl/index.html"
|
|
|
|
input, _ := ioutil.ReadFile(readmePath)
|
|
|
|
body := string(gfm.Markdown(input))
|
|
|
|
c := &content{Body: body}
|
|
|
|
t := template.Must(template.ParseFiles(tplPath))
|
|
|
|
f, err := os.Create(idxPath)
|
|
|
|
t.Execute(f, c)
|
|
|
|
return
|
|
|
|
}
|