aboutsummaryrefslogtreecommitdiff
path: root/extensions/proto/src/main/java/com/google/common/truth/extensions/proto/DiffResult.java
blob: a0ec9373e86e3c89bb13bf515c92a99bb6b4dd18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
/*
 * Copyright (c) 2017 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.truth.extensions.proto;

import com.google.auto.value.AutoValue;
import com.google.auto.value.extension.memoized.Memoized;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.common.truth.extensions.proto.RecursableDiffEntity.WithResultCode.Result;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.ForOverride;
import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Message;
import com.google.protobuf.TextFormat;
import com.google.protobuf.UnknownFieldSet;
import java.io.IOException;
import java.util.Set;

/**
 * Structural summary of the difference between two messages.
 *
 * <p>A {@code DiffResult} has singular fields, repeated fields, and unknowns, each with their own
 * class. The inner classes may also contain their own {@code DiffResult}s for submessages.
 *
 * <p>These classes form a recursive, hierarchical relationship. Much of the common recursion logic
 * across all the classes is in {@link RecursableDiffEntity}.
 */
@AutoValue
abstract class DiffResult extends RecursableDiffEntity.WithoutResultCode {
  /**
   * Structural summary of the difference between two singular (non-repeated) fields.
   *
   * <p>It is possible for the result to be {@code ADDED} or {@code REMOVED}, even if {@code
   * actual()} and {@code expected()} are identical. This occurs if the config does not have {@link
   * FluentEqualityConfig#ignoringFieldAbsence()} enabled, one message had the field explicitly set
   * to the default value, and the other did not.
   */
  @AutoValue
  abstract static class SingularField extends RecursableDiffEntity.WithResultCode {
    /** The type information for this field. May be absent if result code is {@code IGNORED}. */
    abstract Optional<SubScopeId> subScopeId();

    /** The display name for this field. May include an array-index specifier. */
    abstract String fieldName();

    /** The field under test. */
    abstract Optional<Object> actual();

    /** The expected value of said field. */
    abstract Optional<Object> expected();

    /**
     * The detailed breakdown of the comparison, only present if both objects are set on this
     * instance and they are messages.
     *
     * <p>This does not necessarily mean the messages were set on the input protos.
     */
    abstract Optional<DiffResult> breakdown();

    /**
     * The detailed breakdown of the comparison, only present if both objects are set and they are
     * {@link UnknownFieldSet}s.
     *
     * <p>This will only ever be set inside a parent {@link UnknownFieldSetDiff}. The top {@link
     * UnknownFieldSetDiff} is set on the {@link DiffResult}, not here.
     */
    abstract Optional<UnknownFieldSetDiff> unknownsBreakdown();

    /** Returns {@code actual().get()}, or {@code expected().get()}, whichever is available. */
    @Memoized
    Object actualOrExpected() {
      return actual().or(expected()).get();
    }

    @Memoized
    @Override
    Iterable<? extends RecursableDiffEntity> childEntities() {
      return ImmutableList.copyOf(
          Iterables.concat(breakdown().asSet(), unknownsBreakdown().asSet()));
    }

    @Override
    final void printContents(boolean includeMatches, String fieldPrefix, StringBuilder sb) {
      if (!includeMatches && isMatched()) {
        return;
      }

      fieldPrefix = newFieldPrefix(fieldPrefix, fieldName());
      switch (result()) {
        case ADDED:
          sb.append("added: ").append(fieldPrefix).append(": ");
          if (actual().get() instanceof Message) {
            sb.append("\n").append(actual().get());
          } else {
            sb.append(valueString(subScopeId().get(), actual().get())).append("\n");
          }
          return;
        case IGNORED:
          sb.append("ignored: ").append(fieldPrefix).append("\n");
          return;
        case MATCHED:
          sb.append("matched: ").append(fieldPrefix);
          if (actualOrExpected() instanceof Message) {
            sb.append("\n");
            printChildContents(includeMatches, fieldPrefix, sb);
          } else {
            sb.append(": ")
                .append(valueString(subScopeId().get(), actualOrExpected()))
                .append("\n");
          }
          return;
        case MODIFIED:
          sb.append("modified: ").append(fieldPrefix);
          if (actualOrExpected() instanceof Message) {
            sb.append("\n");
            printChildContents(includeMatches, fieldPrefix, sb);
          } else {
            sb.append(": ")
                .append(valueString(subScopeId().get(), expected().get()))
                .append(" -> ")
                .append(valueString(subScopeId().get(), actual().get()))
                .append("\n");
          }
          return;
        case REMOVED:
          sb.append("deleted: ").append(fieldPrefix).append(": ");
          if (expected().get() instanceof Message) {
            sb.append("\n").append(expected().get());
          } else {
            sb.append(valueString(subScopeId().get(), expected().get())).append("\n");
          }
          return;
        default:
          throw new AssertionError("Impossible: " + result());
      }
    }

