More Related Content
Similar to TDD Hands-on (20)
More from Shuji Watanabe (20)
TDD Hands-on
- 1. PRE TDD BOOT CAMP JAVA
TDD Boot Camp
Introduction
Overview
1 / 14
- 2. PRE TDD BOOT CAMP JAVA
Exercise 0: Interface design
public abstract void convert(String text);
public abstract String getFormatedText();
public abstract String convert(String text);
package tddbootcamp.sapporo.wikiengine;
public interface WikiEngine {
/**
* Wiki html .
* @param text
* @return html
* /
String toHtml(String text);
}
2 / 14
- 3. PRE TDD BOOT CAMP JAVA
Exercise 1: First Test (20 minutes)
1.
package tddbootcamp.sapporo.wikiengine;
public class WikiEngineImplTest {
}
2.
@Test
public void toHtml_HelloWorld() {
WikiEngineImpl target = new WikiEngineImpl();
String input = "Hello World";
String expected = "Hello World";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
3.
3 / 14
- 4. PRE TDD BOOT CAMP JAVA
package tddbootcamp.sapporo.wikiengine;
public class WikiEngineImpl {
public String toHtml(String input) {
return null;
}
}
4.
public String toHtml(String input) {
return "Hello World";
}
4 / 14
- 5. PRE TDD BOOT CAMP JAVA
Exercise 2: Second Test (15 minutes)
1.
@Test
public void toHtml_TDD_Bootcamp() {
WikiEngineImpl target = new WikiEngineImpl();
String input = "TDD Bootcamp";
String expected = “TDD Bootcamp”;
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
2.
public String toHtml(String input) {
return input;
}
3.
5 / 14
- 6. PRE TDD BOOT CAMP JAVA
public class WikiEngineImplTest {
WikiEngineImpl target;
@Before
public void setUp() {
target = new WikiEngineImpl();
}
@Test
public void toHtml_HelloWorld() {
String input = "Hello World";
String expected = "Hello World";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
@Test
public void toHtml_TDD_Bootcamp() {
String input = "TDD Bootcamp";
String expected = "TDD Bootcamp";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
}
6 / 14
- 7. PRE TDD BOOT CAMP JAVA
Exercise 3: Interface Test (15 minutes)
1.
@Test
public void implements_WikiEngine() {
assertThat(target, is(instanceOf( )));
}
2.
package tddbootcamp.sapporo.wikiengine;
public class WikiEngineImpl implements WikiEngine {
@Override
public String toHtml(String input) {
return input;
}
}
7 / 14
- 8. PRE TDD BOOT CAMP JAVA
Exercise 4: Null args test (15 minutes)
1.
@Test(expected = IllegalArgumentException.class)
public void toHtml_null() {
target.toHtml(input);
}
2.
package tddbootcamp.sapporo.wikiengine;
public class WikiEngineImpl implements WikiEngine {
@Override
public String toHtml(String input) {
if (input == null)
throw new IllegalArgumentException("input == null");
return input;
}
}
8 / 14
- 9. PRE TDD BOOT CAMP JAVA
Exercise 5: Heading (15 minutes)
1.
@Test
public void toHtml_Heading() {
String input = "= Heading =";
String expected = "<h1>Heading</h1>";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
2.
package tddbootcamp.sapporo.wikiengine;
public class WikiEngineImpl implements WikiEngine {
@Override
public String toHtml(String input) {
if (input == null)
throw new IllegalArgumentException("input == null");
if (input.startsWith("= ") && input.endsWith(" =")) {
return "<h1>"
+ input.substring(2, input.length() - 2) + "</h1>";
}
return input;
}
9 / 14
- 10. PRE TDD BOOT CAMP JAVA
Exercise 6: Subheading (15 minutes)
1.
@Test
public void toHtml_Heading2() {
String input = "== Heading2 ==";
String expected = "<h2>Heading2</h2>";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
2.
package tddbootcamp.sapporo.wikiengine;
public class WikiEngineImpl implements WikiEngine {
@Override
public String toHtml(String input) {
if (input == null)
throw new IllegalArgumentException(“input == null”);
if (input.startsWith("= ") && input.endsWith(" =")) {
return "<h1>" + input.substring(2, input.length() - 2)
+ "</h1>";
} else if (input.startsWith("== ") && input.endsWith(" ==")) {
return "<h2>" + input.substring(3, input.length() - 3)
+ "</h2>";
}
return input;
}
}
10 / 14
- 11. PRE TDD BOOT CAMP JAVA
Exercise 7: Refactering (20 minutes)
1.
if (input.startsWith("= ") && input.endsWith(" =")) {
return "<h1>" + input.substring(2, input.length() - 2)
+ "</h1>";
} else if (input.startsWith("== ") && input.endsWith(" ==")) {
return "<h2>" + input.substring(3, input.length() - 3)
+ "</h2>";
}
2.
public class WikiEngineImpl implements WikiEngine {
static final Pattern HEADER_PATTERN = Pattern.compile("^(=+) .* (=+)$");
@Override
public String toHtml(String input) {
if (input == null) throw new IllegalArgumentException("input == null");
Matcher m = HEADER_PATTERN.matcher(input);
if (m.find()) {
String start = m.group(1);
String end = m.group(2);
if (start.length() == end.length()) {
int level = start.length();
String body = input.substring(level + 1, input.length() - level - 1);
return "<h" + level + ">" + body + "</h" + level + ">";
}
}
return input;
}
}
11 / 14
- 12. PRE TDD BOOT CAMP JAVA
3.
@Test
public void toHtml_Level3() {
String input = "=== Level3 ===";
String expected = "<h3>Level3</h3>";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
@Test
public void toHtml_Level4() {
String input = "==== Level4 ====";
String expected = "<h4>Level4</h4>";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
@Test
public void toHtml_Level5() {
String input = "===== Level5 =====";
String expected = "<h5>Level5</h5>";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
@Test
public void toHtml_Level6() {
String input = "====== Level6 ======";
String expected = "<h6>Level6</h6>";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
12 / 14
- 13. PRE TDD BOOT CAMP JAVA
Exercise 8: Levl7 (10 minutes)
1.
@Test
public void toHtml_Level7_unsupport() {
String input = "======= Level7 =======";
String expected = "======= Level7 =======";
String actual = target.toHtml(input);
assertThat(actual, is(expected));
}
2.
public class WikiEngineImpl implements WikiEngine {
static final Pattern HEADER_PATTERN = Pattern.compile("^(=+) .* (=+)$");
@Override
public String toHtml(String input) {
if (input == null) throw new IllegalArgumentException("input == null");
Matcher m = HEADER_PATTERN.matcher(input);
if (m.find()) {
String start = m.group(1);
String end = m.group(2);
if (start.length() < 7 && start.length() == end.length()) {
int level = start.length();
String body = input.substring(level + 1, input.length() - level - 1);
return "<h" + level + ">" + body + "</h" + level + ">";
}
}
return input;
}
}
13 / 14
- 14. PRE TDD BOOT CAMP JAVA
3.
public class WikiEngineImpl implements WikiEngine {
static final Pattern HEADER_PATTERN = Pattern.compile("^(=+) .* (=+)$");
static final int HEADER_MAX_LEVEL = 6;
@Override
public String toHtml(String input) {
if (input == null) throw new IllegalArgumentException("input == null");
Matcher m = HEADER_PATTERN.matcher(input);
if (m.find()) {
String start = m.group(1);
String end = m.group(2);
int level = start.length();
if (level <= HEADER_MAX_LEVEL && level == end.length()) {
String body = input.substring(level + 1, input.length() - level - 1);
return "<h" + level + ">" + body + "</h" + level + ">";
}
}
return input;
}
}
More Exercise:
Author:
Shuji Watanabe ( Java shuji.w6e@gmail.com
14 / 14