Skip to content

rkdalsdn94/typescript-word-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

6 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ wordwrap ์„ ๊ตฌํ˜„์ด๋‹ค.
์—ฌ๊ธฐ ์ฝ”๋“œ๋ฅผ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

wordwrap

Clean Coders Episode 19 Advanced TDD Part 2 Examples

Getting Stuck

TDD๋ฅผ ์–ด๋А ์ •๋„ ํ•ด ๋ดค๋‹ค๋ฉด "getting stuck"์„ ๊ฒฝํ—˜ํ•ด ๋ดค์„ ๊ฒƒ์ด๋‹ค.

"getting stuck"์€

  • ํ˜„์žฌ ์‹คํŒจํ•˜๊ณ  ์žˆ๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์„ฑ๊ณต์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ ์ง„์ (incremental)์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์ด ์—†๊ณ 
  • ํ…Œ์ŠคํŠธ๋ฅผ ์„ฑ๊ณต์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„์ฃผ ๋งŽ์€ ์–‘์˜ ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•˜๊ณ 
  • ๊ทน๋‹จ์˜ ๊ฒฝ์šฐ ์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋‹ค์‹œ ์ž‘์„ฑํ•ด์•ผ๋งŒ

ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ผ์ปซ๋Š” ๊ธฐ์ˆ ์  ์šฉ์–ด์ด๋‹ค.

"getting stuck"์€ ๋ฌธ์ œ์˜ ์ง•ํ›„๋กœ์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— ๋ฐœ์ƒํ•œ๋‹ค.

  • ์ž˜๋ชป๋œ ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ–ˆ๊ฑฐ๋‚˜
  • ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๋ฅผ ๋„ˆ๋ฌด ๊ตฌ์ฒด์ (too specific not generic)ํ•˜๊ฒŒ ์ž‘์„ฑํ–ˆ๊ฑฐ๋‚˜
  • ์œ„์˜ ๋‘ ๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ–ˆ๊ฑฐ๋‚˜

์ด ์˜ˆ์ œ์—์„œ๋Š” word wrap์˜ ๊ฒฝ์šฐ๋กœ ์„ค๋ช…ํ•œ๋‹ค.

1. Test๋ฅผ ์ž‘์„ฑ

public class WrapperTest {
    @Test
    public void nothing() {
    }
}

2. wrap("word word", 4)

2.1 add failing test

@Test public void
should_wrap() {
    assertThat(wrap("word word", 4), is("word\nword"));
}

2.2 make it pass

private String wrap(String s, int width) {
    return s.replaceAll(" ", "\n");
}
  • with little golf game
  • ์ตœ์†Œํ•œ์˜ ํƒ€์ดํ•‘์œผ๋กœ ์„ฑ๊ณตํ•˜๋„๋ก ํ•œ๋‹ค.

3. wrap("a dog", 5)

3.1 add failing test

assertThat(wrap("a dog", 5), is("a dog"));

์ด ํ…Œ์ŠคํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‚˜๋‹ˆ ๋ญ˜ ํ•ด์•ผ ํ• ์ง€ ๋ฐ”๋กœ ๋–  ์˜ค๋ฅด์ง€ ์•Š๋Š”๋‹ค. ์ด๋•Œ ์กฐ์‹ฌํ•ด์•ผ ํ•œ๋‹ค. getting stuck๋  ๊ฒƒ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

3.2 make it pass

return s.length() > width ? s.replaceAll(" ", "\n") : s;

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ ๋“ฏ ํ•˜๋‹ค.

๊ทผ๋ฐ specific production code๋กœ ๋‘˜๋Ÿฌ ์Œ“์˜€๋‹ค. generalization factor๋ฅผ ์žƒ์—ˆ๋‹ค. ๋งŒ์ผ ๋นจ๋ฆฌ ์ด ๋ฌธ์ œ์—์„œ back out(์ฒ ํšŒ, ํƒˆํ‡ด, ์ทจ์†Œ)ํ•˜์ง€ ์•Š์œผ๋ฉด ์šฐ๋ฆฌ๋Š” ์ƒ๋‹นํžˆ stuck(๊ผผ์ง ๋‹ฌ์‹น ๋ชปํ•˜๋‹ค)ํ•˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

4. wrap("a dog with a bone", 6)

4.1 add failing test

assertThat(wrap("a dog with a bone", 6), is("a dog\nwidth a\nbone"));

4.1 make it pass

์–ด๋–ป๊ฒŒ ์ด ์ผ€์ด์Šค๋ฅผ ํŒจ์Šค์‹œํ‚ฌ๊นŒ ?

์ด๋ฏธ ๋Šฆ์—ˆ๋‹ค. ํƒˆ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

์ •๋ง stuck(๊ผผ์ง ๋‹ฌ์‹น ๋ชปํ•˜๋Š”) ์ƒํƒœ์— ๋น ์กŒ๋‹ค.

์ด์ œ ๋ช…ํ™•ํ•ด ์กŒ๋‹ค. ์ด ํ…Œ์ŠคํŠธ๋ฅผ ํŒจ์Šค์‹œํ‚ค๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋‹ค์‹œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ ๋ฟ์ด๋‹ค. ์ง€๊ธˆ๊ป ์ž‘์„ฑํ•œ ๋ชจ๋“  ๊ฒƒ์„ ๋ฒ„๋ฆฌ๊ณ  ์ฒ˜์Œ๋ถ€ํ„ฐ ๋‹ค์‹œ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

