Your SlideShare is downloading. ×
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
TDD Hands-on
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

TDD Hands-on

1,316

Published on

TDDBC 札幌 0.5で使ったハンズオン資料です

TDDBC 札幌 0.5で使ったハンズオン資料です

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,316
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PRE TDD BOOT CAMP JAVATDD Boot CampIntroductionOverview 1 / 14
  • 2. PRE TDD BOOT CAMP JAVAExercise 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 JAVAExercise 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 JAVAExercise 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 JAVAExercise 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 JAVAExercise 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 JAVAExercise 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 JAVAExercise 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 JAVAExercise 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 JAVA3. @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 JAVAExercise 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 JAVA3. 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

×