2016-10-11 00:21:46 +00:00
|
|
|
package main
|
2015-04-14 04:02:29 +00:00
|
|
|
|
|
|
|
import (
|
2023-02-14 21:55:40 +00:00
|
|
|
"bytes"
|
|
|
|
"github.com/avelino/awesome-go/pkg/markdown"
|
2023-02-14 17:30:51 +00:00
|
|
|
"os"
|
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.*[\.\!]`)
|
|
|
|
)
|
|
|
|
|
2023-02-15 00:14:43 +00:00
|
|
|
func requireNoErr(t *testing.T, err error, msg string) {
|
|
|
|
// FIXME: replace to github.com/stretchr/testify
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if msg == "" {
|
|
|
|
msg = "unknown error"
|
2023-02-14 21:55:40 +00:00
|
|
|
}
|
2023-02-15 00:14:43 +00:00
|
|
|
|
2023-02-14 21:55:40 +00:00
|
|
|
if err != nil {
|
2023-02-15 00:14:43 +00:00
|
|
|
t.Fatalf("Received unexpected error [%s]: %+v", msg, err)
|
2023-02-14 21:55:40 +00:00
|
|
|
}
|
2023-02-15 00:14:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 01:33:11 +00:00
|
|
|
func goqueryFromReadme(t *testing.T) *goquery.Document {
|
2023-02-15 00:14:43 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2023-02-15 01:33:11 +00:00
|
|
|
input, err := os.ReadFile(readmePath)
|
2023-02-15 00:14:43 +00:00
|
|
|
requireNoErr(t, err, "readme file should be exists")
|
|
|
|
|
|
|
|
html, err := markdown.ToHTML(input)
|
|
|
|
requireNoErr(t, err, "markdown should be rendered to html")
|
|
|
|
|
2023-02-15 01:33:11 +00:00
|
|
|
buf := bytes.NewBuffer(html)
|
2023-02-15 00:14:43 +00:00
|
|
|
doc, err := goquery.NewDocumentFromReader(buf)
|
|
|
|
requireNoErr(t, err, "html must be valid for goquery")
|
|
|
|
|
|
|
|
return doc
|
2023-02-14 21:55:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 04:02:29 +00:00
|
|
|
func TestAlpha(t *testing.T) {
|
2023-02-15 00:14:43 +00:00
|
|
|
doc := goqueryFromReadme(t)
|
|
|
|
doc.Find("body > ul").Each(func(i int, s *goquery.Selection) {
|
2022-02-04 18:45:43 +00:00
|
|
|
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) {
|
2023-02-15 00:14:43 +00:00
|
|
|
doc := goqueryFromReadme(t)
|
2015-04-23 19:28:54 +00:00
|
|
|
links := make(map[string]bool, 0)
|
2023-02-15 00:14:43 +00:00
|
|
|
doc.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
|
2023-02-14 17:30:51 +00:00
|
|
|
input, err := os.ReadFile(readmePath)
|
2023-02-15 00:26:37 +00:00
|
|
|
requireNoErr(t, err, "readme should be exists")
|
|
|
|
|
2017-08-31 04:18:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 11:16:02 +00:00
|
|
|
|
2023-02-14 23:35:13 +00:00
|
|
|
func TestRenderIndex(t *testing.T) {
|
2023-02-26 00:40:39 +00:00
|
|
|
requireNoErr(t, mkdirAll(outDir), "output dir should exists")
|
|
|
|
|
2023-02-14 23:35:13 +00:00
|
|
|
err := renderIndex(readmePath, outIndexFile)
|
2023-02-15 00:26:37 +00:00
|
|
|
requireNoErr(t, err, "html should be rendered")
|
2019-03-23 10:33:11 +00:00
|
|
|
}
|
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
|
|
|
}
|