    @Override
    final boolean isContentEmpty() {
      return false;
    }

    static SingularField ignored(String fieldName) {
      return newBuilder().setFieldName(fieldName).setResult(Result.IGNORED).build();
    }

    static Builder newBuilder() {
      return new AutoValue_DiffResult_SingularField.Builder();
    }

    /** Builder for {@link SingularField}. */
    @CanIgnoreReturnValue
    @AutoValue.Builder
    abstract static class Builder {
      abstract Builder setResult(Result result);

      abstract Builder setSubScopeId(SubScopeId subScopeId);

      abstract Builder setFieldName(String fieldName);

      abstract Builder setActual(Object actual);

      abstract Builder setExpected(Object expected);

      abstract Builder setBreakdown(DiffResult breakdown);

      abstract Builder setUnknownsBreakdown(UnknownFieldSetDiff unknownsBreakdown);

      abstract SingularField build();
    }
  }

  /**
   * Structural summary of the difference between two repeated fields.
   *
   * <p>This is only present if the user specified {@code ignoringRepeatedFieldOrder()}. Otherwise,
   * the repeated elements are compared as singular fields, and there are no 'move' semantics.
   */
  @AutoValue
  abstract static class RepeatedField extends RecursableDiffEntity.WithoutResultCode {

    /**
     * Structural summary of the difference between two elements in two corresponding repeated
     * fields, in the context of a {@link RepeatedField} diff.
     *
     * <p>The field indexes will only be present if the corresponding object is also present. If an
     * object is absent, the PairResult represents an extra/missing element in the repeated field.
     * If both are present but the indexes differ, it represents a 'move'.
     */
    @AutoValue
    abstract static class PairResult extends RecursableDiffEntity.WithResultCode {
      /** The {@link FieldDescriptor} describing the repeated field for this pair. */
      abstract FieldDescriptor fieldDescriptor();

      /** The index of the element in the {@code actual()} list that was matched. */
      abstract Optional<Integer> actualFieldIndex();

      /** The index of the element in the {@code expected()} list that was matched. */
      abstract Optional<Integer> expectedFieldIndex();

      /** The element in the {@code actual()} list that was matched. */
      abstract Optional<Object> actual();

      /** The element in the {@code expected()} list that was matched. */
      abstract Optional<Object> expected();

      /**
       * A detailed breakdown of the comparison between the messages. Present iff {@code actual()}
       * and {@code expected()} are {@link Message}s.
       */
      abstract Optional<DiffResult> breakdown();

      @Memoized
      @Override
      Iterable<? extends RecursableDiffEntity> childEntities() {
        return breakdown().asSet();
      }

      /** Returns true if actual() and expected() contain Message types. */
      @Memoized
      boolean isMessage() {
        return actual().orNull() instanceof Message || expected().orNull() instanceof Message;
      }

      private static String indexed(String fieldPrefix, Optional<Integer> fieldIndex) {
        String index = fieldIndex.isPresent() ? fieldIndex.get().toString() : "?";
        return fieldPrefix + "[" + index + "]";
      }

      @Override
      final void printContents(boolean includeMatches, String fieldPrefix, StringBuilder sb) {
        printContentsForRepeatedField(
            /* includeSelfAlways = */ false, includeMatches, fieldPrefix, sb);
      }

