2016-10-10 20:21:46 -04:00
|
|
|
package main
|
2015-04-14 00:02:29 -04:00
|
|
|
|
|
|
|
import (
|
2023-02-14 16:55:40 -05:00
|
|
|
"bytes"
|
|
|
|
"github.com/avelino/awesome-go/pkg/markdown"
|
2023-02-14 12:30:51 -05:00
|
|
|
"os"
|
2017-08-31 00:18:01 -04:00
|
|
|
"regexp"
|
2015-04-14 00:02:29 -04:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/PuerkitoBio/goquery"
|
|
|
|
)
|
|
|
|
|
2022-02-04 13:45:43 -05:00
|
|
|
var (
|
|
|
|
reContainsLink = regexp.MustCompile(`\* \[.*\]\(.*\)`)
|
|
|
|
reOnlyLink = regexp.MustCompile(`\* \[.*\]\([^()]*\)$`)
|
|
|
|
reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!]`)
|
|
|
|
)
|
|
|
|
|
2023-02-14 16:55:40 -05:00
|
|
|
func helpGetReadmeHTML() []byte {
|
|
|
|
input, err := os.ReadFile(readmePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
html, err := markdown.ToHTML(input)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
|
|
|
func helpBuildQuery() *goquery.Document {
|
|
|
|
buf := bytes.NewBuffer(helpGetReadmeHTML())
|
|
|
|
query, err := goquery.NewDocumentFromReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2015-04-14 00:02:29 -04:00
|
|
|
func TestAlpha(t *testing.T) {
|
2023-02-14 16:55:40 -05:00
|
|
|
query := helpBuildQuery()
|
2022-02-04 13:45:43 -05: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 00:02:29 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:28:54 -04:00
|
|
|
func TestDuplicatedLinks(t *testing.T) {
|
2023-02-14 16:55:40 -05:00
|
|
|
query := helpBuildQuery()
|
2015-04-23 15:28:54 -04:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
if links[href] {
|
|
|
|
t.Fatalf("duplicated link '%s'", href)
|
|
|
|
}
|
|
|
|
links[href] = true
|
|
|
|
})
|
2015-04-23 15:28:54 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2023-02-14 12:30:51 -05:00
|
|
|
input, err := os.ReadFile(readmePath)
|
2017-08-31 00:18:01 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-03 07:16:02 -04:00
|
|
|
|
2023-02-14 17:00:00 -05:00
|
|
|
func TestConvertAndRenderIndex(t *testing.T) {
|
2023-02-14 16:55:56 -05:00
|
|
|
err := ConvertAndRenderIndex(readmePath, outIndexFile)
|
2019-03-23 06:33:11 -04:00
|
|
|
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")
|
|
|
|
})
|
2022-02-04 13:45:43 -05:00
|
|
|
t.Run(list.Prev().Text(), func(t *testing.T) {
|
2016-12-13 21:53:38 -05:00
|
|
|
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
|
|
|
}
|