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