      // When printing results for a repeated field, we want to print matches even if
      // !includeMatches if there's a mismatch on the repeated field itself, but not recursively.
      // So we define a second printing method for use by the parent.
      final void printContentsForRepeatedField(
          boolean includeSelfAlways, boolean includeMatches, String fieldPrefix, StringBuilder sb) {
        if (!includeSelfAlways && !includeMatches && isMatched()) {
          return;
        }

        switch (result()) {
          case ADDED:
            sb.append("added: ").append(indexed(fieldPrefix, actualFieldIndex())).append(": ");
            if (isMessage()) {
              sb.append("\n").append(actual().get());
            } else {
              sb.append(valueString(fieldDescriptor(), actual().get())).append("\n");
            }
            return;
          case IGNORED:
            sb.append("ignored: ");
            if (actualFieldIndex().equals(expectedFieldIndex())) {
              sb.append(indexed(fieldPrefix, actualFieldIndex()));
            } else {
              sb.append(indexed(fieldPrefix, expectedFieldIndex()))
                  .append(" -> ")
                  .append(indexed(fieldPrefix, actualFieldIndex()));
            }

            // We output the message contents for ignored pair results, since it's likely not clear
            // from the index alone why they were ignored.
            sb.append(":");
            if (isMessage()) {
              sb.append("\n");
              printChildContents(includeMatches, indexed(fieldPrefix, actualFieldIndex()), sb);
            } else {
              sb.append(" ").append(valueString(fieldDescriptor(), actual().get())).append("\n");
            }
            return;
          case MATCHED:
            if (actualFieldIndex().get().equals(expectedFieldIndex().get())) {
              sb.append("matched: ").append(indexed(fieldPrefix, actualFieldIndex()));
            } else {
              sb.append("moved: ")
                  .append(indexed(fieldPrefix, expectedFieldIndex()))
                  .append(" -> ")
                  .append(indexed(fieldPrefix, actualFieldIndex()));
            }
            sb.append(":");
            if (isMessage()) {
              sb.append("\n");
              printChildContents(includeMatches, indexed(fieldPrefix, actualFieldIndex()), sb);
            } else {
              sb.append(" ").append(valueString(fieldDescriptor(), actual().get())).append("\n");
            }
            return;
          case MOVED_OUT_OF_ORDER:
            sb.append("out_of_order: ")
                .append(indexed(fieldPrefix, expectedFieldIndex()))
                .append(" -> ")
                .append(indexed(fieldPrefix, actualFieldIndex()));
            sb.append(":");
            if (isMessage()) {
              sb.append("\n");
              printChildContents(includeMatches, indexed(fieldPrefix, actualFieldIndex()), sb);
            } else {
              sb.append(" ").append(valueString(fieldDescriptor(), actual().get())).append("\n");
            }
            return;
          case MODIFIED:
            sb.append("modified: ");
            if (actualFieldIndex().get().equals(expectedFieldIndex().get())) {
              sb.append(indexed(fieldPrefix, actualFieldIndex()));
            } else {
              sb.append(indexed(fieldPrefix, expectedFieldIndex()))
                  .append(" -> ")
                  .append(indexed(fieldPrefix, actualFieldIndex()));
            }
            sb.append(":");
            if (isMessage()) {
              sb.append("\n");
              printChildContents(includeMatches, indexed(fieldPrefix, actualFieldIndex()), sb);
            } else {
              sb.append(" ")
                  .append(valueString(fieldDescriptor(), expected().get()))
                  .append(" -> ")
                  .append(valueString(fieldDescriptor(), actual().get()));
            }
            return;
          case REMOVED:
            sb.append("deleted: ").append(indexed(fieldPrefix, expectedFieldIndex())).append(": ");
            if (isMessage()) {
              sb.append("\n").append(expected().get());
            } else {
              sb.append(valueString(fieldDescriptor(), expected().get())).append("\n");
            }
            return;
        }
        throw new AssertionError("Impossible: " + result());
      }

      @Override
      final boolean isContentEmpty() {
        return false;
      }

      abstract Builder toBuilder();

      static Builder newBuilder() {
        return new AutoValue_DiffResult_RepeatedField_PairResult.Builder();
      }

      @CanIgnoreReturnValue
      @AutoValue.Builder
      abstract static class Builder {
        abstract Builder setResult(Result result);

        abstract Builder setFieldDescriptor(FieldDescriptor fieldDescriptor);

        abstract Builder setActualFieldIndex(int actualFieldIndex);

        abstract Builder setExpectedFieldIndex(int expectedFieldIndex);

        abstract Builder setActual(Object actual);

        abstract Builder setExpected(Object expected);

        abstract Builder setBreakdown(DiffResult breakdown);

        abstract PairResult build();
      }
    }

    /** The {@link FieldDescriptor} for this repeated field. */
    abstract FieldDescriptor fieldDescriptor();

    /** The elements under test. */
    abstract ImmutableList<Object> actual();

    /** The elements expected. */
    abstract ImmutableList<Object> expected();

    // TODO(user,peteg): Also provide a minimum-edit-distance pairing between unmatched elements,
    // and the diff report between them.

    /** Pairs of elements which were diffed against each other. */
    abstract ImmutableList<PairResult> pairResults();

