Explorar el Código

Acronym: adjust API to use an instance method (#380)

Fixes issue #351.
FridaTveit hace 9 años
padre
commit
5eb3a6e50c

+ 11
- 1
exercises/acronym/src/example/java/Acronym.java Ver fichero

@@ -2,7 +2,17 @@ import java.util.regex.Matcher;
2 2
 import java.util.regex.Pattern;
3 3
 
4 4
 public class Acronym {
5
-    public static String generate(String phrase){
5
+    private final String acronym;
6
+
7
+    public Acronym(String phrase) {
8
+        acronym = generateAcronym(phrase);
9
+    }
10
+
11
+    public String get() {
12
+        return acronym;
13
+    }
14
+
15
+    private String generateAcronym(String phrase){
6 16
         final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+");
7 17
         final Matcher matcher = BREAK_WORDS.matcher(phrase);
8 18
         final StringBuilder b = new StringBuilder();

+ 8
- 9
exercises/acronym/src/test/java/AcronymTest.java Ver fichero

@@ -5,12 +5,11 @@ import static org.junit.Assert.assertEquals;
5 5
 
6 6
 public class AcronymTest {
7 7
 
8
-
9 8
     @Test
10 9
     public void fromTitleCasedPhrases() {
11 10
         final String phrase = "Portable Network Graphics";
12 11
         final String expected = "PNG";
13
-        assertEquals(expected, Acronym.generate(phrase));
12
+        assertEquals(expected, new Acronym(phrase).get());
14 13
     }
15 14
 
16 15
     @Ignore
@@ -18,7 +17,7 @@ public class AcronymTest {
18 17
     public void fromOtherTitleCasedPhrases() {
19 18
         final String phrase = "Ruby on Rails";
20 19
         final String expected = "ROR";
21
-        assertEquals(expected, Acronym.generate(phrase));
20
+        assertEquals(expected, new Acronym(phrase).get());
22 21
     }
23 22
 
24 23
     @Ignore
@@ -26,7 +25,7 @@ public class AcronymTest {
26 25
     public void fromInconsistentlyCasedPhrases() {
27 26
         final String phrase = "HyperText Markup Language";
28 27
         final String expected = "HTML";
29
-        assertEquals(expected, Acronym.generate(phrase));
28
+        assertEquals(expected, new Acronym(phrase).get());
30 29
     }
31 30
 
32 31
     @Ignore
@@ -34,7 +33,7 @@ public class AcronymTest {
34 33
     public void fromPhrasesWithPunctuation() {
35 34
         final String phrase = "First In, First Out";
36 35
         final String expected = "FIFO";
37
-        assertEquals(expected, Acronym.generate(phrase));
36
+        assertEquals(expected, new Acronym(phrase).get());
38 37
     }
39 38
 
40 39
     @Ignore
@@ -42,7 +41,7 @@ public class AcronymTest {
42 41
     public void fromOtherPhrasesWithPunctuation() {
43 42
         final String phrase = "PHP: Hypertext Preprocessor";
44 43
         final String expected = "PHP";
45
-        assertEquals(expected, Acronym.generate(phrase));
44
+        assertEquals(expected, new Acronym(phrase).get());
46 45
     }
47 46
 
48 47
     @Ignore
@@ -50,7 +49,7 @@ public class AcronymTest {
50 49
     public void fromPhrasesWithNonAcronymAllCapsWord() {
51 50
         final String phrase = "GNU Image Manipulation Program";
52 51
         final String expected = "GIMP";
53
-        assertEquals(expected, Acronym.generate(phrase));
52
+        assertEquals(expected, new Acronym(phrase).get());
54 53
     }
55 54
 
56 55
     @Ignore
@@ -58,7 +57,7 @@ public class AcronymTest {
58 57
     public void fromPhrasesWithPunctuationAndSentenceCasing() {
59 58
         final String phrase = "Complementary metal-oxide semiconductor";
60 59
         final String expected = "CMOS";
61
-        assertEquals(expected, Acronym.generate(phrase));
60
+        assertEquals(expected, new Acronym(phrase).get());
62 61
     }
63 62
 
64 63
     @Ignore
@@ -66,7 +65,7 @@ public class AcronymTest {
66 65
     public void fromPhraseWithSingleLetterWord() {
67 66
         final String phrase = "Cat in a Hat";
68 67
         final String expected = "CIAH";
69
-        assertEquals(expected, Acronym.generate(phrase));
68
+        assertEquals(expected, new Acronym(phrase).get());
70 69
     }
71 70
 
72 71
 }