2016-10-11 00:21:46 +00:00
|
|
|
package main
|
2015-04-14 04:02:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2017-08-31 04:18:01 +00:00
|
|
|
"regexp"
|
2015-04-14 04:02:29 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2022-02-04 18:45:43 +00:00
|
|
|
var (
|
|
|
|
reContainsLink = regexp.MustCompile(`\* \[.*\]\(.*\)`)
|
|
|
|
reOnlyLink = regexp.MustCompile(`\* \[.*\]\([^()]*\)$`)
|
|
|
|
reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!]`)
|
|
|
|
)
|
|
|
|
|
2015-04-14 04:02:29 +00:00
|
|
|
func TestAlpha(t *testing.T) {
|
|
|
|
query := startQuery()
|
2022-02-04 18:45:43 +00:00
|
|
|
query.Find("body > ul").Each(func(i int, s *goquery.Selection) {
|
|
|
|
if i != 0 {
|
|
|
|
// skip content menu
|
|
|
|
// TODO: the sub items (with 3 hash marks `###`) are staying in
|
|
|
|
// the main list, not respecting the hierarchy and making it
|
|
|
|
// impossible to test the alphabetical order
|
|
|
|
testList(t, s)
|
|
|
|
}
|
2015-04-14 04:02:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-23 19:28:54 +00:00
|
|
|
func TestDuplicatedLinks(t *testing.T) {
|
|
|
|
query := startQuery()
|
|
|
|
links := make(map[string]bool, 0)
|
2017-04-08 20:22:12 +00:00
|
|
|
query.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) {
|
2016-12-14 02:53:38 +00:00
|
|
|
t.Run(s.Text(), func(t *testing.T) {
|
|
|
|
href, ok := s.Attr("href")
|
|
|
|
if !ok {
|
|
|
|
t.Error("expected to have href")
|
|
|
|
}
|
|
|
|
if links[href] {
|
|
|
|
t.Fatalf("duplicated link '%s'", href)
|
|
|
|
}
|
|
|
|
links[href] = true
|
|
|
|
})
|
2015-04-23 19:28:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:18:01 +00: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 11:45:07 +00:00
|
|
|
t.Errorf("expected entry to be in form of `* [link] - description.`, got '%s'", line)
|
2017-08-31 04:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-23 10:33:11 +00:00
|
|
|
func TestGenerateHTML(t *testing.T) {
|
2021-12-22 13:12:40 +00:00
|
|
|
err := GenerateHTML()
|
2019-03-23 10:33:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("html generate error '%s'", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2017-08-31 04:18:01 +00:00
|
|
|
|
2015-04-14 04:02:29 +00:00
|
|
|
func testList(t *testing.T, list *goquery.Selection) {
|
|
|
|
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
|
|
|
|
testList(t, items)
|
|
|
|
items.RemoveFiltered("ul")
|
|
|
|
})
|
2022-02-04 18:45:43 +00:00
|
|
|
t.Run(list.Prev().Text(), func(t *testing.T) {
|
2016-12-14 02:53:38 +00:00
|
|
|
checkAlphabeticOrder(t, list)
|
|
|
|
})
|
2015-04-14 04:02:29 +00: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-14 02:53:38 +00:00
|
|
|
t.Errorf("expected '%s' but actual is '%s'", sorted[k], item)
|
2015-04-14 04:02:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-14 02:53:38 +00:00
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("expected order is:\n%s", strings.Join(sorted, "\n"))
|
|
|
|
}
|
2015-04-14 04:02:29 +00:00
|
|
|
}
|