Class FieldScopes
- java.lang.Object
-
- com.google.common.truth.extensions.proto.FieldScopes
-
public final class FieldScopes extends Object
Factory class forFieldScopeinstances.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FieldScopeall()Returns aFieldScopewhich matches all fields without exception.static FieldScopeallowingFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)Returns aFieldScopewhich matches nothing except the provided field descriptors for the message.static FieldScopeallowingFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)Returns aFieldScopewhich matches nothing except the provided field descriptors for the message.static FieldScopeallowingFields(int firstFieldNumber, int... rest)Returns aFieldScopewhich matches nothing except the provided field numbers for the top level message type.static FieldScopeallowingFields(Iterable<Integer> fieldNumbers)Returns aFieldScopewhich matches nothing except the provided field numbers for the top level message type.static FieldScopefromSetFields(Message message)Returns aFieldScopewhich is constrained to precisely those specific field paths that are explicitly set in the message.static FieldScopefromSetFields(Message firstMessage, Message secondMessage, Message... rest)Creates aFieldScopecovering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message).static FieldScopefromSetFields(Iterable<? extends Message> messages)Creates aFieldScopecovering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message).static FieldScopeignoringFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)Returns aFieldScopewhich matches everything except the provided field descriptors for the message.static FieldScopeignoringFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)Returns aFieldScopewhich matches everything except the provided field descriptors for the message.static FieldScopeignoringFields(int firstFieldNumber, int... rest)Returns aFieldScopewhich matches everything except the provided field numbers for the top level message type.static FieldScopeignoringFields(Iterable<Integer> fieldNumbers)Returns aFieldScopewhich matches everything except the provided field numbers for the top level message type.static FieldScopenone()Returns aFieldScopewhich matches no fields.
-
-
-
Method Detail
-
fromSetFields
public static FieldScope fromSetFields(Message message)
Returns aFieldScopewhich is constrained to precisely those specific field paths that are explicitly set in the message. Note that, for version 3 protobufs, such aFieldScopewill 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,
messageis 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
FieldScopedoes 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
FieldScopewill containsub_message.fooandsub_message.barfor *all* repeatedsub_messages, including those beyond index 1.
-
fromSetFields
public static FieldScope fromSetFields(Message firstMessage, Message secondMessage, Message... rest)
Creates aFieldScopecovering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message).This can be thought of as the union of the
FieldScopes for each individual message, or theFieldScopefor the merge of all the messages. These are equivalent.
-
fromSetFields
public static FieldScope fromSetFields(Iterable<? extends Message> messages)
Creates aFieldScopecovering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message).This can be thought of as the union of the
FieldScopes for each individual message, or theFieldScopefor the merge of all the messages. These are equivalent.
-
ignoringFields
public static FieldScope ignoringFields(int firstFieldNumber, int... rest)
Returns aFieldScopewhich 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
YourMessagecontains anotherYourMessagesomewhere within its subtree, field numberXwill be ignored for all submessages of typeYourMessage, as well as for the top-level message.- See Also:
FieldScope.ignoringFields(int, int...)
-
ignoringFields
public static FieldScope ignoringFields(Iterable<Integer> fieldNumbers)
Returns aFieldScopewhich 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
YourMessagecontains anotherYourMessagesomewhere within its subtree, field numberXwill be ignored for all submessages of typeYourMessage, as well as for the top-level message.- See Also:
FieldScope.ignoringFields(Iterable)
-
ignoringFieldDescriptors
public static FieldScope ignoringFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)
Returns aFieldScopewhich matches everything except the provided field descriptors for the message.
-
ignoringFieldDescriptors
public static FieldScope ignoringFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
Returns aFieldScopewhich matches everything except the provided field descriptors for the message.
-
allowingFields
public static FieldScope allowingFields(int firstFieldNumber, int... rest)
Returns aFieldScopewhich matches nothing except the provided field numbers for the top level message type.- See Also:
FieldScope.allowingFields(int, int...)
-
allowingFields
public static FieldScope allowingFields(Iterable<Integer> fieldNumbers)
Returns aFieldScopewhich matches nothing except the provided field numbers for the top level message type.- See Also:
FieldScope.allowingFields(Iterable)
-
allowingFieldDescriptors
public static FieldScope allowingFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)
Returns aFieldScopewhich matches nothing except the provided field descriptors for the message.
-
allowingFieldDescriptors
public static FieldScope allowingFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
Returns aFieldScopewhich matches nothing except the provided field descriptors for the message.
-
all
public static FieldScope all()
Returns aFieldScopewhich 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 aFieldScopewhich 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.
-
-