Skip to content

Commit

Permalink
Create ProtoTruth and LiteProtoTruth utility classes, and deprecate t…
Browse files Browse the repository at this point in the history
…he original factories.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135520761
  • Loading branch information
torquestomp authored and cpovirk committed Oct 10, 2016
1 parent a9240a4 commit 80f6b87
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 14 deletions.
Expand Up @@ -23,8 +23,8 @@
/** /**
* A set of static methods to begin a Truth assertion chain for types that require java8. * A set of static methods to begin a Truth assertion chain for types that require java8.
* *
* <p><em>Note: Usage of different failure strategies such as <em>assume</em> and <em>about</em> * <p>Note: Usage of different failure strategies such as <em>assume</em> and <em>expect</em> should
* should rely on {@link * rely on {@link
* com.google.common.truth.AbstractVerb#about(com.google.common.truth.SubjectFactory)} to begin a * com.google.common.truth.AbstractVerb#about(com.google.common.truth.SubjectFactory)} to begin a
* chain with those alternative behaviors. * chain with those alternative behaviors.
*/ */
Expand Down
Expand Up @@ -55,11 +55,13 @@ public abstract static class Factory<S extends LiteProtoSubject<S, M>, M extends
* about Lite Protobuf properties. * about Lite Protobuf properties.
*/ */
public static Factory<?, MessageLite> liteProtos() { public static Factory<?, MessageLite> liteProtos() {
// TODO(user): Migrate users, make this package-private.
return UntypedSubjectFactory.INSTANCE; return UntypedSubjectFactory.INSTANCE;
} }


/** Returns a Subject using the assertion strategy on the provided {@link MessageLite}. */ /** Returns a Subject using the assertion strategy on the provided {@link MessageLite}. */
public static LiteProtoSubject<?, MessageLite> assertThat(@Nullable MessageLite messageLite) { public static LiteProtoSubject<?, MessageLite> assertThat(@Nullable MessageLite messageLite) {
// TODO(user): Migrate users, delete this method.
return assertAbout(LiteProtoSubject.liteProtos()).that(messageLite); return assertAbout(LiteProtoSubject.liteProtos()).that(messageLite);
} }


Expand Down
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.truth.extensions.proto;

import static com.google.common.truth.Truth.assertAbout;

import com.google.common.truth.AbstractVerb;
import com.google.common.truth.SubjectFactory;
import com.google.protobuf.MessageLite;
import javax.annotation.Nullable;

/**
* A set of static methods to begin a Truth assertion chain for the lite version of protocol
* buffers.
*
* <p>This class implements a subset of what {@link ProtoTruth} provides, so if you are already
* {@link ProtoTruth}, you should not import this class. {@code LiteProtoTruth} is only useful if
* you cannot depend on {@link ProtoTruth} for dependency management reasons.
*
* <p>Note: Usage of different failure strategies such as <em>assume</em> and <em>expect</em> should
* rely on {@link AbstractVerb#about(SubjectFactory)} to begin a chain with those alternative
* behaviors.
*/
public final class LiteProtoTruth {
public static LiteProtoSubject<?, MessageLite> assertThat(@Nullable MessageLite messageLite) {
return assertAbout(liteProtos()).that(messageLite);
}

public static LiteProtoSubject.Factory<?, MessageLite> liteProtos() {
return LiteProtoSubject.liteProtos();
}

private LiteProtoTruth() {}
}
Expand Up @@ -15,7 +15,7 @@
*/ */
package com.google.common.truth.extensions.proto; package com.google.common.truth.extensions.proto;


import static com.google.common.truth.extensions.proto.LiteProtoSubject.assertThat; import static com.google.common.truth.extensions.proto.LiteProtoTruth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -130,8 +130,8 @@ public LiteProtoSubjectTest(@SuppressWarnings("unused") String name, Config conf
this.config = config; this.config = config;
} }


private LiteProtoSubject<?, ?> expectThat(@Nullable MessageLite m) { private LiteProtoSubject<?, MessageLite> expectThat(@Nullable MessageLite m) {
return expect.about(LiteProtoSubject.<MessageLite>liteProtos()).that(m); return expect.about(LiteProtoTruth.liteProtos()).that(m);
} }