    @Memoized
    @Override
    Iterable<? extends RecursableDiffEntity> childEntities() {
      return pairResults();
    }

    @Override
    final void printContents(boolean includeMatches, String fieldPrefix, StringBuilder sb) {
      fieldPrefix = newFieldPrefix(fieldPrefix, fieldDescriptor().getName());
      for (PairResult pairResult : pairResults()) {
        pairResult.printContentsForRepeatedField(
            /* includeSelfAlways = */ !isMatched(), includeMatches, fieldPrefix, sb);
      }
    }

    @Override
    final boolean isContentEmpty() {
      return pairResults().isEmpty();
    }

    static Builder newBuilder() {
      return new AutoValue_DiffResult_RepeatedField.Builder();
    }

    @CanIgnoreReturnValue
    @AutoValue.Builder
    abstract static class Builder {
      abstract Builder setFieldDescriptor(FieldDescriptor fieldDescriptor);

      abstract Builder setActual(Iterable<?> actual);

      abstract Builder setExpected(Iterable<?> expected);

      @ForOverride
      abstract ImmutableList.Builder<PairResult> pairResultsBuilder();

      final Builder addPairResult(PairResult pairResult) {
        pairResultsBuilder().add(pairResult);
        return this;
      }

      abstract RepeatedField build();
    }
  }

  /** Structural summary of the difference between two unknown field sets. */
  @AutoValue
  abstract static class UnknownFieldSetDiff extends RecursableDiffEntity.WithoutResultCode {
    /** The {@link UnknownFieldSet} being tested. */
    abstract Optional<UnknownFieldSet> actual();

    /** The {@link UnknownFieldSet} expected. */
    abstract Optional<UnknownFieldSet> expected();

    /**
     * A list of top-level singular field comparison results.
     *
     * <p>All unknown fields are treated as repeated and with {@code ignoringRepeatedFieldOrder()}
     * off, because we don't know their nature. If they're optional, only last element matters, but
     * if they're repeated, all elements matter.
     */
    abstract ImmutableListMultimap<Integer, SingularField> singularFields();

    @Memoized
    @Override
    Iterable<? extends RecursableDiffEntity> childEntities() {
      return singularFields().values();
    }

    @Override
    final void printContents(boolean includeMatches, String fieldPrefix, StringBuilder sb) {
      if (!includeMatches && isMatched()) {
        return;
      }

      for (int fieldNumber : singularFields().keySet()) {
        for (SingularField singularField : singularFields().get(fieldNumber)) {
          singularField.printContents(includeMatches, fieldPrefix, sb);
        }
      }
    }

    @Override
    final boolean isContentEmpty() {
      return singularFields().isEmpty();
    }

    static Builder newBuilder() {
      return new AutoValue_DiffResult_UnknownFieldSetDiff.Builder();
    }

    @CanIgnoreReturnValue
    @AutoValue.Builder
    abstract static class Builder {
      abstract Builder setActual(UnknownFieldSet actual);

      abstract Builder setExpected(UnknownFieldSet expected);

      @ForOverride
      abstract ImmutableListMultimap.Builder<Integer, SingularField> singularFieldsBuilder();

      final Builder addSingularField(int fieldNumber, SingularField singularField) {
        singularFieldsBuilder().put(fieldNumber, singularField);
        return this;
      }

      final Builder addAllSingularFields(int fieldNumber, Iterable<SingularField> singularFields) {
        singularFieldsBuilder().putAll(fieldNumber, singularFields);
        return this;
      }

      abstract UnknownFieldSetDiff build();
    }
  }

  /** The {@link Message} being tested. */
  abstract Message actual();

  /** The {@link Message} expected. */
  abstract Message expected();

  /** A list of top-level singular field comparison results grouped by field number. */
  abstract ImmutableListMultimap<Integer, SingularField> singularFields();

  /**
   * A list of top-level repeated field comparison results grouped by field number.
   *
   * <p>This is only populated if {@link FluentEqualityConfig#ignoreRepeatedFieldOrder()} is set.
   * Otherwise, repeated fields are compared strictly in index order, as singular fields.
   */
  abstract ImmutableListMultimap<Integer, RepeatedField> repeatedFields();

  /**
   * The result of comparing the message's {@link UnknownFieldSet}s. Not present if unknown fields
   * were not compared.
   */
  abstract Optional<UnknownFieldSetDiff> unknownFields();

