Skip to content

Commit f070204

Browse files
torquestompronshapiro
authored andcommitted
Add ignoringExtraRepeatedFieldElements() to ProtoTruth.
RELNOTES=Add ignoringExtraRepeatedFieldElements() to ProtoTruth. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=191472165
1 parent 7d5868b commit f070204

15 files changed

+487
-27
lines changed

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/DiffResult.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,9 @@ final void printContents(boolean includeMatches, String fieldPrefix, StringBuild
148148
sb.append(valueString(fieldDescriptorOrUnknown().get(), expected().get())).append("\n");
149149
}
150150
return;
151+
default:
152+
throw new AssertionError("Impossible: " + result());
151153
}
152-
throw new AssertionError("Impossible: " + result());
153154
}
154155

155156
@Override
@@ -278,7 +279,16 @@ final void printContentsForRepeatedField(
278279
.append(" -> ")
279280
.append(indexed(fieldPrefix, actualFieldIndex()));
280281
}
281-
sb.append("\n");
282+
283+
// We output the message contents for ignored pair results, since it's likely not clear
284+
// from the index alone why they were ignored.
285+
sb.append(":");
286+
if (isMessage()) {
287+
sb.append("\n");
288+
printChildContents(includeMatches, indexed(fieldPrefix, actualFieldIndex()), sb);
289+
} else {
290+
sb.append(" ").append(valueString(fieldDescriptor(), actual().get())).append("\n");
291+
}
282292
return;
283293
case MATCHED:
284294
if (actualFieldIndex().get().equals(expectedFieldIndex().get())) {
@@ -297,6 +307,19 @@ final void printContentsForRepeatedField(
297307
sb.append(" ").append(valueString(fieldDescriptor(), actual().get())).append("\n");
298308
}
299309
return;
310+
case MOVED_OUT_OF_ORDER:
311+
sb.append("out_of_order: ")
312+
.append(indexed(fieldPrefix, expectedFieldIndex()))
313+
.append(" -> ")
314+
.append(indexed(fieldPrefix, actualFieldIndex()));
315+
sb.append(":");
316+
if (isMessage()) {
317+
sb.append("\n");
318+
printChildContents(includeMatches, indexed(fieldPrefix, actualFieldIndex()), sb);
319+
} else {
320+
sb.append(" ").append(valueString(fieldDescriptor(), actual().get())).append("\n");
321+
}
322+
return;
300323
case MODIFIED:
301324
sb.append("modified: ");
302325
if (actualFieldIndex().get().equals(expectedFieldIndex().get())) {
@@ -334,6 +357,8 @@ final boolean isContentEmpty() {
334357
return false;
335358
}
336359

360+
abstract Builder toBuilder();
361+
337362
static Builder newBuilder() {
338363
return new AutoValue_DiffResult_RepeatedField_PairResult.Builder();
339364
}

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/FluentEqualityConfig.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ abstract class FluentEqualityConfig {
4848
new AutoValue_FluentEqualityConfig.Builder()
4949
.setIgnoreFieldAbsence(false)
5050
.setIgnoreRepeatedFieldOrder(false)
51+
.setIgnoreExtraRepeatedFieldElements(false)
5152
.setCompareExpectedFieldsOnly(false)
5253
.setFieldScopeLogic(FieldScopeLogic.all())
5354
.setReportMismatchesOnly(false)
@@ -76,7 +77,7 @@ public ProtoTruthMessageDifferencer load(Descriptor descriptor) {
7677

7778
abstract boolean ignoreRepeatedFieldOrder();
7879

79-
abstract boolean reportMismatchesOnly();
80+
abstract boolean ignoreExtraRepeatedFieldElements();
8081

8182
abstract Optional<Correspondence<Number, Number>> doubleCorrespondence();
8283

@@ -94,6 +95,8 @@ public ProtoTruthMessageDifferencer load(Descriptor descriptor) {
9495

9596
abstract FieldScopeLogic fieldScopeLogic();
9697

98+
abstract boolean reportMismatchesOnly();
99+
97100
// For pretty-printing, does not affect behavior.
98101
abstract Function<? super Optional<Descriptor>, String> usingCorrespondenceStringFunction();
99102

@@ -119,10 +122,10 @@ final FluentEqualityConfig ignoringRepeatedFieldOrder() {
119122
.build();
120123
}
121124

122-
final FluentEqualityConfig reportingMismatchesOnly() {
125+
final FluentEqualityConfig ignoringExtraRepeatedFieldElements() {
123126
return toBuilder()
124-
.setReportMismatchesOnly(true)
125-
.addUsingCorrespondenceString(".reportingMismatchesOnly()")
127+
.setIgnoreExtraRepeatedFieldElements(true)
128+
.addUsingCorrespondenceString(".ignoringExtraRepeatedFieldElements()")
126129
.build();
127130
}
128131

@@ -192,6 +195,13 @@ final FluentEqualityConfig ignoringFieldScope(FieldScope fieldScope) {
192195
.build();
193196
}
194197

198+
final FluentEqualityConfig reportingMismatchesOnly() {
199+
return toBuilder()
200+
.setReportMismatchesOnly(true)
201+
.addUsingCorrespondenceString(".reportingMismatchesOnly()")
202+
.build();
203+
}
204+
195205
//////////////////////////////////////////////////////////////////////////////////////////////////
196206
// Converters into comparison utilities.
197207
//////////////////////////////////////////////////////////////////////////////////////////////////
@@ -246,7 +256,7 @@ abstract static class Builder {
246256

247257
abstract Builder setIgnoreRepeatedFieldOrder(boolean ignoringRepeatedFieldOrder);
248258

249-
abstract Builder setReportMismatchesOnly(boolean reportMismatchesOnly);
259+
abstract Builder setIgnoreExtraRepeatedFieldElements(boolean ignoreExtraRepeatedFieldElements);
250260

251261
abstract Builder setDoubleCorrespondence(Correspondence<Number, Number> doubleCorrespondence);
252262

@@ -258,6 +268,8 @@ abstract static class Builder {
258268

259269
abstract Builder setFieldScopeLogic(FieldScopeLogic fieldScopeLogic);
260270

271+
abstract Builder setReportMismatchesOnly(boolean reportMismatchesOnly);
272+
261273
@CheckReturnValue
262274
abstract Function<? super Optional<Descriptor>, String> usingCorrespondenceStringFunction();
263275

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/IterableOfProtosFluentAssertion.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,21 @@ public interface IterableOfProtosFluentAssertion<M extends Message>
112112
*/
113113
IterableOfProtosFluentAssertion<M> ignoringRepeatedFieldOrder();
114114

115+
/**
116+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
117+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
118+
* proto which are empty. To ignore empty repeated fields as well, use {@link
119+
* #comparingExpectedFieldsOnly}.
120+
*
121+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrder}. If ignoring
122+
* repeated field order AND extra repeated field elements, all that is tested is that the expected
123+
* elements comprise a subset of the actual elements. If not ignoring repeated field order, but
124+
* still ignoring extra repeated field elements, the actual elements must contain a subsequence
125+
* that matches the expected elements for the test to pass. (The subsequence rule does not apply
126+
* to Map fields, which are always compared by key.)
127+
*/
128+
IterableOfProtosFluentAssertion<M> ignoringExtraRepeatedFieldElements();
129+
115130
/**
116131
* Compares double fields as equal if they are both finite and their absolute difference is less
117132
* than or equal to {@code tolerance}.

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/IterableOfProtosSubject.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,23 @@ public IterableOfProtosFluentAssertion<M> ignoringRepeatedFieldOrder() {
492492
return usingConfig(config.ignoringRepeatedFieldOrder());
493493
}
494494

495+
/**
496+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
497+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
498+
* proto which are empty. To ignore empty repeated fields as well, use {@link
499+
* #comparingExpectedFieldsOnly}.
500+
*
501+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrder}. If ignoring
502+
* repeated field order AND extra repeated field elements, all that is tested is that the expected
503+
* elements comprise a subset of the actual elements. If not ignoring repeated field order, but
504+
* still ignoring extra repeated field elements, the actual elements must contain a subsequence
505+
* that matches the expected elements for the test to pass. (The subsequence rule does not apply
506+
* to Map fields, which are always compared by key.)
507+
*/
508+
public IterableOfProtosFluentAssertion<M> ignoringExtraRepeatedFieldElements() {
509+
return usingConfig(config.ignoringExtraRepeatedFieldElements());
510+
}
511+
495512
/**
496513
* Compares double fields as equal if they are both finite and their absolute difference is less
497514
* than or equal to {@code tolerance}.
@@ -774,6 +791,11 @@ public IterableOfProtosFluentAssertion<M> ignoringRepeatedFieldOrder() {
774791
return subject.ignoringRepeatedFieldOrder();
775792
}
776793

794+
@Override
795+
public IterableOfProtosFluentAssertion<M> ignoringExtraRepeatedFieldElements() {
796+
return subject.ignoringExtraRepeatedFieldElements();
797+
}
798+
777799
@Override
778800
public IterableOfProtosFluentAssertion<M> usingDoubleTolerance(double tolerance) {
779801
return subject.usingDoubleTolerance(tolerance);

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/MapWithProtoValuesFluentAssertion.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ public interface MapWithProtoValuesFluentAssertion<M extends Message> {
121121
*/
122122
MapWithProtoValuesFluentAssertion<M> ignoringRepeatedFieldOrderForValues();
123123

124+
/**
125+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
126+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
127+
* proto which are empty. To ignore empty repeated fields as well, use {@link
128+
* #comparingExpectedFieldsOnlyForValues}.
129+
*
130+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrderForValues}. If
131+
* ignoring repeated field order AND extra repeated field elements, all that is tested is that the
132+
* expected elements comprise a subset of the actual elements. If not ignoring repeated field
133+
* order, but still ignoring extra repeated field elements, the actual elements must contain a
134+
* subsequence that matches the expected elements for the test to pass. (The subsequence rule does
135+
* not apply to Map fields, which are always compared by key.)
136+
*/
137+
MapWithProtoValuesFluentAssertion<M> ignoringExtraRepeatedFieldElementsForValues();
138+
124139
/**
125140
* Compares double fields as equal if they are both finite and their absolute difference is less
126141
* than or equal to {@code tolerance}.

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/MapWithProtoValuesSubject.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,23 @@ public MapWithProtoValuesFluentAssertion<M> ignoringRepeatedFieldOrderForValues(
301301
return usingConfig(config.ignoringRepeatedFieldOrder());
302302
}
303303

304+
/**
305+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
306+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
307+
* proto which are empty. To ignore empty repeated fields as well, use {@link
308+
* #comparingExpectedFieldsOnlyForValues}.
309+
*
310+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrderForValues}. If
311+
* ignoring repeated field order AND extra repeated field elements, all that is tested is that the
312+
* expected elements comprise a subset of the actual elements. If not ignoring repeated field
313+
* order, but still ignoring extra repeated field elements, the actual elements must contain a
314+
* subsequence that matches the expected elements for the test to pass. (The subsequence rule does
315+
* not apply to Map fields, which are always compared by key.)
316+
*/
317+
public MapWithProtoValuesFluentAssertion<M> ignoringExtraRepeatedFieldElementsForValues() {
318+
return usingConfig(config.ignoringExtraRepeatedFieldElements());
319+
}
320+
304321
/**
305322
* Compares double fields as equal if they are both finite and their absolute difference is less
306323
* than or equal to {@code tolerance}.
@@ -475,6 +492,11 @@ public MapWithProtoValuesFluentAssertion<M> ignoringRepeatedFieldOrderForValues(
475492
return subject.ignoringRepeatedFieldOrderForValues();
476493
}
477494

495+
@Override
496+
public MapWithProtoValuesFluentAssertion<M> ignoringExtraRepeatedFieldElementsForValues() {
497+
return subject.ignoringExtraRepeatedFieldElementsForValues();
498+
}
499+
478500
@Override
479501
public MapWithProtoValuesFluentAssertion<M> usingDoubleToleranceForValues(double tolerance) {
480502
return subject.usingDoubleToleranceForValues(tolerance);

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/MultimapWithProtoValuesFluentAssertion.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,21 @@ public interface MultimapWithProtoValuesFluentAssertion<M extends Message> {
121121
*/
122122
MultimapWithProtoValuesFluentAssertion<M> ignoringRepeatedFieldOrderForValues();
123123

124+
/**
125+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
126+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
127+
* proto which are empty. To ignore empty repeated fields as well, use {@link
128+
* #comparingExpectedFieldsOnlyForValues}.
129+
*
130+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrderForValues}. If
131+
* ignoring repeated field order AND extra repeated field elements, all that is tested is that the
132+
* expected elements comprise a subset of the actual elements. If not ignoring repeated field
133+
* order, but still ignoring extra repeated field elements, the actual elements must contain a
134+
* subsequence that matches the expected elements for the test to pass. (The subsequence rule does
135+
* not apply to Map fields, which are always compared by key.)
136+
*/
137+
MultimapWithProtoValuesFluentAssertion<M> ignoringExtraRepeatedFieldElementsForValues();
138+
124139
/**
125140
* Compares double fields as equal if they are both finite and their absolute difference is less
126141
* than or equal to {@code tolerance}.

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/MultimapWithProtoValuesSubject.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ public MultimapWithProtoValuesFluentAssertion<M> ignoringRepeatedFieldOrderForVa
345345
return usingConfig(config.ignoringRepeatedFieldOrder());
346346
}
347347

348+
/**
349+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
350+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
351+
* proto which are empty. To ignore empty repeated fields as well, use {@link
352+
* #comparingExpectedFieldsOnlyForValues}.
353+
*
354+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrderForValues}. If
355+
* ignoring repeated field order AND extra repeated field elements, all that is tested is that the
356+
* expected elements comprise a subset of the actual elements. If not ignoring repeated field
357+
* order, but still ignoring extra repeated field elements, the actual elements must contain a
358+
* subsequence that matches the expected elements for the test to pass. (The subsequence rule does
359+
* not apply to Map fields, which are always compared by key.)
360+
*/
361+
public MultimapWithProtoValuesFluentAssertion<M> ignoringExtraRepeatedFieldElementsForValues() {
362+
return usingConfig(config.ignoringExtraRepeatedFieldElements());
363+
}
364+
348365
/**
349366
* Compares double fields as equal if they are both finite and their absolute difference is less
350367
* than or equal to {@code tolerance}.
@@ -521,6 +538,11 @@ public MultimapWithProtoValuesFluentAssertion<M> ignoringRepeatedFieldOrderForVa
521538
return subject.ignoringRepeatedFieldOrderForValues();
522539
}
523540

541+
@Override
542+
public MultimapWithProtoValuesFluentAssertion<M> ignoringExtraRepeatedFieldElementsForValues() {
543+
return subject.ignoringExtraRepeatedFieldElementsForValues();
544+
}
545+
524546
@Override
525547
public MultimapWithProtoValuesFluentAssertion<M> usingDoubleToleranceForValues(
526548
double tolerance) {

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/ProtoFluentAssertion.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ public interface ProtoFluentAssertion {
110110
*/
111111
ProtoFluentAssertion ignoringRepeatedFieldOrder();
112112

113+
/**
114+
* Specifies that, for all repeated and map fields, any elements in the 'actual' proto which are
115+
* not found in the 'expected' proto are ignored, with the exception of fields in the expected
116+
* proto which are empty. To ignore empty repeated fields as well, use {@link
117+
* #comparingExpectedFieldsOnly}.
118+
*
119+
* <p>This rule is applied independently from {@link #ignoringRepeatedFieldOrder}. If ignoring
120+
* repeated field order AND extra repeated field elements, all that is tested is that the expected
121+
* elements comprise a subset of the actual elements. If not ignoring repeated field order, but
122+
* still ignoring extra repeated field elements, the actual elements must contain a subsequence
123+
* that matches the expected elements for the test to pass. (The subsequence rule does not apply
124+
* to Map fields, which are always compared by key.)
125+
*/
126+
ProtoFluentAssertion ignoringExtraRepeatedFieldElements();
127+
113128
/**
114129
* Compares double fields as equal if they are both finite and their absolute difference is less
115130
* than or equal to {@code tolerance}.

extensions/proto/src/main/java/com/google/common/truth/extensions/proto/ProtoSubject.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ public ProtoFluentAssertion ignoringRepeatedFieldOrder() {
8383
return usingConfig(config.ignoringRepeatedFieldOrder());
8484
}
8585

86+
@Override
87+
public ProtoFluentAssertion ignoringExtraRepeatedFieldElements() {
88+
return usingConfig(config.ignoringExtraRepeatedFieldElements());
89+
}
90+
8691
@Override
8792
public ProtoFluentAssertion usingDoubleTolerance(double tolerance) {
8893
return usingConfig(config.usingDoubleTolerance(tolerance));

0 commit comments

Comments
 (0)