|
| 1 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +//go:build go1.23 |
| 6 | + |
| 7 | +package html |
| 8 | + |
| 9 | +import ( |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNode_ChildNodes(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + in string |
| 17 | + want string |
| 18 | + }{ |
| 19 | + {"", ""}, |
| 20 | + {"<a></a>", "a"}, |
| 21 | + {"a", "a"}, |
| 22 | + {"<a></a><!--b-->", "a b"}, |
| 23 | + {"a<b></b>c", "a b c"}, |
| 24 | + {"a<b><!--c--></b>d", "a b d"}, |
| 25 | + {"<a><b>c<!--d-->e</b></a>f<!--g--><h>i</h>", "a f g h"}, |
| 26 | + } |
| 27 | + for _, test := range tests { |
| 28 | + doc, err := Parse(strings.NewReader(test.in)) |
| 29 | + if err != nil { |
| 30 | + t.Fatal(err) |
| 31 | + } |
| 32 | + // Drill to <html><head></head><body> |
| 33 | + n := doc.FirstChild.FirstChild.NextSibling |
| 34 | + var results []string |
| 35 | + for c := range n.ChildNodes() { |
| 36 | + results = append(results, c.Data) |
| 37 | + } |
| 38 | + if got := strings.Join(results, " "); got != test.want { |
| 39 | + t.Errorf("ChildNodes = %q, want %q", got, test.want) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestNode_Descendants(t *testing.T) { |
| 45 | + tests := []struct { |
| 46 | + in string |
| 47 | + want string |
| 48 | + }{ |
| 49 | + {"", ""}, |
| 50 | + {"<a></a>", "a"}, |
| 51 | + {"<a><b></b></a>", "a b"}, |
| 52 | + {"<a>b</a>", "a b"}, |
| 53 | + {"<a><!--b--></a>", "a b"}, |
| 54 | + {"<a>b<c></c>d</a>", "a b c d"}, |
| 55 | + {"<a>b<c><!--d--></c>e</a>", "a b c d e"}, |
| 56 | + {"<a><b><c>d<!--e-->f</c></b>g<!--h--><i>j</i></a>", "a b c d e f g h i j"}, |
| 57 | + } |
| 58 | + for _, test := range tests { |
| 59 | + doc, err := Parse(strings.NewReader(test.in)) |
| 60 | + if err != nil { |
| 61 | + t.Fatal(err) |
| 62 | + } |
| 63 | + // Drill to <html><head></head><body> |
| 64 | + n := doc.FirstChild.FirstChild.NextSibling |
| 65 | + var results []string |
| 66 | + for c := range n.Descendants() { |
| 67 | + results = append(results, c.Data) |
| 68 | + } |
| 69 | + if got := strings.Join(results, " "); got != test.want { |
| 70 | + t.Errorf("Descendants = %q; want: %q", got, test.want) |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestNode_Ancestors(t *testing.T) { |
| 76 | + for _, size := range []int{0, 1, 2, 10, 100, 10_000} { |
| 77 | + n := buildChain(size) |
| 78 | + nParents := 0 |
| 79 | + for _ = range n.Ancestors() { |
| 80 | + nParents++ |
| 81 | + } |
| 82 | + if nParents != size { |
| 83 | + t.Errorf("number of Ancestors = %d; want: %d", nParents, size) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func buildChain(size int) *Node { |
| 89 | + child := new(Node) |
| 90 | + for range size { |
| 91 | + parent := child |
| 92 | + child = new(Node) |
| 93 | + parent.AppendChild(child) |
| 94 | + } |
| 95 | + return child |
| 96 | +} |
0 commit comments