Permalink
Browse files

[tdd-diamond] Refactor: introduced packages to separate api from impl…

…ementation
  • Loading branch information...
michaelszymczak committed Apr 9, 2017
1 parent 169792f commit 8da449a27a1ae385f655a557e8e81f3a9ad3441f
Showing with 116 additions and 90 deletions.
  1. +4 −1 tdd-diamond/src/main/java/com/michaelszymczak/diamond/App.java
  2. +5 −0 tdd-diamond/src/main/java/com/michaelszymczak/diamond/api/Diamond.java
  3. +1 −1 tdd-diamond/src/main/java/com/michaelszymczak/diamond/{ → api}/Letter.java
  4. +13 −0 tdd-diamond/src/main/java/com/michaelszymczak/diamond/app/DiamondFactory.java
  5. +9 −5 ...java/com/michaelszymczak/diamond/{Diamond.java → implementation/AllLettersContainingDiamond.java}
  6. +2 −9 tdd-diamond/src/main/java/com/michaelszymczak/diamond/{ → implementation}/Board.java
  7. +4 −2 tdd-diamond/src/main/java/com/michaelszymczak/diamond/{ → implementation}/Coordinates.java
  8. +8 −8 tdd-diamond/src/main/java/com/michaelszymczak/diamond/{ → implementation}/Layout.java
  9. +5 −2 tdd-diamond/src/main/java/com/michaelszymczak/diamond/{ → implementation}/PositionedLetter.java
  10. +1 −1 tdd-diamond/src/main/java/com/michaelszymczak/diamond/{ → support}/ValueObject.java
  11. +2 −2 tdd-diamond/src/test/groovy/com/michaelszymczak/diamond/AppTest.groovy
  12. +0 −46 tdd-diamond/src/test/groovy/com/michaelszymczak/diamond/BoardShould.groovy
  13. +6 −4 tdd-diamond/src/test/groovy/com/michaelszymczak/diamond/DiamondAcceptanceTest.groovy
  14. +50 −0 tdd-diamond/src/test/groovy/com/michaelszymczak/diamond/implementation/BoardShould.groovy
  15. +4 −7 tdd-diamond/src/test/groovy/com/michaelszymczak/diamond/{ → implementation}/LayoutShould.groovy
  16. +2 −2 tdd-diamond/src/test/groovy/com/michaelszymczak/diamond/{ → implementation}/LetterShould.groovy
@@ -1,8 +1,11 @@
package com.michaelszymczak.diamond;

import com.michaelszymczak.diamond.api.Letter;
import com.michaelszymczak.diamond.app.DiamondFactory;

