Procházet zdrojové kódy

Add hello-world exercise

This implementation does not generate a test suite based on the json
meta the hello-world exercise.
See https://github.com/exercism/todo/issues/13

Fixes https://github.com/exercism/todo/issues/35
Brian Mathiyakom před 10 roky
rodič
revize
d09fb713d1

+ 2
- 1
config.json Zobrazit soubor

@@ -34,7 +34,8 @@
34 34
     "octal",
35 35
     "luhn",
36 36
     "pig-latin",
37
-    "pascals-triangle"
37
+    "pascals-triangle",
38
+    "hello-world"
38 39
   ],
39 40
   "deprecated": [
40 41
   ],

+ 156
- 0
hello-world/GETTING_STARTED.md Zobrazit soubor

@@ -0,0 +1,156 @@
1
+# Getting Started
2
+
3
+These exercises lean on Test-Driven Development (TDD), but they're not an exact match.
4
+
5
+The following steps assume that you are in the base directory of the exercise.
6
+
7
+You must have `gradle` installed.
8
+
9
+## Step 1
10
+
11
+Run the test suite. It's written using the JUnit framework, and can be run with gradle:
12
+
13
+```sh
14
+$ gradle test
15
+```
16
+
17
+This will fail, complaining that there is no method called `hello` for the `HelloWorld` class.
18
+
19
+To fix the error, open `src/main/java/HelloWorld.java` and add a `hello` method that returns a `String`.
20
+The method can return an empty string `""` for now:
21
+
22
+```java
23
+public class HelloWorld {
24
+    public static String hello() {
25
+        return "";
26
+    }
27
+}
28
+```
29
+
30
+## Step 2
31
+
32
+Run the tests again. It will give you a new error, since now the method exists, but the tests are expects the
33
+method to accept one argument as input, a name.
34
+
35
+The errors will look something like:
36
+
37
+```
38
+$ gradle test
39
+:compileJava
40
+:processResources UP-TO-DATE
41
+:classes
42
+:compileTestJava
43
+/exercism/exercises/java/hello-world/src/test/java/HelloWorldTest.java:9: error: method hello in class HelloWorld cannot be applied to given types;
44
+        assertEquals("Hello, World!", HelloWorld.hello(""));
45
+                                                ^
46
+  required: no arguments
47
+  found: String
48
+  reason: actual and formal argument lists differ in length
49
+
50
+  ...
51
+
52
+4 errors
53
+:compileTestJava FAILED
54
+
55
+FAILURE: Build failed with an exception.
56
+
57
+* What went wrong:
58
+Execution failed for task ':compileTestJava'.
59
+> Compilation failed; see the compiler error output for details.
60
+```
61
+
62
+### Fixing the Error
63
+
64
+To fix it, open `src/main/java/HelloWorld.java` and add a `name` to the method the signature:
65
+
66
+```java
67
+public class HelloWorld {
68
+    public static String hello(String name) {
69
+        return "";
70
+    }
71
+}
72
+```
73
+
74
+Now the `hello` method accepts a `name` as an input.
75
+
76
+### Understanding Test Failures
77
+
78
+Test failures will either be compilation errors or test failures. So far, we've been dealing with compilation
79
+errors. Once our test and main code successful compile, `gradle` will run the actual test suite.
80
+
81
+## Step 3
82
+
83
+Run the tests again. You'll notice that the gradle output passes the `:compileTestJava` phase and fails
84
+at the `:test` phase.
85
+
86
+The errors will look something like:
87
+
88
+```
89
+$ gradle test
90
+:compileJava
91
+:processResources UP-TO-DATE
92
+:classes
93
+:compileTestJava
94
+:processTestResources UP-TO-DATE
95
+:testClasses
96
+:test
97
+
98
+HelloWorldTest > helloNoName FAILED
99
+    org.junit.ComparisonFailure at HelloWorldTest.java:9
100
+
101
+...
102
+
103
+3 tests completed, 3 failed
104
+:test FAILED
105
+```
106
+
107
+These are actual test failures. It means that both our main and test java code is running but the test is
108
+expecting one outcome, but getting another.
109
+
110
+Open `src/test/java/HelloWorldTest.java` and go to line 9.
111
+
112
+```java
113
+assertEquals("Hello, World!", HelloWorld.hello(""));
114
+```
115
+
116
+The test is expecting that `hello` returns "Hello, World!", when given an empty string (`""`) as input.
117
+The easiest way to make the test pass is to return `"Hello, World!"` instead of `"".
118
+
119
+```java
120
+public class HelloWorld {
121
+    public static String hello(String name) {
122
+        return "Hello, World!";
123
+    }
124
+}
125
+```
126
+
127
+## Wash, Rinse, Repeat
128
+
129
+Run the tests again.
130
+
131
+If it fails you're going to need to read the error message carefully to figure out what went wrong, fix the problem
132
+in the main code, and then try again until all the tests pass.
133
+
134
+```
135
+$ gradle test
136
+:compileJava
137
+:processResources UP-TO-DATE
138
+:classes
139
+:compileTestJava
140
+:processTestResources UP-TO-DATE
141
+:testClasses
142
+:test
143
+
144
+BUILD SUCCESSFUL
145
+```
146
+
147
+## Submit
148
+
149
+When everything is passing, you can submit your code with the following command:
150
+
151
+```
152
+$ exercism submit src/main/java/HelloWorld.java
153
+```
154
+
155
+
156
+

+ 12
- 0
hello-world/build.gradle Zobrazit soubor

@@ -0,0 +1,12 @@
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.10"
11
+}
12
+

+ 9
- 0
hello-world/src/example/java/HelloWorld.java Zobrazit soubor

@@ -0,0 +1,9 @@
1
+public class HelloWorld {
2
+    public static String hello(String name) {
3
+        if (name == null || name.isEmpty()) {
4
+            return "Hello, World!";
5
+        }
6
+
7
+        return String.format("Hello, %s!", name);
8
+    }
9
+}

+ 0
- 0
hello-world/src/main/java/.keep Zobrazit soubor


+ 2
- 0
hello-world/src/main/java/HelloWorld.java Zobrazit soubor

@@ -0,0 +1,2 @@
1
+public class HelloWorld {
2
+}

+ 22
- 0
hello-world/src/test/java/HelloWorldTest.java Zobrazit soubor

@@ -0,0 +1,22 @@
1
+import org.junit.Test;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+
5
+public class HelloWorldTest {
6
+
7
+    @Test
8
+    public void helloNoName() {
9
+        assertEquals("Hello, World!", HelloWorld.hello(""));
10
+        assertEquals("Hello, World!", HelloWorld.hello(null));
11
+    }
12
+
13
+    @Test
14
+    public void helloSampleName() {
15
+        assertEquals("Hello, Alice!", HelloWorld.hello("Alice"));
16
+    }
17
+
18
+    @Test
19
+    public void helloAnotherSampleName() throws Exception {
20
+        assertEquals("Hello, Bob!", HelloWorld.hello("Bob"));
21
+    }
22
+}

+ 1
- 0
settings.gradle Zobrazit soubor

@@ -9,6 +9,7 @@ include 'etl'
9 9
 include 'gigasecond'
10 10
 include 'grade-school'
11 11
 include 'hamming'
12
+include 'hello-world'
12 13
 include 'luhn'
13 14
 include 'meetup'
14 15
 include 'nucleotide-count'