Skip to content

Commit

Permalink
Implement IterableProtoSubject.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137572300
  • Loading branch information
torquestomp authored and cpovirk committed Nov 1, 2016
1 parent 21ebb7b commit a94b128
Show file tree
Hide file tree
Showing 15 changed files with 1,902 additions and 63 deletions.
Expand Up @@ -23,9 +23,11 @@
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Functions; import com.google.common.base.Functions;
import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Message; import com.google.protobuf.Message;
import java.util.List;


/** /**
* Implementation of a {@link FieldScope}. It takes a logic component {@link FieldScopeLogic}, and * Implementation of a {@link FieldScope}. It takes a logic component {@link FieldScopeLogic}, and
Expand Down Expand Up @@ -75,7 +77,7 @@ static FieldScope createFromSetFields(Iterable<? extends Message> messages) {
checkArgument( checkArgument(
optDescriptor.isPresent(), optDescriptor.isPresent(),
"Cannot create scope from messages with different descriptors: %s", "Cannot create scope from messages with different descriptors: %s",
messages); getDescriptors(messages));


Message.Builder builder = null; Message.Builder builder = null;
for (Message message : messages) { for (Message message : messages) {
Expand All @@ -90,7 +92,7 @@ static FieldScope createFromSetFields(Iterable<? extends Message> messages) {


return create( return create(
FieldScopeLogic.partialScope(builder.build()), FieldScopeLogic.partialScope(builder.build()),
Functions.constant(String.format("FieldScopes.fromSetFields(%s)", messages.toString()))); Functions.constant(String.format("FieldScopes.fromSetFields(%s)", formatList(messages))));
} }


static FieldScope createIgnoringFields(int... fieldNumbers) { static FieldScope createIgnoringFields(int... fieldNumbers) {
Expand Down Expand Up @@ -180,12 +182,6 @@ public final FieldScope allowingFieldDescriptors(FieldDescriptor... fieldDescrip
".allowingFieldDescriptors(%s)", fieldDescriptors)); ".allowingFieldDescriptors(%s)", fieldDescriptors));
} }


private Function<Optional<Descriptor>, String> addUsingCorrespondenceString(
String fmt, Object... args) {
return FieldScopeUtil.concat(
usingCorrespondenceStringFunction(), Functions.constant(String.format(fmt, args)));
}

private Function<Optional<Descriptor>, String> addUsingCorrespondenceFieldNumbersString( private Function<Optional<Descriptor>, String> addUsingCorrespondenceFieldNumbersString(
String fmt, int... fieldNumbers) { String fmt, int... fieldNumbers) {
return FieldScopeUtil.concat( return FieldScopeUtil.concat(
Expand All @@ -199,4 +195,20 @@ private Function<Optional<Descriptor>, String> addUsingCorrespondenceFieldDescri
usingCorrespondenceStringFunction(), usingCorrespondenceStringFunction(),
Functions.constant(String.format(fmt, join(fieldDescriptors)))); Functions.constant(String.format(fmt, join(fieldDescriptors))));
} }

private static Iterable<String> getDescriptors(Iterable<? extends Message> messages) {
List<String> descriptors = Lists.newArrayList();
for (Message message : messages) {
descriptors.add(message == null ? "null" : message.getDescriptorForType().getFullName());
}
return descriptors;
}

private static String formatList(Iterable<? extends Message> messages) {
List<String> strings = Lists.newArrayList();
for (Message message : messages) {
strings.add(message == null ? "null" : "{" + message + "}");
}
return "[" + join(strings) + "]";
}
} }
Expand Up @@ -331,8 +331,8 @@ void validate(Descriptor descriptor) {
expectedDescriptor.equals(descriptor), expectedDescriptor.equals(descriptor),
"Message given to FieldScopes.fromSetFields() does not have the same descriptor as the " "Message given to FieldScopes.fromSetFields() does not have the same descriptor as the "
+ "message being tested. Expected %s, got %s.", + "message being tested. Expected %s, got %s.",
expectedDescriptor, expectedDescriptor.getFullName(),
descriptor); descriptor.getFullName());
} }


@Override @Override
Expand Down
Expand Up @@ -23,6 +23,8 @@
import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Message; import com.google.protobuf.Message;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;


/** Utility methods for {@link FieldScope}s and {@link FluentEqualityConfig}. */ /** Utility methods for {@link FieldScope}s and {@link FluentEqualityConfig}. */
Expand Down Expand Up @@ -92,6 +94,15 @@ static Optional<Descriptor> getSingleDescriptor(Iterable<? extends Message> mess
return optDescriptor; return optDescriptor;
} }


