public static FieldScope fromSetFields(Message message) {
    return fromSetFields(
        message, AnyUtils.defaultTypeRegistry(), AnyUtils.defaultExtensionRegistry());
  }
@Test
  public void testFromSetFields_unknownFields() throws InvalidProtocolBufferException {
    // Make sure that merging of repeated fields, separation by tag number, and separation by
    // unknown field type all work.
    Message scopeMessage =
        fromUnknownFields(
            UnknownFieldSet.newBuilder()
                .addField(333, Field.newBuilder().addFixed32(1).addFixed64(1).build())
                .addField(
                    444,
                    Field.newBuilder()
                        .addVarint(1)
                        .addLengthDelimited(ByteString.copyFrom("1", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(1).build())
                                .build())
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(2, Field.newBuilder().addFixed64(1).build())
                                .build())
                        .build())
                .build());

    // 1 = compared, [2, 3] = ignored, 4 = compared and fails
    Message message =
        fromUnknownFields(
            UnknownFieldSet.newBuilder()
                .addField(222, Field.newBuilder().addFixed32(2).addFixed64(2).build())
                .addField(
                    333,
                    Field.newBuilder()
                        .addFixed32(1)
                        .addFixed64(1)
                        .addVarint(2)
                        .addLengthDelimited(ByteString.copyFrom("2", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(2).build())
                                .build())
                        .build())
                .addField(
                    444,
                    Field.newBuilder()
                        .addFixed32(2)
                        .addFixed64(2)
                        .addVarint(1)
                        .addLengthDelimited(ByteString.copyFrom("1", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(1).addFixed64(2).build())
                                .addField(2, Field.newBuilder().addFixed32(2).addFixed64(1).build())
                                .addField(3, Field.newBuilder().addFixed32(2).build())
                                .build())
                        .build())
                .build());
    Message diffMessage =
        fromUnknownFields(
            UnknownFieldSet.newBuilder()
                .addField(222, Field.newBuilder().addFixed32(3).addFixed64(3).build())
                .addField(
                    333,
                    Field.newBuilder()
                        .addFixed32(4)
                        .addFixed64(4)
                        .addVarint(3)
                        .addLengthDelimited(ByteString.copyFrom("3", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(3).build())
                                .build())
                        .build())
                .addField(
                    444,
                    Field.newBuilder()
                        .addFixed32(3)
                        .addFixed64(3)
                        .addVarint(4)
                        .addLengthDelimited(ByteString.copyFrom("4", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(4).addFixed64(3).build())
                                .addField(2, Field.newBuilder().addFixed32(3).addFixed64(4).build())
                                .addField(3, Field.newBuilder().addFixed32(3).build())
                                .build())
                        .build())
                .build());
    Message eqMessage =
        fromUnknownFields(
            UnknownFieldSet.newBuilder()
                .addField(222, Field.newBuilder().addFixed32(3).addFixed64(3).build())
                .addField(
                    333,
                    Field.newBuilder()
                        .addFixed32(1)
                        .addFixed64(1)
                        .addVarint(3)
                        .addLengthDelimited(ByteString.copyFrom("3", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(3).build())
                                .build())
                        .build())
                .addField(
                    444,
                    Field.newBuilder()
                        .addFixed32(3)
                        .addFixed64(3)
                        .addVarint(1)
                        .addLengthDelimited(ByteString.copyFrom("1", UTF_8))
                        .addGroup(
                            UnknownFieldSet.newBuilder()
                                .addField(1, Field.newBuilder().addFixed32(1).addFixed64(3).build())
                                .addField(2, Field.newBuilder().addFixed32(3).addFixed64(1).build())
                                .addField(3, Field.newBuilder().addFixed32(3).build())
                                .build())
                        .build())
                .build());

    expectThat(diffMessage).isNotEqualTo(message);
    expectThat(eqMessage).isNotEqualTo(message);

    expectThat(diffMessage)
        .withPartialScope(FieldScopes.fromSetFields(scopeMessage))
        .isNotEqualTo(message);
    expectThat(eqMessage)
        .withPartialScope(FieldScopes.fromSetFields(scopeMessage))
        .isEqualTo(message);

    expectFailureWhenTesting().that(diffMessage).isEqualTo(message);
    expectIsEqualToFailed();
    expectThatFailure().hasMessageThat().contains("1 -> 4");
    expectThatFailure().hasMessageThat().contains("\"1\" -> \"4\"");
    expectThatFailure().hasMessageThat().contains("2 -> 3");
    expectThatFailure().hasMessageThat().contains("\"2\" -> \"3\"");

    expectFailureWhenTesting()
        .that(diffMessage)
        .withPartialScope(FieldScopes.fromSetFields(scopeMessage))
        .isEqualTo(message);
    expectIsEqualToFailed();
    expectThatFailure().hasMessageThat().contains("1 -> 4");
    expectThatFailure().hasMessageThat().contains("\"1\" -> \"4\"");
    expectThatFailure().hasMessageThat().doesNotContain("2 -> 3");
    expectThatFailure().hasMessageThat().doesNotContain("\"2\" -> \"3\"");

    expectFailureWhenTesting()
        .that(eqMessage)
        .withPartialScope(FieldScopes.fromSetFields(scopeMessage))
        .isNotEqualTo(message);
    expectIsNotEqualToFailed();
    expectThatFailure().hasMessageThat().doesNotContain("2 -> 3");
    expectThatFailure().hasMessageThat().doesNotContain("\"2\" -> \"3\"");
  }