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.table; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static com.google.i18n.phonenumbers.metadata.testing.RangeTreeSubject.assertThat; 20 import static java.util.Arrays.asList; 21 import static com.google.i18n.phonenumbers.metadata.testing.AssertUtil.assertThrows; 22 23 import com.google.i18n.phonenumbers.metadata.RangeSpecification; 24 import com.google.i18n.phonenumbers.metadata.RangeTree; 25 import java.util.Arrays; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 @RunWith(JUnit4.class) 31 public class ChangeTest { 32 private static final Column<String> COL_A = Column.ofString("A"); 33 private static final Column<String> COL_B = Column.ofString("B"); 34 35 @Test testEmpty()36 public void testEmpty() { 37 assertThat(Change.empty().getRanges()).isEmpty(); 38 assertThat(Change.empty().getAssignments()).isEmpty(); 39 // Not all "no-op" changes are equal to the "empty" change (unlike RangeTree). This should be 40 // fine however since Changes are expected to have a very short lifecycle in most code and not 41 // be used as keys in maps etc... 42 assertThat(Change.empty()) 43 .isNotEqualTo(Change.builder(RangeTree.empty()).assign(COL_A, "foo").build()); 44 assertThat(Change.empty()).isNotEqualTo(Change.builder(ranges("12xxxx")).build()); 45 } 46 47 @Test testBuilder()48 public void testBuilder() { 49 Change c = Change.builder(ranges("12xxxx")).assign(COL_A, "foo").assign(COL_B, "bar").build(); 50 assertThat(c.getRanges()).containsExactly("12xxxx"); 51 Assignment<String> assignFoo = Assignment.of(COL_A, "foo"); 52 Assignment<String> assignBar = Assignment.of(COL_B, "bar"); 53 assertThat(c.getAssignments()).containsExactly(assignFoo, assignBar); 54 assertThat(c).isEqualTo(Change.of(ranges("12xxxx"), asList(assignFoo, assignBar))); 55 // Don't allow same column twice (this could be relaxed in future if necessary)! 56 assertThrows(IllegalArgumentException.class, 57 () -> Change.builder(ranges("12xxxx")).assign(COL_A, "foo").assign(COL_A, "bar").build()); 58 } 59 60 @Test testBuilderUnassignment()61 public void testBuilderUnassignment() { 62 Change c = Change.builder(ranges("12xxxx")).unassign(COL_A).build(); 63 Assignment<String> unassign = Assignment.unassign(COL_A); 64 assertThat(c.getAssignments()).containsExactly(unassign); 65 assertThat(c).isEqualTo(Change.of(ranges("12xxxx"), asList(unassign))); 66 } 67 ranges(String... rangeSpecs)68 private static RangeTree ranges(String... rangeSpecs) { 69 return RangeTree.from(Arrays.stream(rangeSpecs).map(RangeSpecification::parse)); 70 } 71 } 72