From 7bed5b79b2e70f5876d893cea477e52ec0824a42 Mon Sep 17 00:00:00 2001 From: Google Java Core Libraries Date: Mon, 30 Sep 2024 06:40:04 -0700 Subject: [PATCH] Test that `comparingExpectedFieldsOnly` is effective on nested `Any` values. PiperOrigin-RevId: 680553503 --- .../extensions/proto/ProtoSubjectTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/extensions/proto/src/test/java/com/google/common/truth/extensions/proto/ProtoSubjectTest.java b/extensions/proto/src/test/java/com/google/common/truth/extensions/proto/ProtoSubjectTest.java index c9eb636b4..1d7543c8c 100644 --- a/extensions/proto/src/test/java/com/google/common/truth/extensions/proto/ProtoSubjectTest.java +++ b/extensions/proto/src/test/java/com/google/common/truth/extensions/proto/ProtoSubjectTest.java @@ -550,11 +550,33 @@ public void testComparingExpectedFieldsOnly() { expectFailureWhenTesting() .that(message) + .unpackingAnyUsing(getTypeRegistry(), getExtensionRegistry()) .comparingExpectedFieldsOnly() .isNotEqualTo(narrowMessage); expectThatFailure().hasMessageThat().contains("ignored: r_string"); } + @Test + public void testComparingExpectedFieldsOnly_anyMessage() { + String typeUrl = + isProto3() + ? "type.googleapis.com/com.google.common.truth.extensions.proto.SubTestMessage3" + : "type.googleapis.com/com.google.common.truth.extensions.proto.SubTestMessage2"; + + Message message = parse("o_any_message: { [" + typeUrl + "]: {r_string: \"foo\"} }"); + Message diffMessage = + parse("o_any_message: { [" + typeUrl + "]: {o_int: 3 r_string: \"foo\"} }"); + + expectThat(diffMessage).comparingExpectedFieldsOnly().isEqualTo(message); + + expectFailureWhenTesting().that(message).comparingExpectedFieldsOnly().isEqualTo(diffMessage); + if (isProto3()) { + expectThatFailure().hasMessageThat().contains("modified: o_any_message.value.o_int: 3 -> 0"); + } else { + expectThatFailure().hasMessageThat().contains("deleted: o_any_message.value.o_int: 3"); + } + } + @Test public void testIgnoringExtraRepeatedFieldElements_respectingOrder() { Message message = parse("r_string: 'foo' r_string: 'bar'");