/** Joins the arguments into a {@link List} for convenience. */
static <T> List<T> makeList(T first, T second, T... rest) {
List<T> list = new ArrayList<T>();
list.add(first);
list.add(second);
list.addAll(Arrays.asList(rest));
return list;
}

private static final Joiner JOINER = Joiner.on(", "); private static final Joiner JOINER = Joiner.on(", ");


static String join(int... fieldNumbers) { static String join(int... fieldNumbers) {
Expand Down Expand Up @@ -121,7 +132,7 @@ private static String resolveFieldNumbers(
FieldDescriptor field = descriptor.findFieldByNumber(fieldNumber); FieldDescriptor field = descriptor.findFieldByNumber(fieldNumber);
strings.add(field != null ? field.toString() : String.format("%d (?)", fieldNumber)); strings.add(field != null ? field.toString() : String.format("%d (?)", fieldNumber));
} }
return join(strings); return String.format(fmt, join(strings));
} else { } else {
return String.format(fmt, join(fieldNumbers)); return String.format(fmt, join(fieldNumbers));
} }
Expand Down
Expand Up @@ -73,8 +73,19 @@ public static FieldScope fromSetFields(Message message) {
* <p>This can be thought of as the union of the {@link FieldScope}s for each individual message, * <p>This can be thought of as the union of the {@link FieldScope}s for each individual message,
* or the {@link FieldScope} for the merge of all the messages. These are equivalent. * or the {@link FieldScope} for the merge of all the messages. These are equivalent.
*/ */
// TODO(user): Make public when IterableProtoSubject is exposed and this has valid use. public static FieldScope fromSetFields(
private static FieldScope fromSetFields(Iterable<? extends Message> messages) { Message firstMessage, Message secondMessage, Message... rest) {
return fromSetFields(FieldScopeUtil.makeList(firstMessage, secondMessage, rest));
}

/**
* Creates a {@link FieldScope} convering the fields set in every message in the provided list of
* messages, with the same semantics as in {@link #fromSetFields(Message)}.
*
* <p>This can be thought of as the union of the {@link FieldScope}s for each individual message,
* or the {@link FieldScope} for the merge of all the messages. These are equivalent.
*/
public static FieldScope fromSetFields(Iterable<? extends Message> messages) {
return FieldScopeImpl.createFromSetFields(messages); return FieldScopeImpl.createFromSetFields(messages);
} }


Expand Down
Expand Up @@ -160,11 +160,11 @@ final MessageDifferencer toMessageDifferencer(Descriptor descriptor) {
return messageDifferencers.getUnchecked(descriptor); return messageDifferencers.getUnchecked(descriptor);
} }


final Correspondence<Message, Message> toCorrespondence( final <M extends Message> Correspondence<M, M> toCorrespondence(
final Optional<Descriptor> optDescriptor) { final Optional<Descriptor> optDescriptor) {
return new Correspondence<Message, Message>() { return new Correspondence<M, M>() {
@Override @Override
public final boolean compare(@Nullable Message actual, @Nullable Message expected) { public final boolean compare(@Nullable M actual, @Nullable M expected) {
return ProtoTruth.assertThat(actual) return ProtoTruth.assertThat(actual)
.usingConfig(FluentEqualityConfig.this) .usingConfig(FluentEqualityConfig.this)
.testIsEqualTo(expected); .testIsEqualTo(expected);
Expand Down

0 comments on commit a94b128

Please sign in to comment.