  @Memoized
  @Override
  Iterable<? extends RecursableDiffEntity> childEntities() {
    // Assemble the diffs in field number order so it most closely matches the schema.
    ImmutableList.Builder<RecursableDiffEntity> builder =
        ImmutableList.builderWithExpectedSize(
            singularFields().size() + repeatedFields().size() + unknownFields().asSet().size());
    Set<Integer> fieldNumbers = Sets.union(singularFields().keySet(), repeatedFields().keySet());
    for (int fieldNumber : Ordering.natural().sortedCopy(fieldNumbers)) {
      builder.addAll(singularFields().get(fieldNumber));
      builder.addAll(repeatedFields().get(fieldNumber));
    }
    builder.addAll(unknownFields().asSet());
    return builder.build();
  }

  /** Prints the full {@link DiffResult} to a human-readable string, for use in test outputs. */
  final String printToString(boolean reportMismatchesOnly) {
    StringBuilder sb = new StringBuilder();

    if (!isMatched()) {
      sb.append("Differences were found:\n");
      printContents(/* includeMatches = */ false, /* fieldPrefix = */ "", sb);

      if (!reportMismatchesOnly && isAnyChildMatched()) {
        sb.append("\nFull diff report:\n");
        printContents(/* includeMatches = */ true, /* fieldPrefix = */ "", sb);
      }
    } else {
      sb.append("No differences were found.");
      if (!reportMismatchesOnly) {
        if (isAnyChildIgnored()) {
          sb.append("\nSome fields were ignored for comparison, however.\n");
        } else {
          sb.append("\nFull diff report:\n");
        }
        printContents(/* includeMatches = */ true, /* fieldPrefix = */ "", sb);
      }
    }

    return sb.toString();
  }

  @Override
  final void printContents(boolean includeMatches, String fieldPrefix, StringBuilder sb) {
    for (RecursableDiffEntity child : childEntities()) {
      child.printContents(includeMatches, fieldPrefix, sb);
    }
  }

  @Override
  final boolean isContentEmpty() {
    return Iterables.isEmpty(childEntities());
  }

  static Builder newBuilder() {
    return new AutoValue_DiffResult.Builder();
  }

  private static String newFieldPrefix(String rootFieldPrefix, String toAdd) {
    return rootFieldPrefix.isEmpty() ? toAdd : (rootFieldPrefix + "." + toAdd);
  }

  private static String valueString(SubScopeId subScopeId, Object o) {
    switch (subScopeId.kind()) {
      case FIELD_DESCRIPTOR:
        return valueString(subScopeId.fieldDescriptor(), o);
      case UNKNOWN_FIELD_DESCRIPTOR:
        return valueString(subScopeId.unknownFieldDescriptor(), o);
    }
    throw new AssertionError(subScopeId.kind());
  }

  private static String valueString(FieldDescriptor fieldDescriptor, Object o) {
    StringBuilder sb = new StringBuilder();
    try {
      TextFormat.printFieldValue(fieldDescriptor, o, sb);
      return sb.toString();
    } catch (IOException impossible) {
      throw new AssertionError(impossible);
    }
  }

  private static String valueString(UnknownFieldDescriptor unknownFieldDescriptor, Object o) {
    StringBuilder sb = new StringBuilder();
    try {
      TextFormat.printUnknownFieldValue(unknownFieldDescriptor.type().wireType(), o, sb);
      return sb.toString();
    } catch (IOException impossible) {
      throw new AssertionError(impossible);
    }
  }

  @CanIgnoreReturnValue
  @AutoValue.Builder
  abstract static class Builder {
    abstract Builder setActual(Message actual);

    abstract Builder setExpected(Message expected);

    @ForOverride
    abstract ImmutableListMultimap.Builder<Integer, SingularField> singularFieldsBuilder();

    final Builder addSingularField(int fieldNumber, SingularField singularField) {
      singularFieldsBuilder().put(fieldNumber, singularField);
      return this;
    }

    final Builder addAllSingularFields(int fieldNumber, Iterable<SingularField> singularFields) {
      singularFieldsBuilder().putAll(fieldNumber, singularFields);
      return this;
    }

    @ForOverride
    abstract ImmutableListMultimap.Builder<Integer, RepeatedField> repeatedFieldsBuilder();

    final Builder addRepeatedField(int fieldNumber, RepeatedField repeatedField) {
      repeatedFieldsBuilder().put(fieldNumber, repeatedField);
      return this;
    }

    abstract Builder setUnknownFields(UnknownFieldSetDiff unknownFields);

    abstract DiffResult build();
  }
}