You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
awesome-go/repo_test.go

61 lines
1.2 KiB
Go

package repo
import (
"bytes"
"io/ioutil"
"log"
"sort"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/russross/blackfriday"
)
func TestAlpha(t *testing.T) {
query := startQuery()
query.Find("body > ul").Each(func(_ int, s *goquery.Selection) {
testList(t, s)
})
}
func testList(t *testing.T, list *goquery.Selection) {
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
testList(t, items)
items.RemoveFiltered("ul")
})
checkAlphabeticOrder(t, list)
}
func readme() []byte {
input, _ := ioutil.ReadFile("./README.md")
html := append([]byte("<body>"), blackfriday.MarkdownCommon(input)...)
html = append(html, []byte("</body>")...)
return html
}
func startQuery() *goquery.Document {
buf := bytes.NewBuffer(readme())
query, _ := goquery.NewDocumentFromReader(buf)
return query
}
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] {
log.Printf("expected '%s' but actual is '%s'", sorted[k], item)
t.Fail()
}
}
}