Path: blob/main/vendor/github.com/google/go-cmp/cmp/report.go
2880 views
// Copyright 2017, The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package cmp56// defaultReporter implements the reporter interface.7//8// As Equal serially calls the PushStep, Report, and PopStep methods, the9// defaultReporter constructs a tree-based representation of the compared value10// and the result of each comparison (see valueNode).11//12// When the String method is called, the FormatDiff method transforms the13// valueNode tree into a textNode tree, which is a tree-based representation14// of the textual output (see textNode).15//16// Lastly, the textNode.String method produces the final report as a string.17type defaultReporter struct {18root *valueNode19curr *valueNode20}2122func (r *defaultReporter) PushStep(ps PathStep) {23r.curr = r.curr.PushStep(ps)24if r.root == nil {25r.root = r.curr26}27}28func (r *defaultReporter) Report(rs Result) {29r.curr.Report(rs)30}31func (r *defaultReporter) PopStep() {32r.curr = r.curr.PopStep()33}3435// String provides a full report of the differences detected as a structured36// literal in pseudo-Go syntax. String may only be called after the entire tree37// has been traversed.38func (r *defaultReporter) String() string {39assert(r.root != nil && r.curr == nil)40if r.root.NumDiff == 0 {41return ""42}43ptrs := new(pointerReferences)44text := formatOptions{}.FormatDiff(r.root, ptrs)45resolveReferences(text)46return text.String()47}4849func assert(ok bool) {50if !ok {51panic("assertion failure")52}53}545556