-
Notifications
You must be signed in to change notification settings - Fork 127
/
PhraseTest.java
204 lines (171 loc) · 6.43 KB
/
PhraseTest.java
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (C) 2013 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.phrase;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.widget.TextView;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import static com.squareup.phrase.Phrase.from;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.fail;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class PhraseTest {
@Rule public ExpectedException thrown = ExpectedException.none();
@Test public void emptyStringFormatsToItself() {
assertThat(from("").format().toString()).isEqualTo("");
}
@Test public void trivialStringFormatsToItself() {
assertThat(from("Hello").format().toString()).isEqualTo("Hello");
}
@Test public void toStringReturnsThePattern() {
assertThat(from("hello {name}").toString()).isEqualTo("hello {name}");
}
@Test public void puttingNullValueThrowsException() {
try {
from("hi {name}").put("name", null);
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Null value for 'name'");
}
}
@Test public void singleCurlyBraceIsAMistake() {
try {
from("{");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
@Test public void twoLeftCurlyBracesFormatAsSingleCurlyBrace() {
assertThat(from("{{").format().toString()).isEqualTo("{");
}
@Test public void testSimpleSuccessfulSubstitution() {
assertThat(from("hi {name}").put("name", "Eric").format().toString()).isEqualTo("hi Eric");
}
@Test public void loneCurlyBraceIsAMistake() {
try {
from("hi { {age}.");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
@Test public void ignoresTokenNextToEscapedBrace() {
assertThat(from("hi {{name} {name}").put("name", "Bubba").format().toString()).isEqualTo(
"hi {name} Bubba");
}
@Test public void canEscapeCurlyBracesImmediatelyBeforeKey() {
assertThat(from("you are {{{name}").put("name", "Steve").format().toString()).isEqualTo(
"you are {Steve");
}
@Test public void canReplaceKeysAndReusePhraseInstance() {
Phrase pattern = from("hi {name}.");
assertThat(pattern.put("name", "George").format().toString()).isEqualTo("hi George.");
pattern.put("name", "Abe");
assertThat(pattern.put("name", "Abe").format().toString()).isEqualTo("hi Abe.");
}
@Test public void patternsCanHaveSeveralKeys() {
assertThat(from("hi {name}, you are {age} years old. {name}").put("name", "Abe")
.put("age", 20)
.format()
.toString()).isEqualTo("hi Abe, you are 20 years old. Abe");
}
@Test public void putOptionalIgnoresKey() {
assertThat(from("Hello").putOptional("key", "value").format().toString()).isEqualTo("Hello");
}
@Test public void putOptionalWorksIfKeyIsPresent() {
assertThat(from("Hello {name}").putOptional("name", "Eric").format().toString())
.isEqualTo("Hello Eric");
}
@Test public void putFailUppercaseNotAllowedMiddle() {
try{
from("Hello {aName}").putOptional("Name", "Eric").format();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()) //
.isEqualTo("Unexpected character 'N'; expecting lower case a-z, '_', or '}'");
}
}
private Phrase gender = from("{gender}");
@Test
public void formatFailsFastWhenKeysAreMissing() {
try {
gender.format();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
@Test public void putFailsFastWhenPuttingUnknownKey() {
try {
gender.put("bogusKey", "whatever");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
@Test public void emptyTokenFailsFast() {
try {
from("illegal {} pattern");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
@Test public void illegalStartOfTokenCharactersFailFast() {
try {
from("blah {NoUppercaseAllowed}");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage()).isEqualTo("Unexpected first character 'N'; must be lower case a-z.");
}
}
@Test public void tokensCanHaveUnderscores() {
assertThat(from("{first_name}").put("first_name", "Eric").format().toString()).isEqualTo(
"Eric");
}
@Test public void keysCannotStartWithUnderscore() {
try {
from("{_foo}");
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
@Test public void testRetainsSpans() {
SpannableStringBuilder ssb =
new SpannableStringBuilder("Hello {name}, you are {age} years old.");
ssb.setSpan("bold", 5, 28, 0);
CharSequence formatted = Phrase.from(ssb).put("name",
"Abe").put("age", 20).format();
assertThat(formatted.toString()).isEqualTo("Hello Abe, you are 20 years old.");
assertThat(formatted).isInstanceOf(Spannable.class);
}
@Test public void intoSetsTargetText() {
Context context = RuntimeEnvironment.application;
TextView textView = new TextView(context);
Phrase.from("Hello {user}!").put("user", "Eric").into(textView);
CharSequence actual = textView.getText().toString();
assertThat(actual.toString()).isEqualTo("Hello Eric!");
}
@Test public void intoNullFailsFast() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("TextView must not be null.");
Phrase.from("Hello {user}!").put("user", "Eric").into(null);
}
}