MCPcopy
hub / github.com/google/go-cmp / FormatDiff

Method FormatDiff

cmp/report_compare.go:98–202  ·  view source on GitHub ↗

FormatDiff converts a valueNode tree into a textNode tree, where the later is a textual representation of the differences detected in the former.

(v *valueNode, ptrs *pointerReferences)

Source from the content-addressed store, hash-verified

96// FormatDiff converts a valueNode tree into a textNode tree, where the later
97// is a textual representation of the differences detected in the former.
98func (opts formatOptions) FormatDiff(v *valueNode, ptrs *pointerReferences) (out textNode) {
99 if opts.DiffMode == diffIdentical {
100 opts = opts.WithVerbosity(1)
101 } else if opts.verbosity() < 3 {
102 opts = opts.WithVerbosity(3)
103 }
104
105 // Check whether we have specialized formatting for this node.
106 // This is not necessary, but helpful for producing more readable outputs.
107 if opts.CanFormatDiffSlice(v) {
108 return opts.FormatDiffSlice(v)
109 }
110
111 var parentKind reflect.Kind
112 if v.parent != nil && v.parent.TransformerName == "" {
113 parentKind = v.parent.Type.Kind()
114 }
115
116 // For leaf nodes, format the value based on the reflect.Values alone.
117 // As a special case, treat equal []byte as a leaf nodes.
118 isBytes := v.Type.Kind() == reflect.Slice && v.Type.Elem() == byteType
119 isEqualBytes := isBytes && v.NumDiff+v.NumIgnored+v.NumTransformed == 0
120 if v.MaxDepth == 0 || isEqualBytes {
121 switch opts.DiffMode {
122 case diffUnknown, diffIdentical:
123 // Format Equal.
124 if v.NumDiff == 0 {
125 outx := opts.FormatValue(v.ValueX, parentKind, ptrs)
126 outy := opts.FormatValue(v.ValueY, parentKind, ptrs)
127 if v.NumIgnored > 0 && v.NumSame == 0 {
128 return textEllipsis
129 } else if outx.Len() < outy.Len() {
130 return outx
131 } else {
132 return outy
133 }
134 }
135
136 // Format unequal.
137 assert(opts.DiffMode == diffUnknown)
138 var list textList
139 outx := opts.WithTypeMode(elideType).FormatValue(v.ValueX, parentKind, ptrs)
140 outy := opts.WithTypeMode(elideType).FormatValue(v.ValueY, parentKind, ptrs)
141 for i := 0; i <= maxVerbosityPreset && outx != nil && outy != nil && outx.Equal(outy); i++ {
142 opts2 := verbosityPreset(opts, i).WithTypeMode(elideType)
143 outx = opts2.FormatValue(v.ValueX, parentKind, ptrs)
144 outy = opts2.FormatValue(v.ValueY, parentKind, ptrs)
145 }
146 if outx != nil {
147 list = append(list, textRecord{Diff: '-', Value: outx})
148 }
149 if outy != nil {
150 list = append(list, textRecord{Diff: '+', Value: outy})
151 }
152 return opts.WithTypeMode(emitType).FormatType(v.Type, list)
153 case diffRemoved:
154 return opts.FormatValue(v.ValueX, parentKind, ptrs)
155 case diffInserted:

Callers 2

formatDiffListMethod · 0.95
StringMethod · 0.80

Calls 15

WithVerbosityMethod · 0.95
verbosityMethod · 0.95
CanFormatDiffSliceMethod · 0.95
FormatDiffSliceMethod · 0.95
FormatValueMethod · 0.95
WithTypeModeMethod · 0.95
FormatTypeMethod · 0.95
formatDiffListMethod · 0.95
assertFunction · 0.85
verbosityPresetFunction · 0.85
wrapTrunkReferencesFunction · 0.85
PushPairMethod · 0.80

Tested by

no test coverage detected