query_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package goquery
  2. import (
  3. "testing"
  4. )
  5. func TestIs(t *testing.T) {
  6. sel := Doc().Find(".footer p:nth-child(1)")
  7. if !sel.Is("p") {
  8. t.Error("Expected .footer p:nth-child(1) to be p.")
  9. }
  10. }
  11. func TestIsInvalid(t *testing.T) {
  12. sel := Doc().Find(".footer p:nth-child(1)")
  13. if sel.Is("") {
  14. t.Error("Is should not succeed with invalid selector string")
  15. }
  16. }
  17. func TestIsPositional(t *testing.T) {
  18. sel := Doc().Find(".footer p:nth-child(2)")
  19. if !sel.Is("p:nth-child(2)") {
  20. t.Error("Expected .footer p:nth-child(2) to be p:nth-child(2).")
  21. }
  22. }
  23. func TestIsPositionalNot(t *testing.T) {
  24. sel := Doc().Find(".footer p:nth-child(1)")
  25. if sel.Is("p:nth-child(2)") {
  26. t.Error("Expected .footer p:nth-child(1) NOT to be p:nth-child(2).")
  27. }
  28. }
  29. func TestIsFunction(t *testing.T) {
  30. ok := Doc().Find("div").IsFunction(func(i int, s *Selection) bool {
  31. return s.HasClass("container-fluid")
  32. })
  33. if !ok {
  34. t.Error("Expected some div to have a container-fluid class.")
  35. }
  36. }
  37. func TestIsFunctionRollback(t *testing.T) {
  38. ok := Doc().Find("div").IsFunction(func(i int, s *Selection) bool {
  39. return s.HasClass("container-fluid")
  40. })
  41. if !ok {
  42. t.Error("Expected some div to have a container-fluid class.")
  43. }
  44. }
  45. func TestIsSelection(t *testing.T) {
  46. sel := Doc().Find("div")
  47. sel2 := Doc().Find(".pvk-gutter")
  48. if !sel.IsSelection(sel2) {
  49. t.Error("Expected some div to have a pvk-gutter class.")
  50. }
  51. }
  52. func TestIsSelectionNot(t *testing.T) {
  53. sel := Doc().Find("div")
  54. sel2 := Doc().Find("a")
  55. if sel.IsSelection(sel2) {
  56. t.Error("Expected some div NOT to be an anchor.")
  57. }
  58. }
  59. func TestIsNodes(t *testing.T) {
  60. sel := Doc().Find("div")
  61. sel2 := Doc().Find(".footer")
  62. if !sel.IsNodes(sel2.Nodes[0]) {
  63. t.Error("Expected some div to have a footer class.")
  64. }
  65. }
  66. func TestDocContains(t *testing.T) {
  67. sel := Doc().Find("h1")
  68. if !Doc().Contains(sel.Nodes[0]) {
  69. t.Error("Expected document to contain H1 tag.")
  70. }
  71. }
  72. func TestSelContains(t *testing.T) {
  73. sel := Doc().Find(".row-fluid")
  74. sel2 := Doc().Find("a[ng-click]")
  75. if !sel.Contains(sel2.Nodes[0]) {
  76. t.Error("Expected .row-fluid to contain a[ng-click] tag.")
  77. }
  78. }
  79. func TestSelNotContains(t *testing.T) {
  80. sel := Doc().Find("a.link")
  81. sel2 := Doc().Find("span")
  82. if sel.Contains(sel2.Nodes[0]) {
  83. t.Error("Expected a.link to NOT contain span tag.")
  84. }
  85. }