Class FieldScopes

java.lang.Object
com.google.common.truth.extensions.proto.FieldScopes

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

    • 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:
    • 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.

    • fromSetFields

      public static FieldScope fromSetFields(Iterable<? extends Message> messages, TypeRegistry typeRegistry, ExtensionRegistry extensionRegistry)
      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.

      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:
    • 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:
    • 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:
    • ignoringFieldDescriptors

      public static FieldScope ignoringFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)
      Returns a FieldScope which matches everything except the provided field descriptors for the message.
      See Also:
    • ignoringFieldDescriptors

      public static FieldScope ignoringFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
      Returns a FieldScope which matches everything except the provided field descriptors for the message.
      See Also:
    • allowingFields

      public static FieldScope allowingFields(int firstFieldNumber, int... rest)
      Returns a FieldScope which matches nothing except the provided field numbers for the top level message type.
      See Also:
    • allowingFields

      public static FieldScope allowingFields(Iterable<Integer> fieldNumbers)
      Returns a FieldScope which matches nothing except the provided field numbers for the top level message type.
      See Also:
    • allowingFieldDescriptors

      public static FieldScope allowingFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)
      Returns a FieldScope which matches nothing except the provided field descriptors for the message.
      See Also:
    • allowingFieldDescriptors

      public static FieldScope allowingFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
      Returns a FieldScope which matches nothing except the provided field descriptors for the message.
      See Also:
    • 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.