コード例 #1
0
  /**
   * Use forEach, with the given StringBuilder, to assemble a String which represents every Shape in
   * the given list.
   *
   * <p>Change the 'method under test' to make the test pass.
   *
   * <p>The String should be a concatenation of each Shape's toString.
   *
   * @see Shapes#makeStringOfAllColors(java.util.List, StringBuilder)
   * @see Shape#toString()
   * @see StringBuilder#append(String)
   * @see Iterable#forEach
   */
  @Test
  public void buildStringRepresentingAllShapes() {
    List<Shape> allMyShapes = Arrays.asList(new Shape(RED), new Shape(BLACK), new Shape(YELLOW));
    StringBuilder builder = new StringBuilder();

    // method under test
    Shapes.makeStringOfAllColors(allMyShapes, builder);

    assertThat(builder.toString(), equalTo("[a RED shape][a BLACK shape][a YELLOW shape]"));
  }
コード例 #2
0
  /**
   * Use forEach to change the color of every shape to RED.
   *
   * <p>Change the 'method under test' to make the test pass.
   *
   * @see Shapes#colorAll(java.util.List, Color)
   * @see Iterable#forEach
   * @see Shape#setColor(Color)
   */
  @Test
  public void changeColorOfAllShapes() {
    List<Shape> myShapes = Arrays.asList(new Shape(BLUE), new Shape(BLACK), new Shape(YELLOW));

    // method under test
    Shapes.colorAll(myShapes, RED);

    assertThat(myShapes, hasSize(3));
    assertThat(myShapes, everyItem(hasColor(RED)));
  }
コード例 #3
0
  /**
   * Use forEach to change the color, and build a string showing the old colors of each shape.
   *
   * <p>Try to perform both actions using a single call to forEach.
   *
   * @see Shapes#changeColorAndMakeStringOfOldColors
   * @see Shape#getColor()
   * @see Color#name()
   * @see StringBuilder#append(String)
   */
  @Test
  public void changeColorOfAllShapes_AND_buildStringShowingAllTheOldColors() {
    List<Shape> myShapes = Arrays.asList(new Shape(BLUE), new Shape(BLACK), new Shape(YELLOW));
    StringBuilder builder = new StringBuilder();

    Shapes.changeColorAndMakeStringOfOldColors(myShapes, RED, builder);

    assertThat(myShapes, hasSize(3));
    assertThat(myShapes, everyItem(hasColor(RED)));
    assertThat(builder.toString(), equalTo("[a BLUE shape][a BLACK shape][a YELLOW shape]"));
  }