|
| 1 | +/* |
| 2 | + * Copyright 2026 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.testing; |
| 18 | + |
| 19 | +import static com.google.common.truth.Fact.fact; |
| 20 | + |
| 21 | +import com.google.common.truth.FailureMetadata; |
| 22 | +import com.google.common.truth.Subject; |
| 23 | +import io.grpc.Status; |
| 24 | +import javax.annotation.Nullable; |
| 25 | + |
| 26 | +/** Propositions for {@link Status} subjects. */ |
| 27 | +public final class StatusSubject extends Subject { |
| 28 | + |
| 29 | + private static final Subject.Factory<StatusSubject, Status> statusFactory = new Factory(); |
| 30 | + |
| 31 | + public static Subject.Factory<StatusSubject, Status> status() { |
| 32 | + return statusFactory; |
| 33 | + } |
| 34 | + |
| 35 | + private final Status actual; |
| 36 | + |
| 37 | + private StatusSubject(FailureMetadata metadata, @Nullable Status subject) { |
| 38 | + super(metadata, subject); |
| 39 | + this.actual = subject; |
| 40 | + } |
| 41 | + |
| 42 | + /** Fails if the subject is not OK. */ |
| 43 | + public void isOk() { |
| 44 | + if (actual == null) { |
| 45 | + failWithActual("expected to be OK but was", "null"); |
| 46 | + } else if (!actual.isOk()) { |
| 47 | + failWithoutActual( |
| 48 | + fact("expected to be OK but was", actual.getCode()), |
| 49 | + fact("description", actual.getDescription()), |
| 50 | + fact("cause", actual.getCause())); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** Fails if the subject does not have the given code. */ |
| 55 | + public void hasCode(Status.Code expectedCode) { |
| 56 | + if (actual == null) { |
| 57 | + failWithActual("expected to have code " + expectedCode + " but was", "null"); |
| 58 | + } else { |
| 59 | + check("getCode()").that(actual.getCode()).isEqualTo(expectedCode); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private static final class Factory implements Subject.Factory<StatusSubject, Status> { |
| 64 | + @Override |
| 65 | + public StatusSubject createSubject(FailureMetadata metadata, @Nullable Status that) { |
| 66 | + return new StatusSubject(metadata, that); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments