示例#1
0
func (w *Walker) Walk(node ast.Node) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Println(r)
			fmt.Printf("%T - %+v\n", node, node)
			panic(r)
		}
	}()

	if node == nil {
		fmt.Fprintf(w.W, "%s(<nil>)\n", strings.Repeat("\t", w.tabLevel))
	} else {
		fmt.Fprintf(w.W, "%s(%T)%s\n", strings.Repeat("\t", w.tabLevel), node, node.String())

		switch children := node.Children(); children {
		case nil:
		default:
			w.tabLevel += 1
			for _, child := range children {
				if child != nil {
					w.Walk(child)
				}
			}
			w.tabLevel -= 1
		}
	}
}
示例#2
0
func findDifference(found, expected ast.Node) {
	w := printing.NewWalker()
	foundChildren := found.Children()
	expectedChildren := expected.Children()
	if len(foundChildren) != len(expectedChildren) {
		fmt.Printf("Found Subtree:    %s\n", found)
		w.Walk(found)
		fmt.Printf("Expected Subtree: %+s\n", expected)
		w.Walk(expected)
	} else if len(foundChildren) != 0 && len(foundChildren) == len(expectedChildren) {
		for i := 0; i < len(foundChildren); i++ {
			if !reflect.DeepEqual(foundChildren[i], expectedChildren[i]) {
				findDifference(foundChildren[i], expectedChildren[i])
			}
		}
	} else {
		fmt.Printf("Found Subtree:    %s\n", found)
		w.Walk(found)
		fmt.Printf("Expected Subtree: %+s\n", expected)
		w.Walk(expected)
	}
}