Class FieldScopes


  • public final class FieldScopes
    extends Object
    Factory class for FieldScope instances.
    • Method Detail

      • fromSetFields

        public static FieldScope fromSetFields​(Message message)
        Returns a FieldScope which is constrained to precisely those specific field paths that are explicitly set in the message. Note that, for version 3 protobufs, such a FieldScope will omit fields in the provided message which are set to default values.

        This can be used limit the scope of a comparison to a complex set of fields in a very brief statement. Often, message is the expected half of a comparison about to be performed.

        Example usage:

        
         Foo actual = Foo.newBuilder().setBar(3).setBaz(4).build();
         Foo expected = Foo.newBuilder().setBar(3).setBaz(5).build();
         // Fails, because actual.getBaz() != expected.getBaz().
         assertThat(actual).isEqualTo(expected);
        
         Foo scope = Foo.newBuilder().setBar(2).build();
         // Succeeds, because only the field 'bar' is compared.
         assertThat(actual).withPartialScope(FieldScopes.fromSetFields(scope)).isEqualTo(expected);
        
         

        The returned FieldScope does not respect repeated field indices nor map keys. For example, if the provided message sets different field values for different elements of a repeated field, like so:

        
         sub_message: {
           foo: "foo"
         }
         sub_message: {
           bar: "bar"
         }
         

        The FieldScope will contain sub_message.foo and sub_message.bar for *all* repeated sub_messages, including those beyond index 1.

      • fromSetFields

        public static FieldScope fromSetFields​(Message message,
                                               TypeRegistry typeRegistry,
                                               ExtensionRegistry extensionRegistry)
        Returns a FieldScope which is constrained to precisely those specific field paths that are explicitly set in the message. Note that, for version 3 protobufs, such a FieldScope will omit fields in the provided message which are set to default values.

        This can be used limit the scope of a comparison to a complex set of fields in a very brief statement. Often, message is the expected half of a comparison about to be performed.

        Example usage:

        
         Foo actual = Foo.newBuilder().setBar(3).setBaz(4).build();
         Foo expected = Foo.newBuilder().setBar(3).setBaz(5).build();
         // Fails, because actual.getBaz() != expected.getBaz().
         assertThat(actual).isEqualTo(expected);
        
         Foo scope = Foo.newBuilder().setBar(2).build();
         // Succeeds, because only the field 'bar' is compared.
         assertThat(actual).withPartialScope(FieldScopes.fromSetFields(scope)).isEqualTo(expected);
        
         

        The returned FieldScope does not respect repeated field indices nor map keys. For example, if the provided message sets different field values for different elements of a repeated field, like so:

        
         sub_message: {
           foo: "foo"
         }
         sub_message: {
           bar: "bar"
         }
         

        The FieldScope will contain sub_message.foo and sub_message.bar for *all* repeated sub_messages, including those beyond index 1.

        If there are google.protobuf.Any protos anywhere within these messages, they will be unpacked using the provided TypeRegistry and ExtensionRegistry to determine which fields within them should be compared.

        Since:
        1.2
        See Also:
        ProtoFluentAssertion.unpackingAnyUsing(com.google.protobuf.TypeRegistry, com.google.protobuf.ExtensionRegistry)
      • fromSetFields

        public static FieldScope fromSetFields​(Message firstMessage,
                                               Message secondMessage,
                                               Message... rest)
        Creates a FieldScope covering the fields set in every message in the provided list of messages, with the same semantics as in fromSetFields(Message).

        This can be thought of as the union of the FieldScopes for each individual message, or the FieldScope for the merge of all the messages. These are equivalent.

      • fromSetFields

        public static FieldScope fromSetFields​(Iterable<? extends Message> messages)
        Creates a FieldScope covering the fields set in every message in the provided list of messages, with the same semantics as in fromSetFields(Message).

        This can be thought of as the union of the FieldScopes for each individual message, or the FieldScope for the merge of all the messages. These are equivalent.

      • ignoringFields

        public static FieldScope ignoringFields​(int firstFieldNumber,
                                                int... rest)
        Returns a FieldScope which matches everything except the provided field numbers for the top level message type.

        The field numbers are ignored recursively on this type. That is, if YourMessage contains another YourMessage somewhere within its subtree, field number X will be ignored for all submessages of type YourMessage, as well as for the top-level message.

        See Also:
        FieldScope.ignoringFields(int, int...)
      • ignoringFields

        public static FieldScope ignoringFields​(Iterable<Integer> fieldNumbers)
        Returns a FieldScope which matches everything except the provided field numbers for the top level message type.

        The field numbers are ignored recursively on this type. That is, if YourMessage contains another YourMessage somewhere within its subtree, field number X will be ignored for all submessages of type YourMessage, as well as for the top-level message.

        See Also:
        FieldScope.ignoringFields(Iterable)
      • all

        public static FieldScope all()
        Returns a FieldScope which matches all fields without exception. Generally not needed, since the other factory functions will build on top of this for you.
      • none

        public static FieldScope none()
        Returns a FieldScope which matches no fields. A comparison made using this scope alone will always trivially pass. Generally not needed, since the other factory functions will build on top of this for you.