...

Source file src/github.com/yuin/goldmark/ast/ast_test.go

Documentation: github.com/yuin/goldmark/ast

     1  package ast
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestRemoveChildren(t *testing.T) {
     9  	root := NewDocument()
    10  
    11  	node1 := NewDocument()
    12  
    13  	node2 := NewDocument()
    14  
    15  	root.AppendChild(root, node1)
    16  	root.AppendChild(root, node2)
    17  
    18  	root.RemoveChildren(root)
    19  
    20  	t.Logf("%+v", node2.PreviousSibling())
    21  }
    22  
    23  func TestWalk(t *testing.T) {
    24  	tests := []struct {
    25  		name   string
    26  		node   Node
    27  		want   []NodeKind
    28  		action map[NodeKind]WalkStatus
    29  	}{
    30  		{
    31  			"visits all in depth first order",
    32  			node(NewDocument(), node(NewHeading(1), NewText()), NewLink()),
    33  			[]NodeKind{KindDocument, KindHeading, KindText, KindLink},
    34  			map[NodeKind]WalkStatus{},
    35  		},
    36  		{
    37  			"stops after heading",
    38  			node(NewDocument(), node(NewHeading(1), NewText()), NewLink()),
    39  			[]NodeKind{KindDocument, KindHeading},
    40  			map[NodeKind]WalkStatus{KindHeading: WalkStop},
    41  		},
    42  		{
    43  			"skip children",
    44  			node(NewDocument(), node(NewHeading(1), NewText()), NewLink()),
    45  			[]NodeKind{KindDocument, KindHeading, KindLink},
    46  			map[NodeKind]WalkStatus{KindHeading: WalkSkipChildren},
    47  		},
    48  	}
    49  	for _, tt := range tests {
    50  		var kinds []NodeKind
    51  		collectKinds := func(n Node, entering bool) (WalkStatus, error) {
    52  			if entering {
    53  				kinds = append(kinds, n.Kind())
    54  			}
    55  			if status, ok := tt.action[n.Kind()]; ok {
    56  				return status, nil
    57  			}
    58  			return WalkContinue, nil
    59  		}
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			if err := Walk(tt.node, collectKinds); err != nil {
    62  				t.Errorf("Walk() error = %v", err)
    63  			} else if !reflect.DeepEqual(kinds, tt.want) {
    64  				t.Errorf("Walk() expected = %v, got = %v", tt.want, kinds)
    65  			}
    66  		})
    67  	}
    68  }
    69  
    70  func node(n Node, children ...Node) Node {
    71  	for _, c := range children {
    72  		n.AppendChild(n, c)
    73  	}
    74  	return n
    75  }
    76  

View as plain text