1 /* 2 * Copyright (C) 2015 Square, Inc. 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.squareup.javapoet; 17 18 import org.junit.Test; 19 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.fail; 23 24 public final class NameAllocatorTest { 25 usage()26 @Test public void usage() throws Exception { 27 NameAllocator nameAllocator = new NameAllocator(); 28 assertThat(nameAllocator.newName("foo", 1)).isEqualTo("foo"); 29 assertThat(nameAllocator.newName("bar", 2)).isEqualTo("bar"); 30 assertThat(nameAllocator.get(1)).isEqualTo("foo"); 31 assertThat(nameAllocator.get(2)).isEqualTo("bar"); 32 } 33 nameCollision()34 @Test public void nameCollision() throws Exception { 35 NameAllocator nameAllocator = new NameAllocator(); 36 assertThat(nameAllocator.newName("foo")).isEqualTo("foo"); 37 assertThat(nameAllocator.newName("foo")).isEqualTo("foo_"); 38 assertThat(nameAllocator.newName("foo")).isEqualTo("foo__"); 39 } 40 nameCollisionWithTag()41 @Test public void nameCollisionWithTag() throws Exception { 42 NameAllocator nameAllocator = new NameAllocator(); 43 assertThat(nameAllocator.newName("foo", 1)).isEqualTo("foo"); 44 assertThat(nameAllocator.newName("foo", 2)).isEqualTo("foo_"); 45 assertThat(nameAllocator.newName("foo", 3)).isEqualTo("foo__"); 46 assertThat(nameAllocator.get(1)).isEqualTo("foo"); 47 assertThat(nameAllocator.get(2)).isEqualTo("foo_"); 48 assertThat(nameAllocator.get(3)).isEqualTo("foo__"); 49 } 50 characterMappingSubstitute()51 @Test public void characterMappingSubstitute() throws Exception { 52 NameAllocator nameAllocator = new NameAllocator(); 53 assertThat(nameAllocator.newName("a-b", 1)).isEqualTo("a_b"); 54 } 55 characterMappingSurrogate()56 @Test public void characterMappingSurrogate() throws Exception { 57 NameAllocator nameAllocator = new NameAllocator(); 58 assertThat(nameAllocator.newName("a\uD83C\uDF7Ab", 1)).isEqualTo("a_b"); 59 } 60 characterMappingInvalidStartButValidPart()61 @Test public void characterMappingInvalidStartButValidPart() throws Exception { 62 NameAllocator nameAllocator = new NameAllocator(); 63 assertThat(nameAllocator.newName("1ab", 1)).isEqualTo("_1ab"); 64 assertThat(nameAllocator.newName("a-1", 2)).isEqualTo("a_1"); 65 } 66 characterMappingInvalidStartIsInvalidPart()67 @Test public void characterMappingInvalidStartIsInvalidPart() throws Exception { 68 NameAllocator nameAllocator = new NameAllocator(); 69 assertThat(nameAllocator.newName("&ab", 1)).isEqualTo("_ab"); 70 } 71 javaKeyword()72 @Test public void javaKeyword() throws Exception { 73 NameAllocator nameAllocator = new NameAllocator(); 74 assertThat(nameAllocator.newName("public", 1)).isEqualTo("public_"); 75 assertThat(nameAllocator.get(1)).isEqualTo("public_"); 76 } 77 tagReuseForbidden()78 @Test public void tagReuseForbidden() throws Exception { 79 NameAllocator nameAllocator = new NameAllocator(); 80 nameAllocator.newName("foo", 1); 81 try { 82 nameAllocator.newName("bar", 1); 83 fail(); 84 } catch (IllegalArgumentException expected) { 85 assertThat(expected).hasMessageThat().isEqualTo("tag 1 cannot be used for both 'foo' and 'bar'"); 86 } 87 } 88 useBeforeAllocateForbidden()89 @Test public void useBeforeAllocateForbidden() throws Exception { 90 NameAllocator nameAllocator = new NameAllocator(); 91 try { 92 nameAllocator.get(1); 93 fail(); 94 } catch (IllegalArgumentException expected) { 95 assertThat(expected).hasMessageThat().isEqualTo("unknown tag: 1"); 96 } 97 } 98 cloneUsage()99 @Test public void cloneUsage() throws Exception { 100 NameAllocator outterAllocator = new NameAllocator(); 101 outterAllocator.newName("foo", 1); 102 103 NameAllocator innerAllocator1 = outterAllocator.clone(); 104 assertThat(innerAllocator1.newName("bar", 2)).isEqualTo("bar"); 105 assertThat(innerAllocator1.newName("foo", 3)).isEqualTo("foo_"); 106 107 NameAllocator innerAllocator2 = outterAllocator.clone(); 108 assertThat(innerAllocator2.newName("foo", 2)).isEqualTo("foo_"); 109 assertThat(innerAllocator2.newName("bar", 3)).isEqualTo("bar"); 110 } 111 } 112