github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/property_test.go (about)

     1  package goquery
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestAttrExists(t *testing.T) {
    10  	if val, ok := Doc().Find("a").Attr("href"); !ok {
    11  		t.Error("Expected a value for the href attribute.")
    12  	} else {
    13  		t.Logf("Href of first anchor: %v.", val)
    14  	}
    15  }
    16  
    17  func TestAttrNotExist(t *testing.T) {
    18  	if val, ok := Doc().Find("div.row-fluid").Attr("href"); ok {
    19  		t.Errorf("Expected no value for the href attribute, got %v.", val)
    20  	}
    21  }
    22  
    23  func TestText(t *testing.T) {
    24  	txt := Doc().Find("h1").Text()
    25  	if strings.Trim(txt, " \n\r\t") != "Provok.in" {
    26  		t.Errorf("Expected text to be Provok.in, found %s.", txt)
    27  	}
    28  }
    29  
    30  func TestText2(t *testing.T) {
    31  	txt := Doc().Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text()
    32  	if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil {
    33  		t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt)
    34  		if e != nil {
    35  			t.Logf("Error: %s.", e.Error())
    36  		}
    37  	}
    38  }
    39  
    40  func TestText3(t *testing.T) {
    41  	txt := Doc().Find(".pvk-gutter").First().Text()
    42  	// There's an   character in there...
    43  	if ok, e := regexp.MatchString(`^[\s\x{00A0}]+$`, txt); !ok || e != nil {
    44  		t.Errorf("Expected spaces, found <%v>.", txt)
    45  		if e != nil {
    46  			t.Logf("Error: %s.", e.Error())
    47  		}
    48  	}
    49  }
    50  
    51  func TestHtml(t *testing.T) {
    52  	txt, e := Doc().Find("h1").Html()
    53  	if e != nil {
    54  		t.Errorf("Error: %s.", e)
    55  	}
    56  
    57  	if ok, e := regexp.MatchString(`^\s*<a href="/">Provok<span class="green">\.</span><span class="red">i</span>n</a>\s*$`, txt); !ok || e != nil {
    58  		t.Errorf("Unexpected HTML content, found %s.", txt)
    59  		if e != nil {
    60  			t.Logf("Error: %s.", e.Error())
    61  		}
    62  	}
    63  }