Просмотр исходного кода

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

Fixes issue #351.
FridaTveit 9 лет назад
Родитель
Сommit
5eb3a6e50c
2 измененных файлов: 19 добавлений и 10 удалений
  1. 11
    1
      exercises/acronym/src/example/java/Acronym.java
  2. 8
    9
      exercises/acronym/src/test/java/AcronymTest.java

+ 11
- 1
exercises/acronym/src/example/java/Acronym.java Просмотреть файл

2
 import java.util.regex.Pattern;
2
 import java.util.regex.Pattern;
3
 
3
 
4
 public class Acronym {
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
         final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+");
16
         final Pattern BREAK_WORDS = Pattern.compile("[A-Z]+[a-z]*|[a-z]+");
7
         final Matcher matcher = BREAK_WORDS.matcher(phrase);
17
         final Matcher matcher = BREAK_WORDS.matcher(phrase);
8
         final StringBuilder b = new StringBuilder();
18
         final StringBuilder b = new StringBuilder();

+ 8
- 9
exercises/acronym/src/test/java/AcronymTest.java Просмотреть файл

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