From 5cc721859edebeb66d55389cfe92da6ebb967e46 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Fri, 8 Apr 2016 07:39:02 -0400 Subject: [PATCH] edit example to follow Go conventions --- README.md | 2 +- example_test.go | 16 +++++++--------- type_test.go | 13 +++++++++++++ utilities_test.go | 2 +- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 09cad80..df5ade6 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,7 @@ Please note that Cascadia's selectors do not necessarily match all supported sel See some tips and tricks in the [wiki][]. -Taken (and adapted as if executed from outside the goquery package) from example_test.go: +Adapted from example_test.go: ```Go package main diff --git a/example_test.go b/example_test.go index 9e8c3c5..1698a2d 100644 --- a/example_test.go +++ b/example_test.go @@ -1,24 +1,22 @@ -package goquery +package goquery_test import ( "fmt" "log" - // In real use, this import would be required (not in this example, since it - // is part of the goquery package) - //"github.com/PuerkitoBio/goquery" + "github.com/PuerkitoBio/goquery" ) // This example scrapes the reviews shown on the home page of metalsucks.net. -func ExampleScrape_MetalSucks() { - // Load the HTML document (in real use, the type would be *goquery.Document) - doc, err := NewDocument("http://metalsucks.net") +func Example() { + // Load the HTML document + doc, err := goquery.NewDocument("http://metalsucks.net") if err != nil { log.Fatal(err) } - // Find the review items (the type of the Selection would be *goquery.Selection) - doc.Find(".reviews-wrap article .review-rhs").Each(func(i int, s *Selection) { + // Find the review items + doc.Find(".reviews-wrap article .review-rhs").Each(func(i int, s *goquery.Selection) { // For each item found, get the band and title band := s.Find("h3").Text() title := s.Find("i").Text() diff --git a/type_test.go b/type_test.go index 98ee3a6..ec32272 100644 --- a/type_test.go +++ b/type_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "os" + "strings" "testing" "golang.org/x/net/html" @@ -190,3 +191,15 @@ func TestNewDocumentFromResponseNil(t *testing.T) { t.Error("Expected error, got none") } } + +func TestIssue103(t *testing.T) { + d, err := NewDocumentFromReader(strings.NewReader("Scientists Stored These Images in DNA—Then Flawlessly Retrieved Them")) + if err != nil { + t.Error(err) + } + text := d.Find("title").Text() + for i, r := range text { + fmt.Printf("%d: %d - %q\n", i, r, string(r)) + } + fmt.Println(text) +} diff --git a/utilities_test.go b/utilities_test.go index 4e2143d..c8e9d54 100644 --- a/utilities_test.go +++ b/utilities_test.go @@ -77,7 +77,7 @@ func TestNodeNameMultiSel(t *testing.T) { sort.Strings(in) sort.Strings(out) if !reflect.DeepEqual(in, out) { - t.Error("want %v, got %v") + t.Errorf("want %v, got %v", in, out) } }