Class FieldScopes
- java.lang.Object
-
- com.google.common.truth.extensions.proto.FieldScopes
-
public final class FieldScopes extends Object
Factory class forFieldScope
instances.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FieldScope
all()
Returns aFieldScope
which matches all fields without exception.static FieldScope
allowingFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)
Returns aFieldScope
which matches nothing except the provided field descriptors for the message.static FieldScope
allowingFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
Returns aFieldScope
which matches nothing except the provided field descriptors for the message.static FieldScope
allowingFields(int firstFieldNumber, int... rest)
Returns aFieldScope
which matches nothing except the provided field numbers for the top level message type.static FieldScope
allowingFields(Iterable<Integer> fieldNumbers)
Returns aFieldScope
which matches nothing except the provided field numbers for the top level message type.static FieldScope
fromSetFields(Message message)
Returns aFieldScope
which is constrained to precisely those specific field paths that are explicitly set in the message.static FieldScope
fromSetFields(Message firstMessage, Message secondMessage, Message... rest)
Creates aFieldScope
covering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message)
.static FieldScope
fromSetFields(Message message, TypeRegistry typeRegistry, ExtensionRegistry extensionRegistry)
Returns aFieldScope
which is constrained to precisely those specific field paths that are explicitly set in the message.static FieldScope
fromSetFields(Iterable<? extends Message> messages)
Creates aFieldScope
covering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message)
.static FieldScope
fromSetFields(Iterable<? extends Message> messages, TypeRegistry typeRegistry, ExtensionRegistry extensionRegistry)
Creates aFieldScope
covering the fields set in every message in the provided list of messages, with the same semantics as infromSetFields(Message)
.static FieldScope
ignoringFieldDescriptors(Descriptors.FieldDescriptor firstFieldDescriptor, Descriptors.FieldDescriptor... rest)
Returns aFieldScope
which matches everything except the provided field descriptors for the message.static FieldScope
ignoringFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
Returns aFieldScope
which matches everything except the provided field descriptors for the message.static FieldScope
ignoringFields(int firstFieldNumber, int... rest)
Returns aFieldScope
which matches everything except the provided field numbers for the top level message type.static FieldScope
ignoringFields(Iterable<Integer> fieldNumbers)
Returns aFieldScope
which matches everything except the provided field numbers for the top level message type.static FieldScope
none()
Returns aFieldScope
which matches no fields.
-
-
-
Method Detail
-
fromSetFields
public static FieldScope fromSetFields(Message message)
Returns aFieldScope
which is constrained to precisely those specific field paths that are explicitly set in the message. Note that, for version 3 protobufs, such aFieldScope
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 containsub_message.foo
andsub_message.bar
for *all* repeatedsub_messages
, including those beyond index 1.
-
fromSetFields
public static FieldScope fromSetFields(Message message, TypeRegistry typeRegistry, ExtensionRegistry extensionRegistry)
Returns aFieldScope
which is constrained to precisely those specific field paths that are explicitly set in the message. Note that, for version 3 protobufs, such aFieldScope
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 containsub_message.foo
andsub_message.bar
for *all* repeatedsub_messages
, including those beyond index 1.If there are
google.protobuf.Any
protos anywhere within these messages, they will be unpacked using the providedTypeRegistry
andExtensionRegistry
to determine which fields within them should be compared.
-
fromSetFields
public static FieldScope fromSetFields(Message firstMessage, Message secondMessage, Message... rest)
Creates aFieldScope
covering 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
FieldScope
s for each individual message, or theFieldScope
for the merge of all the messages. These are equivalent.
-
fromSetFields
public static FieldScope fromSetFields(Iterable<? extends Message> messages)
Creates aFieldScope
covering 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
FieldScope
s for each individual message, or theFieldScope
for the merge of all the messages. These are equivalent.
-
fromSetFields
public static FieldScope fromSetFields(Iterable<? extends Message> messages, TypeRegistry typeRegistry, ExtensionRegistry extensionRegistry)
Creates aFieldScope
covering 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
FieldScope
s for each individual message, or theFieldScope
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 providedTypeRegistry
andExtensionRegistry
to determine which fields within them should be compared.
-
ignoringFields
public static FieldScope ignoringFields(int firstFieldNumber, int... rest)
Returns aFieldScope
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 anotherYourMessage
somewhere within its subtree, field numberX
will 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 aFieldScope
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 anotherYourMessage
somewhere within its subtree, field numberX
will 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 aFieldScope
which matches everything except the provided field descriptors for the message.
-
ignoringFieldDescriptors
public static FieldScope ignoringFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
Returns aFieldScope
which matches everything except the provided field descriptors for the message.
-
allowingFields
public static FieldScope allowingFields(int firstFieldNumber, int... rest)
Returns aFieldScope
which 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 aFieldScope
which 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 aFieldScope
which matches nothing except the provided field descriptors for the message.
-
allowingFieldDescriptors
public static FieldScope allowingFieldDescriptors(Iterable<Descriptors.FieldDescriptor> fieldDescriptors)
Returns aFieldScope
which matches nothing except the provided field descriptors for the message.
-
all
public static FieldScope all()
Returns aFieldScope
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 aFieldScope
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.
-
-