private Subject<?, ?> expectThat(@Nullable Object o) { private Subject<?, ?> expectThat(@Nullable Object o) {
Expand Down
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */

package com.google.common.truth.extensions.proto; package com.google.common.truth.extensions.proto;


import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -34,13 +35,13 @@
import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
* Truth subjects for the full version of Protocol Buffers. * Truth subject for the full version of Protocol Buffers.
* *
* <p>{@code ProtoSubject.assertThat(actual).isEqualTo(expected)} performs the same assertion as * <p>{@code ProtoSubject.assertThat(actual).isEqualTo(expected)} performs the same assertion as
* {@code Truth.assertThat(actual).isEqualTo(expected)}, but with a better failure message. * {@code Truth.assertThat(actual).isEqualTo(expected)}, but with a better failure message. By
* By default, the assertions are strict with respect to repeated field order, missing fields, etc. * default, the assertions are strict with respect to repeated field order, missing fields, etc.
* This behavior can be changed with the configuration methods on this subject, e.g. * This behavior can be changed with the configuration methods on this subject, e.g. {@code
* {@code ProtoSubject.assertThat(actual).ignoringRepeatedFieldOrder().isEqualTo(expected)}. * ProtoSubject.assertThat(actual).ignoringRepeatedFieldOrder().isEqualTo(expected)}.
* *
* <p>Equality tests, and other methods, may yield slightly different behavior for versions 2 and 3 * <p>Equality tests, and other methods, may yield slightly different behavior for versions 2 and 3
* of Protocol Buffers. If testing protos of multiple versions, make sure you understand the * of Protocol Buffers. If testing protos of multiple versions, make sure you understand the
Expand Down Expand Up @@ -73,11 +74,15 @@ public abstract static class Factory<S extends ProtoSubject<S, M>, M extends Mes
* things about Protobuf properties. * things about Protobuf properties.
*/ */
public static Factory<?, Message> protos() { public static Factory<?, Message> protos() {
// TODO(user): Migrate users, make this package-private.
return UntypedSubjectFactory.INSTANCE; return UntypedSubjectFactory.INSTANCE;
} }


/** Returns a {@link Subject} using the assertion strategy on the provided {@link Message}. */ /**
* Returns a {@link Subject} using the assertion strategy on the provided {@link Message}.
*/
public static ProtoSubject<?, Message> assertThat(@Nullable Message message) { public static ProtoSubject<?, Message> assertThat(@Nullable Message message) {
// TODO(user): Migrate users, delete this method.
return assertAbout(ProtoSubject.protos()).that(message); return assertAbout(ProtoSubject.protos()).that(message);
} }


Expand Down
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.common.truth.extensions.proto;

import static com.google.common.truth.Truth.assertAbout;

import com.google.common.truth.AbstractVerb;
import com.google.common.truth.SubjectFactory;
import com.google.protobuf.Message;
import com.google.protobuf.MessageLite;
import javax.annotation.Nullable;

/**
* A set of static methods to begin a Truth assertion chain for protocol buffers.
*
* <p>Note: Usage of different failure strategies such as <em>assume</em> and <em>expect</em> should
* rely on {@link AbstractVerb#about(SubjectFactory)} to begin a chain with those alternative
* behaviors.
*/
public final class ProtoTruth {
//////////////////////////////////////////////////////////////////////////////////////////////////
// assertThat() overloads.
//////////////////////////////////////////////////////////////////////////////////////////////////

public static LiteProtoSubject<?, MessageLite> assertThat(@Nullable MessageLite messageLite) {
return assertAbout(liteProtos()).that(messageLite);
}

public static ProtoSubject<?, Message> assertThat(@Nullable Message message) {
return assertAbout(protos()).that(message);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
// SubjectFactory factories.
//////////////////////////////////////////////////////////////////////////////////////////////////

public static ProtoSubject.Factory<?, Message> protos() {
return ProtoSubject.protos();
}

public static LiteProtoSubject.Factory<?, MessageLite> liteProtos() {
return LiteProtoSubject.liteProtos();
}

private ProtoTruth() {}
}
Expand Up @@ -15,7 +15,7 @@
*/ */
package com.google.common.truth.extensions.proto; package com.google.common.truth.extensions.proto;


import static com.google.common.truth.extensions.proto.ProtoSubject.assertThat; import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
Expand Down
Expand Up @@ -15,7 +15,7 @@
*/ */
package com.google.common.truth.extensions.proto; package com.google.common.truth.extensions.proto;


import static com.google.common.truth.extensions.proto.ProtoSubject.assertThat; import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


import com.google.protobuf.Message; import com.google.protobuf.Message;
Expand Down
Expand Up @@ -116,7 +116,7 @@ protected final boolean isProto3() {
} }


protected final ProtoSubject<?, Message> expectThat(@Nullable Message message) { protected final ProtoSubject<?, Message> expectThat(@Nullable Message message) {
return expect.about(ProtoSubject.protos()).that(message); return expect.about(ProtoTruth.protos()).that(message);
} }


protected final void expectIsEqualToFailed(AssertionError e) { protected final void expectIsEqualToFailed(AssertionError e) {
Expand Down

0 comments on commit 80f6b87

Please sign in to comment.