5. Getting Unstuck

  • most degenerate test case๋ฅผ ๋จผ์ € ์ž‘์„ฑํ•˜๊ณ 
  • ์ด ๋ณต์žกํ•จ ํ€ด์ฆˆ๋ฅผ ์•„์ฃผ ์ž‘์€ ์Šคํ…์”ฉ ์˜ฌ๋ผ๊ฐ€์ž.
  • ๊ฐ ๊ฒฝ์šฐ์—์„œ ํ•ด๋‹น ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผ์‹œํ‚ค๋„๋ก specificํ•œ fix๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ
  • production code๋ฅผ generalizingํ•˜์—ฌ ํ…Œ์ŠคํŠธ๊ฐ€ ํ†ต๊ณผ๋˜๋„๋ก ํ•˜์ž.

6. wrap(null, 1)

6.1 add failing test

assertThat(wrap(null, 1), is(""));

6.2 make it pass

return null; ๋Œ€์‹  return "";๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์„ฑ๊ณต์‹œํ‚ด

7. wrap(โ€œโ€, 1)

7.2 add failing test

assertThat(wrap("", 1), is(""));

8. wrap(โ€œxโ€, 1)

์ด์ œ ์ƒ๊ฐ๋‚˜๋Š” most degenerate test case๋Š” wrap(โ€œxโ€, 1)์ด๋‹ค.

8.1 add failing test

assertThat(wrap("x", 1), is("x"));

8.2 make it pass

private String wrap(String s, int width) {
    if(s == null)
        return "";
    return s;
}

8.3 refactor

assertThat(wrap(null, 1), is("")); ๊ตฌ์กฐ๊ฐ€ ๋ฐ˜๋ณต(3๋ฒˆ)๋œ๋‹ค.

  • null -> s, 1 -> width, "" -> expected๋กœ extract variable
  • assertWraps(s, width, expected)๋กœ extract methodํ•œ๋‹ค.
  • ๋™์ผํ•œ ๊ตฌ์กฐ๋ฅผ ๋‹ค๋ฅธ 2 ๋ผ์ธ๋„ extract method๊ฐ€ ์ ์šฉ๋˜๋„๋ก ํ•œ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  s, width, expected๋ฅผ inlineํ•œ๋‹ค.
  • assertWraps ๋ฉ”์†Œ๋“œ๋ฅผ should_wrap ํ…Œ์ŠคํŠธ ๋ฉ”์†Œ๋“œ ์œ„๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
private void assertWraps(String s, int width, String expected) {
    assertThat(wrap(s, width), is(expected));
}

@Test
public void
should_wrap() {
    assertWraps(null, 1, "");
    assertWraps("", 1, "");
    assertWraps("x", 1, "x");
}

9.assertWraps("xx", 1, "x\nx")

first, simplest condition that can break๋ฅผ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

9.1 add failing test

add failing test for assertWraps("xx", 1, "x\nx");

assertWraps("xx", 1, "x\nx");

9.2 make it pass

private String wrap(String s, int width) {
    if(s == null)
        return "";
    if(s.length() <= width)
        return s;
    else
        return s.substring(0, width) + "\n" + s.substring(width);
}

10. assertWraps("xxx", 1)

10.1 add failing test

add failing test for multiple lines("xxx", 1)

10.2 make it work

private String wrap(String s, int width) {
    if(s == null)
        return "";
    if(s.length() <= width)
        return s;
    else
        return s.substring(0, width) + "\n" + wrap(s.substring(width), width);
}

11. assertWraps("x x", 1, "x\nx")

11.1 add failing test

space๊ฐ€ ํฌํ•จ๋œ ๊ฒฝ์šฐ์— ๋Œ€ํ•œ ์‹คํŒจํ•˜๋Š” ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€

assertWraps("x x", 1, "x\nx");

11.2 make it pass

return s.substring(0, width) + "\n" + wrap(s.substring(width).trim(), width);

trim()์„ ์ ์šฉ

12.assertWraps("x xx", 3, "x\nxx");

12.1 add failing test

  • width๊ฐ€ ๊ณต๋ฐฑ ๋’ค์— ์˜ค๋Š” ๋‹จ์–ด์˜ ์ผ๋ถ€๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ
assertWraps("x xx", 3, "x\nxx");

12.2 make it work

private String wrap(String s, int width) {
    if(s == null)
        return "";
    if(s.length() <= width)
        return s;
    else {
        int breakPoint = s.lastIndexOf(" ", width);
        if(breakPoint == -1)
            breakPoint = width;
        return s.substring(0, breakPoint) + "\n" + wrap(s.substring(breakPoint).trim(), width);
    }
}

13. complex case

13.1 add failing test

assertWraps("four score and seven years ago our fathers brought forth upon this continent", 7, "four\nscore\nand\nseven\nyears\nago our\nfathers\nbrought\nforth\nupon\nthis\ncontine\nnt");

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors