testingreflections / NarrativeFixture (http://pairwith.us/current-project.html)
New Narrative Fixture as featured in http://pairwith.us
Clone this repository (size: 2.4 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/testingreflections/narrativefixture/
| commit 42: | e3def245b8c9 |
| parent 41: | caa3ed9295a1 |
| branch: | default |
[am, ap, pairwithus] New Behaviour: MethodName tiny type knows how to take a phrase and make it read like a method name
S02E08
8 months ago
Changed (Δ1.9 KB):
raw changeset »
src/org/jnarrate/fit/fixture/entityname/MethodName.java (35 lines added, 0 lines removed)
test/org/jnarrate/fit/fixture/entityname/MethodNameShould.java (37 lines added, 0 lines removed)
Up to file-list src/org/jnarrate/fit/fixture/entityname/MethodName.java:
1 |
package org.jnarrate.fit.fixture.entityname; |
|
2 |
||
3 |
import org.jnarrate.tinytype.TinyType; |
|
4 |
||
5 |
public class MethodName extends TinyType<String>{ |
|
6 |
||
7 |
public MethodName(String name) { |
|
8 |
super(convertToMethodCase(name)); |
|
9 |
} |
|
10 |
||
11 |
private static String convertToMethodCase(String name) { |
|
12 |
StringBuffer result = new StringBuffer(); |
|
13 |
String[] words = name.trim().split("\\s+"); |
|
14 |
for (String word : words) { |
|
15 |
result.append(capitalised(word)); |
|
16 |
} |
|
17 |
return lowerCaseFirstLetterOf(result.toString()); |
|
18 |
} |
|
19 |
||
20 |
private static String capitalised(String word) { |
|
21 |
return firstLetterOf(word).toUpperCase() + restOf(word); |
|
22 |
} |
|
23 |
||
24 |
private static String firstLetterOf(String word) { |
|
25 |
return word.substring(0,1); |
|
26 |
} |
|
27 |
||
28 |
private static String restOf(String word) { |
|
29 |
return word.substring(1); |
|
30 |
} |
|
31 |
||
32 |
private static String lowerCaseFirstLetterOf(String name) { |
|
33 |
return firstLetterOf(name).toLowerCase() + restOf(name); |
|
34 |
} |
|
35 |
} |
Up to file-list test/org/jnarrate/fit/fixture/entityname/MethodNameShould.java:
1 |
package org.jnarrate.fit.fixture.entityname; |
|
2 |
||
3 |
import static org.hamcrest.CoreMatchers.is; |
|
4 |
import static org.junit.Assert.assertThat; |
|
5 |
||
6 |
import java.util.Arrays; |
|
7 |
import java.util.List; |
|
8 |
||
9 |
import org.junit.Test; |
|
10 |
||
11 |
public class MethodNameShould { |
|
12 |
||
13 |
@Test |
|
14 |
public void convertSomethingDifferentToMethodCase() throws Exception { |
|
15 |
assertThat(converted("a different method name"), is("aDifferentMethodName")); |
|
16 |
} |
|
17 |
||
18 |
@Test |
|
19 |
public void convertTheseToMethodCase() throws Exception { |
|
20 |
List<String> methodNames = Arrays.asList ( |
|
21 |
"a Method Name", |
|
22 |
"A Method Name", |
|
23 |
"A method name", |
|
24 |
"A method name", |
|
25 |
"A method name ", |
|
26 |
" A method name" |
|
27 |
); |
|
28 |
||
29 |
for (String methodName : methodNames) { |
|
30 |
assertThat("Expected [" + methodName + "] to be converted to aMethodName", converted(methodName), is("aMethodName")); |
|
31 |
} |
|
32 |
} |
|
33 |
||
34 |
private String converted(String methodName) { |
|
35 |
return new MethodName(methodName).toString(); |
|
36 |
} |
|
37 |
} |