public class App {
public String getGreeting() {
return "Hello Diamond.";
return new DiamondFactory().createFor(Letter.G).rendered();
}

public static void main(String[] args) {
@@ -0,0 +1,5 @@
package com.michaelszymczak.diamond.api;

public interface Diamond {
String rendered();
}
@@ -1,4 +1,4 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.api;

import java.util.List;
import java.util.stream.Collectors;
@@ -0,0 +1,13 @@
package com.michaelszymczak.diamond.app;

import com.michaelszymczak.diamond.api.Diamond;
import com.michaelszymczak.diamond.api.Letter;
import com.michaelszymczak.diamond.implementation.AllLettersContainingDiamond;

public class DiamondFactory {

public Diamond createFor(Letter letter)
{
return AllLettersContainingDiamond.of(letter);
}
}
@@ -1,20 +1,24 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.implementation;

import com.michaelszymczak.diamond.api.Diamond;
import com.michaelszymczak.diamond.api.Letter;

import java.util.List;
import java.util.stream.Collectors;

public class Diamond {
public class AllLettersContainingDiamond implements Diamond {

private Letter letter;

public static Diamond of(Letter letter) {
return new Diamond(letter);
public static AllLettersContainingDiamond of(Letter letter) {
return new AllLettersContainingDiamond(letter);
}

private Diamond(Letter letter) {
private AllLettersContainingDiamond(Letter letter) {
this.letter = letter;
}

@Override
public String rendered() {
return new Board(allPositionedLetters()).toString();
}
@@ -1,20 +1,14 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.implementation;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;

public class Board {
class Board {

private final PositionedLetter[][] board;
private final String emptyCell;

public Board(PositionedLetter... cells) {
this(" ", asList(cells));
}

public Board(Collection<PositionedLetter> cells) {
this(" ", cells);
}
@@ -24,7 +18,6 @@ public Board(String emptyCell, Collection<PositionedLetter> cells) {
this.board = boardWith(cells);
}


private static PositionedLetter[][] boardWith(Collection<PositionedLetter> cells) {
int maxCellPosition = cells.stream()
.mapToInt(PositionedLetter::maxXorY)
@@ -1,6 +1,8 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.implementation;

public class Coordinates extends ValueObject {
import com.michaelszymczak.diamond.support.ValueObject;

class Coordinates extends ValueObject {

private final int y;
private final int x;
@@ -1,13 +1,13 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.implementation;

import com.google.common.collect.ImmutableSet;
import com.michaelszymczak.diamond.api.Letter;

import java.util.Set;

import static com.michaelszymczak.diamond.Coordinates.ofYX;
import static com.michaelszymczak.diamond.Letter.A;
import static com.michaelszymczak.diamond.implementation.Coordinates.ofYX;

public class Layout {
class Layout {

private final Letter lastLetter;

@@ -19,19 +19,19 @@ private Layout(Letter lastLetter) {
this.lastLetter = lastLetter;
}

public int yOfTop(Letter letter) {
int yOfTop(Letter letter) {
return letter.ordinal();
}

public int yOfBottom(Letter letter) {
int yOfBottom(Letter letter) {
return lastLetter.ordinal() + lastLetter.ordinal() - letter.ordinal();
}

public int xOfLeft(Letter letter) {
int xOfLeft(Letter letter) {
return lastLetter.ordinal() - letter.ordinal();
}

public int xOfRight(Letter letter) {
int xOfRight(Letter letter) {
return lastLetter.ordinal() + letter.ordinal();
}

@@ -1,6 +1,9 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.implementation;

public class PositionedLetter extends ValueObject {
import com.michaelszymczak.diamond.api.Letter;
import com.michaelszymczak.diamond.support.ValueObject;

class PositionedLetter extends ValueObject {
private final Coordinates coordinates;
private final Letter letter;

@@ -1,4 +1,4 @@
package com.michaelszymczak.diamond;
package com.michaelszymczak.diamond.support;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
@@ -3,14 +3,14 @@ package com.michaelszymczak.diamond
import spock.lang.Specification

class AppTest extends Specification {
def "application has a greeting"() {
def "application prints diamond"() {
setup:
def app = new App()

when:
def result = app.greeting

then:
result != null
result.contains("G")
}
}

This file was deleted.

Oops, something went wrong.
@@ -1,25 +1,27 @@
package com.michaelszymczak.diamond

import com.michaelszymczak.diamond.api.Letter
import com.michaelszymczak.diamond.app.DiamondFactory
import spock.lang.Specification

class DiamondAcceptanceTest extends Specification {

def "contains one letter if it is the first letter"() {
expect:
Diamond.of(Letter.A).rendered() == "A"
new DiamondFactory().createFor(Letter.A).rendered() == "A"
}

def "uses all letters up to the provided one"() {
expect:
Diamond.of(Letter.B).rendered() == "" +
new DiamondFactory().createFor(Letter.B).rendered() == "" +
" A " + "\n" +
"B B" + "\n" +
" A "
}

def "creates diamond-like shape"() {
expect:
Diamond.of(Letter.C).rendered() == "" +
new DiamondFactory().createFor(Letter.C).rendered() == "" +
" A " + "\n" +
" B B " + "\n" +
"C C" + "\n" +
@@ -29,7 +31,7 @@ class DiamondAcceptanceTest extends Specification {

def "uses all letters if possible"() {
expect:
Diamond.of(Letter.Z).rendered() == "" +
new DiamondFactory().createFor(Letter.Z).rendered() == "" +
" A " + "\n" +
" B B " + "\n" +
" C C " + "\n" +
@@ -0,0 +1,50 @@
package com.michaelszymczak.diamond.implementation

import spock.lang.Specification

import static com.michaelszymczak.diamond.api.Letter.*
import static com.michaelszymczak.diamond.implementation.Coordinates.ofYX

class BoardShould extends Specification {

def "should print a symbol"() {
expect: new Board([new PositionedLetter(ofYX(0,0),B)]).toString() == "B"
}

def "should print symbols respecting their positions"() {
expect:
new Board([
new PositionedLetter(ofYX(0,0),A),
new PositionedLetter(ofYX(0,1),B),
new PositionedLetter(ofYX(1,0),C),
new PositionedLetter(ofYX(1,1),D),
]).toString() == shapeOf("""
AB
CD
""")
}

def "should fill gaps with defined symbol "() {
expect:
new Board("_", [
new PositionedLetter(ofYX(0,2),A),
new PositionedLetter(ofYX(1,1),B),
new PositionedLetter(ofYX(1,3),B),
new PositionedLetter(ofYX(2,0),C),
new PositionedLetter(ofYX(2,4),C),
new PositionedLetter(ofYX(3,1),B),
new PositionedLetter(ofYX(3,3),B),
new PositionedLetter(ofYX(4,2),A),
]).toString() == shapeOf("""
__A__
_B_B_
C___C
_B_B_
__A__
""")
}

private static String shapeOf(String shape) {
shape.replaceAll("^\n", "").replaceAll("\n\$", "")
}
}
@@ -1,13 +1,10 @@
package com.michaelszymczak.diamond
package com.michaelszymczak.diamond.implementation

import spock.lang.Specification

import static com.michaelszymczak.diamond.Coordinates.ofYX
import static com.michaelszymczak.diamond.Layout.forLastLetterBeing
import static com.michaelszymczak.diamond.Letter.A
import static com.michaelszymczak.diamond.Letter.B
import static com.michaelszymczak.diamond.Letter.C
import static com.michaelszymczak.diamond.Letter.D
import static com.michaelszymczak.diamond.api.Letter.*
import static com.michaelszymczak.diamond.implementation.Coordinates.ofYX
import static com.michaelszymczak.diamond.implementation.Layout.forLastLetterBeing

class LayoutShould extends Specification {

@@ -1,8 +1,8 @@
package com.michaelszymczak.diamond
package com.michaelszymczak.diamond.implementation

import spock.lang.Specification

import static com.michaelszymczak.diamond.Letter.*
import static com.michaelszymczak.diamond.api.Letter.*

class LetterShould extends Specification {

0 comments on commit 8da449a

Please sign in to comment.