Explorar el Código

proverb: add to track (#1077)

* Initial commit for new exercise proverb

* Add first test and source code passing it

* Add second test with short chain of consequences

* Add test for longer chain of events

* Add test asserting dictionary is not hard coded

* Add test for entire proverb

* All tests and source code complete

* Add stub due to low difficulty level

* Add proverb to config.json and settings.gradle

* Limit scope to package-private asccess level

* Set config to unlock proverb by two-fer

* Update tests to match exercism canonical data

* Fix starter implementation to only include thrown exception

* Add StringBuilder and basic refactoring

* Fix test spacing

* Refactor out extraneous if-statement

* Add curly braces around if-statement
Ryan Workman hace 8 años
padre
commit
c88d6e20f8

+ 12
- 0
config.json Ver fichero

203
     {
203
     {
204
       "core": false,
204
       "core": false,
205
       "difficulty": 4,
205
       "difficulty": 4,
206
+      "slug": "proverb",
207
+      "topics": [
208
+        "arrays",
209
+        "loops",
210
+        "strings"
211
+      ],
212
+      "unlocked_by": "two-fer",
213
+      "uuid": "9906491b-a638-408d-86a4-4ad320a92658"
214
+    },
215
+    {
216
+      "core": false,
217
+      "difficulty": 4,
206
       "slug": "isbn-verifier",
218
       "slug": "isbn-verifier",
207
       "topics": [
219
       "topics": [
208
         "integers",
220
         "integers",

+ 19
- 0
exercises/proverb/.meta/src/reference/java/Proverb.java Ver fichero

1
+final class Proverb {
2
+    private String[] words;
3
+
4
+    Proverb(String[] words) {
5
+        this.words = words;
6
+    }
7
+
8
+    String recite() {
9
+        if (words.length < 1) {
10
+            return "";
11
+        }
12
+        final StringBuilder result = new StringBuilder();
13
+        for(int i = 1; i < words.length; i++) {
14
+            result.append("For want of a " + words[i - 1] + " the " + words[i] + " was lost.\n");
15
+        }
16
+        result.append("And all for the want of a " + words[0] + ".");
17
+        return result.toString();
18
+    }
19
+}

+ 29
- 0
exercises/proverb/README.md Ver fichero

1
+# Proverb in Java
2
+
3
+For want of a horseshoe nail, a kingdom was lost, or so the saying goes. Dynamically output the full test of this proverbial rhyme.
4
+
5
+> For want of a nail the shoe was lost.<br>
6
+> For want of a shoe the horse was lost.<br>
7
+> For want of a horse the rider was lost.<br>
8
+> For want of a rider the message was lost.<br>
9
+> For want of a message the battle was lost.<br>
10
+> For want of a battle the kingdom was lost.<br>
11
+> And all for the want of a horseshoe nail.<br>
12
+
13
+# Running the tests
14
+
15
+You can run all the tests for an exercise by entering
16
+
17
+```sh
18
+$ gradle test
19
+```
20
+
21
+in your terminal.
22
+
23
+## Source
24
+
25
+Wikipedia [https://en.wikipedia.org/wiki/For_Want_of_a_Nail](https://en.wikipedia.org/wiki/For_Want_of_a_Nail)
26
+
27
+## Submitting Incomplete Solutions
28
+
29
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 18
- 0
exercises/proverb/build.gradle Ver fichero

1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.12"
11
+}
12
+
13
+test {
14
+  testLogging {
15
+    exceptionFormat = 'full'
16
+    events = ["passed", "failed", "skipped"]
17
+  }
18
+}

+ 11
- 0
exercises/proverb/src/main/java/Proverb.java Ver fichero

1
+class Proverb {
2
+
3
+    Proverb(String[] words) {
4
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5
+    }
6
+
7
+    String recite() {
8
+        throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9
+    }
10
+
11
+}

+ 80
- 0
exercises/proverb/src/test/java/ProverbTest.java Ver fichero

1
+import org.junit.Ignore;
2
+import org.junit.Test;
3
+
4
+import static org.hamcrest.CoreMatchers.is;
5
+import static org.junit.Assert.assertThat;
6
+
7
+public class ProverbTest {
8
+
9
+    @Test
10
+    public void zeroWordsAreGiven() {
11
+        String[] words  = new String[0];
12
+        String proverb  = new Proverb(words).recite(),
13
+               expected = "";
14
+
15
+        assertThat(proverb, is(expected));
16
+    }
17
+
18
+    @Ignore("Remove to run test")
19
+    @Test
20
+    public void singlePieceOfProverb() {
21
+        String[] words  = new String[]{"nail"};
22
+        String proverb  = new Proverb(words).recite(),
23
+               expected = "And all for the want of a nail.";
24
+
25
+        assertThat(proverb, is(expected));
26
+    }
27
+
28
+    @Ignore("Remove to run test")
29
+    @Test
30
+    public void twoPiecesOfProverb() {
31
+        String[] words  = new String[]{"nail", "shoe"};
32
+        String proverb  = new Proverb(words).recite(),
33
+               expected = "For want of a nail the shoe was lost.\n" +
34
+                          "And all for the want of a nail.";
35
+
36
+        assertThat(proverb, is(expected));
37
+    }
38
+
39
+    @Ignore("Remove to run test")
40
+    @Test
41
+    public void shortChainOfConsequences() {
42
+        String[] words  = new String[]{"nail", "shoe", "horse"};
43
+        String proverb  = new Proverb(words).recite(),
44
+               expected = "For want of a nail the shoe was lost.\n" +
45
+                          "For want of a shoe the horse was lost.\n" +
46
+                          "And all for the want of a nail.";
47
+
48
+        assertThat(proverb, is(expected));
49
+    }
50
+
51
+    @Ignore("Remove to run test")
52
+    @Test
53
+    public void fullProverb() {
54
+        String[] words  = new String[]{"nail", "shoe", "horse", "rider", "message", "battle", "kingdom"};
55
+        String proverb  = new Proverb(words).recite(),
56
+               expected = "For want of a nail the shoe was lost.\n" +
57
+                          "For want of a shoe the horse was lost.\n" +
58
+                          "For want of a horse the rider was lost.\n" +
59
+                          "For want of a rider the message was lost.\n" +
60
+                          "For want of a message the battle was lost.\n" +
61
+                          "For want of a battle the kingdom was lost.\n" +
62
+                          "And all for the want of a nail.";
63
+
64
+        assertThat(proverb, is(expected));
65
+    }
66
+
67
+    @Ignore("Remove to run test")
68
+    @Test
69
+    public void fourPiecesModernizedProverb() {
70
+        String[] words  = new String[]{"pin", "gun", "soldier", "battle"};
71
+        String proverb  = new Proverb(words).recite(),
72
+               expected = "For want of a pin the gun was lost.\n" +
73
+                          "For want of a gun the soldier was lost.\n" +
74
+                          "For want of a soldier the battle was lost.\n" +
75
+                          "And all for the want of a pin.";
76
+
77
+        assertThat(proverb, is(expected));
78
+    }
79
+
80
+}

+ 1
- 0
exercises/settings.gradle Ver fichero

58
 include 'poker'
58
 include 'poker'
59
 include 'prime-factors'
59
 include 'prime-factors'
60
 include 'protein-translation'
60
 include 'protein-translation'
61
+include 'proverb'
61
 include 'pythagorean-triplet'
62
 include 'pythagorean-triplet'
62
 include 'queen-attack'
63
 include 'queen-attack'
63
 include 'raindrops'
64
 include 'raindrops'