Skip to content

Instantly share code, notes, and snippets.

@verhas
Created November 22, 2017 12:03
Show Gist options
  • Select an option

  • Save verhas/52bc9ec9d44ac3ae972a416d00d7f46e to your computer and use it in GitHub Desktop.

Select an option

Save verhas/52bc9ec9d44ac3ae972a416d00d7f46e to your computer and use it in GitHub Desktop.

Revisions

  1. verhas created this gist Nov 22, 2017.
    88 changes: 88 additions & 0 deletions TestOneNumberGame.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    package arpad.kosa;

    import org.junit.Assert;
    import org.junit.Test;

    import java.io.*;

    public class TestOneNumberGame {
    private static final String SAMPLE_GAME = "1 1 2 3 5 4 Arpad\n1 1 2 3 5 3 Elod\n2 2 2 2 2 2 Ond";
    private static final String SAMPLE_GAME_NO_ONE = "3 1 2 3 5 4 Arpad\n3 1 2 3 5 3 Elod\n2 2 2 2 2 2 Ond";
    private static final String SAMPLE_GAME_INVALID = "1 3 1 2 3 5 4 Arpad\n3 1 2 3 5 3 Elod\n2 2 2 2 2 2 Ond";

    @Test
    public void sampleRun() throws IOException {
    OneNumberGame sut = new OneNumberGame();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StringWriter stringWriter = new StringWriter();
    runGame(sut, baos, stringWriter, 2,SAMPLE_GAME);

    Assert.assertEquals("3. feladat: a jatekosok szama: 3" +
    "4. feladat: a fordulok szama: 6" +
    "5. feladat: az elso forduloban volt egyes" +
    "6. feladat: a legnagyobb tipp: 5" +
    "7. feladat: Kerem a fordulo sorszamat: [1-6]" +
    "7. feladat: A nyertes tipp a megadott forduloban: 2" +
    "8. feladat: A megadott fordulo nyertese: Ond",
    baos.toString("utf-8").replaceAll("[\\n\\r]", ""));
    Assert.assertEquals("Fordulo sorszama: 2Nyertes tipp: 2" +
    "Nyertes players: Ond", stringWriter.toString().replaceAll("[\\n\\r]", ""));
    }

    @Test
    public void sampleRunNoWinner() throws IOException {
    OneNumberGame sut = new OneNumberGame();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StringWriter stringWriter = new StringWriter();
    runGame(sut, baos, stringWriter, 3,SAMPLE_GAME);

    Assert.assertEquals("3. feladat: a jatekosok szama: 3" +
    "4. feladat: a fordulok szama: 6" +
    "5. feladat: az elso forduloban volt egyes" +
    "6. feladat: a legnagyobb tipp: 5" +
    "7. feladat: Kerem a fordulo sorszamat: [1-6]" +
    "7. feladat: A megadott forduloban nem volt nyertes. " +
    "A 8. feladat: a megadott forduloban nem volt nyertes. ",
    baos.toString("utf-8").replaceAll("[\\n\\r]", ""));
    Assert.assertEquals("", stringWriter.toString().replaceAll("[\\n\\r]", ""));
    }

    @Test
    public void sampleRunNoOneInFirstTournament() throws IOException {
    OneNumberGame sut = new OneNumberGame();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StringWriter stringWriter = new StringWriter();
    runGame(sut, baos, stringWriter, 3,SAMPLE_GAME_NO_ONE);

    Assert.assertEquals("3. feladat: a jatekosok szama: 3" +
    "4. feladat: a fordulok szama: 6" +
    "5. feladat: az elso forduloban NEM volt egyes" +
    "6. feladat: a legnagyobb tipp: 5" +
    "7. feladat: Kerem a fordulo sorszamat: [1-6]" +
    "7. feladat: A megadott forduloban nem volt nyertes. " +
    "A 8. feladat: a megadott forduloban nem volt nyertes. ",
    baos.toString("utf-8").replaceAll("[\\n\\r]", ""));
    Assert.assertEquals("", stringWriter.toString().replaceAll("[\\n\\r]", ""));
    }

    @Test(expected = IllegalArgumentException.class)
    public void invalidGameThrowsException() throws IOException {
    OneNumberGame sut = new OneNumberGame();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final StringWriter stringWriter = new StringWriter();
    runGame(sut, baos, stringWriter, 3,SAMPLE_GAME_INVALID);
    }

    private void runGame(final OneNumberGame sut,
    final ByteArrayOutputStream baos,
    final StringWriter stringWriter,
    final int index,
    final String game) throws IOException {
    try (final BufferedReader reader = new BufferedReader(new StringReader(game));
    final BufferedWriter writer = new BufferedWriter(stringWriter);
    final InputStream in = new ByteArrayInputStream((index + "\n").getBytes("utf-8"));
    final PrintStream out = new PrintStream(baos, true, "utf-8");) {
    sut.play(reader, writer, in, out);
    }
    }
    }