From 8da085ef05fbebf12d5d74f99d17f11dc5290f68 Mon Sep 17 00:00:00 2001 From: Duke Date: Tue, 14 Apr 2015 01:02:29 -0300 Subject: [PATCH] add tests to validate alphabetic order #342 --- repo_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 repo_test.go diff --git a/repo_test.go b/repo_test.go new file mode 100644 index 00000000..87eb4b22 --- /dev/null +++ b/repo_test.go @@ -0,0 +1,63 @@ +package repo + +import ( + "bytes" + "io/ioutil" + "log" + "os" + "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(""), blackfriday.MarkdownCommon(input)...) + html = append(html, []byte("")...) + return html +} + +func startQuery() *goquery.Document { + ioutil.WriteFile("z.html", readme(), os.ModePerm) + + 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 { + log.Println("Current: ", item, "=> ", sorted[k]) + if item != sorted[k] { + t.Fatalf("expected '%s' but actual is '%s'", sorted[k], item) + } + } +}