1 /* 2 * Copyright (C) 2017 The Libphonenumber 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 package com.google.i18n.phonenumbers.metadata.testing; 17 18 import static com.google.common.truth.Fact.simpleFact; 19 import static com.google.common.truth.Truth.assertAbout; 20 import static com.google.common.truth.Truth.assertWithMessage; 21 22 import com.google.common.collect.FluentIterable; 23 import com.google.common.collect.ImmutableSet; 24 import com.google.common.truth.FailureMetadata; 25 import com.google.common.truth.Subject; 26 import com.google.i18n.phonenumbers.metadata.DigitSequence; 27 import com.google.i18n.phonenumbers.metadata.PrefixTree; 28 import com.google.i18n.phonenumbers.metadata.RangeSpecification; 29 import com.google.i18n.phonenumbers.metadata.RangeTree; 30 import javax.annotation.Nullable; 31 32 /** A Truth subject for asserting on {@link RangeTree} instances. */ 33 public class RangeTreeSubject extends Subject { 34 assertThat(@ullable RangeTree tree)35 public static RangeTreeSubject assertThat(@Nullable RangeTree tree) { 36 return assertAbout(RangeTreeSubject.SUBJECT_FACTORY).that(tree); 37 } 38 assertThat(@ullable PrefixTree tree)39 public static RangeTreeSubject assertThat(@Nullable PrefixTree tree) { 40 return assertAbout(RangeTreeSubject.SUBJECT_FACTORY).that(tree.asRangeTree()); 41 } 42 assertWithMessageThat( @ullable RangeTree tree, String message, Object... args)43 public static RangeTreeSubject assertWithMessageThat( 44 @Nullable RangeTree tree, String message, Object... args) { 45 return assertWithMessage(message, args).about( 46 RangeTreeSubject.SUBJECT_FACTORY).that(tree); 47 } 48 49 private static final Factory<RangeTreeSubject, RangeTree> SUBJECT_FACTORY = 50 RangeTreeSubject::new; 51 52 private final RangeTree actual; 53 RangeTreeSubject(FailureMetadata failureMetadata, @Nullable RangeTree subject)54 private RangeTreeSubject(FailureMetadata failureMetadata, @Nullable RangeTree subject) { 55 super(failureMetadata, subject); 56 this.actual = subject; 57 } 58 59 // Add more methods below as needed. 60 isEmpty()61 public void isEmpty() { 62 if (!actual.isEmpty()) { 63 failWithActual(simpleFact("expected to be empty")); 64 } 65 } 66 isNotEmpty()67 public void isNotEmpty() { 68 if (actual.isEmpty()) { 69 failWithActual(simpleFact("expected not to be empty")); 70 } 71 } 72 hasSize(long size)73 public void hasSize(long size) { 74 check("size()").withMessage("size").that(actual.size()).isEqualTo(size); 75 } 76 contains(String digits)77 public void contains(String digits) { 78 DigitSequence seq = digits.isEmpty() ? DigitSequence.empty() : DigitSequence.of(digits); 79 if (!actual.contains(seq)) { 80 failWithActual("expected to contain ", digits); 81 } 82 } 83 doesNotContain(String digits)84 public void doesNotContain(String digits) { 85 DigitSequence seq = digits.isEmpty() ? DigitSequence.empty() : DigitSequence.of(digits); 86 if (actual.contains(seq)) { 87 failWithActual("expected not to contain", digits); 88 } 89 } 90 containsExactly(RangeSpecification spec)91 public void containsExactly(RangeSpecification spec) { 92 RangeTree tree = RangeTree.from(spec); 93 if (!actual.equals(tree)) { 94 failWithActual("expected to be equal to", spec); 95 } 96 } 97 containsExactly(Iterable<RangeSpecification> specs)98 public void containsExactly(Iterable<RangeSpecification> specs) { 99 RangeTree tree = RangeTree.from(specs); 100 if (!actual.equals(tree)) { 101 failWithActual("expected to be equal to", specs); 102 } 103 } 104 containsExactly(String spec)105 public void containsExactly(String spec) { 106 containsExactly(RangeSpecification.parse(spec)); 107 } 108 containsExactly(String... specs)109 public void containsExactly(String... specs) { 110 containsExactly(FluentIterable.from(specs).transform(RangeSpecification::parse)); 111 } 112 hasLengths(Integer... lengths)113 public void hasLengths(Integer... lengths) { 114 check("getLengths()") 115 .that(actual.getLengths()) 116 .containsExactlyElementsIn(ImmutableSet.copyOf(lengths)); 117 } 118 } 119