...

Source file src/github.com/PuerkitoBio/goquery/property_test.go

Documentation: github.com/PuerkitoBio/goquery

     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 TestAttrOr(t *testing.T) {
    18  	if val := Doc().Find("a").AttrOr("fake-attribute", "alternative"); val != "alternative" {
    19  		t.Error("Expected an alternative value for 'fake-attribute' attribute.")
    20  	} else {
    21  		t.Logf("Value returned for not existing attribute: %v.", val)
    22  	}
    23  	if val := Doc().Find("zz").AttrOr("fake-attribute", "alternative"); val != "alternative" {
    24  		t.Error("Expected an alternative value for 'fake-attribute' on an empty selection.")
    25  	} else {
    26  		t.Logf("Value returned for empty selection: %v.", val)
    27  	}
    28  }
    29  
    30  func TestAttrNotExist(t *testing.T) {
    31  	if val, ok := Doc().Find("div.row-fluid").Attr("href"); ok {
    32  		t.Errorf("Expected no value for the href attribute, got %v.", val)
    33  	}
    34  }
    35  
    36  func TestRemoveAttr(t *testing.T) {
    37  	sel := Doc2Clone().Find("div")
    38  
    39  	sel.RemoveAttr("id")
    40  
    41  	_, ok := sel.Attr("id")
    42  	if ok {
    43  		t.Error("Expected there to be no id attributes set")
    44  	}
    45  }
    46  
    47  func TestSetAttr(t *testing.T) {
    48  	sel := Doc2Clone().Find("#main")
    49  
    50  	sel.SetAttr("id", "not-main")
    51  
    52  	val, ok := sel.Attr("id")
    53  	if !ok {
    54  		t.Error("Expected an id attribute on main")
    55  	}
    56  
    57  	if val != "not-main" {
    58  		t.Errorf("Expected an attribute id to be not-main, got %s", val)
    59  	}
    60  }
    61  
    62  func TestSetAttr2(t *testing.T) {
    63  	sel := Doc2Clone().Find("#main")
    64  
    65  	sel.SetAttr("foo", "bar")
    66  
    67  	val, ok := sel.Attr("foo")
    68  	if !ok {
    69  		t.Error("Expected an 'foo' attribute on main")
    70  	}
    71  
    72  	if val != "bar" {
    73  		t.Errorf("Expected an attribute 'foo' to be 'bar', got '%s'", val)
    74  	}
    75  }
    76  
    77  func TestText(t *testing.T) {
    78  	txt := Doc().Find("h1").Text()
    79  	if strings.Trim(txt, " \n\r\t") != "Provok.in" {
    80  		t.Errorf("Expected text to be Provok.in, found %s.", txt)
    81  	}
    82  }
    83  
    84  func TestText2(t *testing.T) {
    85  	txt := Doc().Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text()
    86  	if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil {
    87  		t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt)
    88  		if e != nil {
    89  			t.Logf("Error: %s.", e.Error())
    90  		}
    91  	}
    92  }
    93  
    94  func TestText3(t *testing.T) {
    95  	txt := Doc().Find(".pvk-gutter").First().Text()
    96  	// There's an   character in there...
    97  	if ok, e := regexp.MatchString(`^[\s\x{00A0}]+$`, txt); !ok || e != nil {
    98  		t.Errorf("Expected spaces, found <%v>.", txt)
    99  		if e != nil {
   100  			t.Logf("Error: %s.", e.Error())
   101  		}
   102  	}
   103  }
   104  
   105  func TestHtml(t *testing.T) {
   106  	txt, e := Doc().Find("h1").Html()
   107  	if e != nil {
   108  		t.Errorf("Error: %s.", e)
   109  	}
   110  
   111  	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 {
   112  		t.Errorf("Unexpected HTML content, found %s.", txt)
   113  		if e != nil {
   114  			t.Logf("Error: %s.", e.Error())
   115  		}
   116  	}
   117  }
   118  
   119  func TestNbsp(t *testing.T) {
   120  	src := `<p>Some&nbsp;text</p>`
   121  	d, err := NewDocumentFromReader(strings.NewReader(src))
   122  	if err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	txt := d.Find("p").Text()
   126  	ix := strings.Index(txt, "\u00a0")
   127  	if ix != 4 {
   128  		t.Errorf("Text: expected a non-breaking space at index 4, got %d", ix)
   129  	}
   130  
   131  	h, err := d.Find("p").Html()
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	ix = strings.Index(h, "\u00a0")
   136  	if ix != 4 {
   137  		t.Errorf("Html: expected a non-breaking space at index 4, got %d", ix)
   138  	}
   139  }
   140  
   141  func TestAddClass(t *testing.T) {
   142  	sel := Doc2Clone().Find("#main")
   143  	sel.AddClass("main main main")
   144  
   145  	// Make sure that class was only added once
   146  	if a, ok := sel.Attr("class"); !ok || a != "main" {
   147  		t.Error("Expected #main to have class main")
   148  	}
   149  }
   150  
   151  func TestAddClassSimilar(t *testing.T) {
   152  	sel := Doc2Clone().Find("#nf5")
   153  	sel.AddClass("odd")
   154  
   155  	assertClass(t, sel, "odd")
   156  	assertClass(t, sel, "odder")
   157  	printSel(t, sel.Parent())
   158  }
   159  
   160  func TestAddEmptyClass(t *testing.T) {
   161  	sel := Doc2Clone().Find("#main")
   162  	sel.AddClass("")
   163  
   164  	// Make sure that class was only added once
   165  	if a, ok := sel.Attr("class"); ok {
   166  		t.Errorf("Expected #main to not to have a class, have: %s", a)
   167  	}
   168  }
   169  
   170  func TestAddClasses(t *testing.T) {
   171  	sel := Doc2Clone().Find("#main")
   172  	sel.AddClass("a b")
   173  
   174  	// Make sure that class was only added once
   175  	if !sel.HasClass("a") || !sel.HasClass("b") {
   176  		t.Errorf("#main does not have classes")
   177  	}
   178  }
   179  
   180  func TestHasClass(t *testing.T) {
   181  	sel := Doc().Find("div")
   182  	if !sel.HasClass("span12") {
   183  		t.Error("Expected at least one div to have class span12.")
   184  	}
   185  }
   186  
   187  func TestHasClassNone(t *testing.T) {
   188  	sel := Doc().Find("h2")
   189  	if sel.HasClass("toto") {
   190  		t.Error("Expected h1 to have no class.")
   191  	}
   192  }
   193  
   194  func TestHasClassNotFirst(t *testing.T) {
   195  	sel := Doc().Find(".alert")
   196  	if !sel.HasClass("alert-error") {
   197  		t.Error("Expected .alert to also have class .alert-error.")
   198  	}
   199  }
   200  
   201  func TestRemoveClass(t *testing.T) {
   202  	sel := Doc2Clone().Find("#nf1")
   203  	sel.RemoveClass("one row")
   204  
   205  	if !sel.HasClass("even") || sel.HasClass("one") || sel.HasClass("row") {
   206  		classes, _ := sel.Attr("class")
   207  		t.Error("Expected #nf1 to have class even, has ", classes)
   208  	}
   209  }
   210  
   211  func TestRemoveClassSimilar(t *testing.T) {
   212  	sel := Doc2Clone().Find("#nf5, #nf6")
   213  	assertLength(t, sel.Nodes, 2)
   214  
   215  	sel.RemoveClass("odd")
   216  	assertClass(t, sel.Eq(0), "odder")
   217  	printSel(t, sel)
   218  }
   219  
   220  func TestRemoveAllClasses(t *testing.T) {
   221  	sel := Doc2Clone().Find("#nf1")
   222  	sel.RemoveClass()
   223  
   224  	if a, ok := sel.Attr("class"); ok {
   225  		t.Error("All classes were not removed, has ", a)
   226  	}
   227  
   228  	sel = Doc2Clone().Find("#main")
   229  	sel.RemoveClass()
   230  	if a, ok := sel.Attr("class"); ok {
   231  		t.Error("All classes were not removed, has ", a)
   232  	}
   233  }
   234  
   235  func TestToggleClass(t *testing.T) {
   236  	sel := Doc2Clone().Find("#nf1")
   237  
   238  	sel.ToggleClass("one")
   239  	if sel.HasClass("one") {
   240  		t.Error("Expected #nf1 to not have class one")
   241  	}
   242  
   243  	sel.ToggleClass("one")
   244  	if !sel.HasClass("one") {
   245  		t.Error("Expected #nf1 to have class one")
   246  	}
   247  
   248  	sel.ToggleClass("one even row")
   249  	if a, ok := sel.Attr("class"); ok {
   250  		t.Errorf("Expected #nf1 to have no classes, have %q", a)
   251  	}
   252  }
   253  

View as plain text