Replace System Rules with System Lambda

System Lambda is more specific. It only wraps the part of the code that
produces the output.
This commit is contained in:
Stefan Birkner
2020-08-24 23:12:45 +02:00
parent 6d83ceba28
commit 3754c66604
4 changed files with 40 additions and 45 deletions

View File

@ -23,55 +23,48 @@
package com.iluwatar.subclasssandbox;
import com.github.stefanbirkner.systemlambda.Statement;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized;
/**
* GroundDive unit tests.
*/
public class GroundDiveTest {
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Test
public void testMove() {
log.clearLog();
public void testMove() throws Exception {
var groundDive = new GroundDive();
groundDive.move(1.0, 1.0, 1.0);
var outputLog = getLogContent(log.getLog());
var outputLog = getLogContent(() -> groundDive.move(1.0, 1.0, 1.0));
var expectedLog = "Move to ( 1.0, 1.0, 1.0 )";
Assert.assertEquals(outputLog, expectedLog);
}
@Test
public void testPlaySound() {
log.clearLog();
public void testPlaySound() throws Exception {
var groundDive = new GroundDive();
groundDive.playSound("SOUND_NAME", 1);
var outputLog = getLogContent(log.getLog());
var outputLog = getLogContent(() -> groundDive.playSound("SOUND_NAME", 1));
var expectedLog = "Play SOUND_NAME with volumn 1";
Assert.assertEquals(outputLog, expectedLog);
}
@Test
public void testSpawnParticles() {
log.clearLog();
public void testSpawnParticles() throws Exception {
var groundDive = new GroundDive();
groundDive.spawnParticles("PARTICLE_TYPE", 100);
final var outputLog = getLogContent(log.getLog());
final var outputLog = getLogContent(
() -> groundDive.spawnParticles("PARTICLE_TYPE", 100));
final var expectedLog = "Spawn 100 particle with type PARTICLE_TYPE";
Assert.assertEquals(outputLog, expectedLog);
}
@Test
public void testActivate() {
log.clearLog();
public void testActivate() throws Exception {
var groundDive = new GroundDive();
groundDive.activate();
var logs = log.getLog().split("\n");
var logs = tapSystemOutNormalized(groundDive::activate)
.split("\n");
final var expectedSize = 3;
final var log1 = logs[0].split("-")[1].trim() + " -" + logs[0].split("-")[2].trim();
final var expectedLog1 = "Move to ( 0.0, 0.0, -20.0 )";
@ -85,6 +78,11 @@ public class GroundDiveTest {
Assert.assertEquals(log3, expectedLog3);
}
private String getLogContent(Statement statement) throws Exception {
var log = tapSystemOutNormalized(statement);
return getLogContent(log);
}
private String getLogContent(String log) {
return log.split("-")[1].trim();
}

View File

@ -23,55 +23,47 @@
package com.iluwatar.subclasssandbox;
import com.github.stefanbirkner.systemlambda.Statement;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemOutRule;
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOutNormalized;
/**
* SkyLaunch unit tests.
*/
public class SkyLaunchTest {
@Rule
public SystemOutRule log = new SystemOutRule().enableLog();
@Test
public void testMove() {
log.clearLog();
public void testMove() throws Exception {
var skyLaunch = new SkyLaunch();
skyLaunch.move(1.0, 1.0, 1.0);
var outputLog = getLogContent(log.getLog());
var outputLog = getLogContent(() -> skyLaunch.move(1.0, 1.0, 1.0));
var expectedLog = "Move to ( 1.0, 1.0, 1.0 )";
Assert.assertEquals(outputLog, expectedLog);
}
@Test
public void testPlaySound() {
log.clearLog();
public void testPlaySound() throws Exception {
var skyLaunch = new SkyLaunch();
skyLaunch.playSound("SOUND_NAME", 1);
var outputLog = getLogContent(log.getLog());
var outputLog = getLogContent(() -> skyLaunch.playSound("SOUND_NAME", 1));
var expectedLog = "Play SOUND_NAME with volumn 1";
Assert.assertEquals(outputLog, expectedLog);
}
@Test
public void testSpawnParticles() {
log.clearLog();
public void testSpawnParticles() throws Exception {
var skyLaunch = new SkyLaunch();
skyLaunch.spawnParticles("PARTICLE_TYPE", 100);
var outputLog = getLogContent(log.getLog());
var outputLog = getLogContent(
() -> skyLaunch.spawnParticles("PARTICLE_TYPE", 100));
var expectedLog = "Spawn 100 particle with type PARTICLE_TYPE";
Assert.assertEquals(outputLog, expectedLog);
}
@Test
public void testActivate() {
log.clearLog();
public void testActivate() throws Exception {
var skyLaunch = new SkyLaunch();
skyLaunch.activate();
var logs = log.getLog().split("\n");
var logs = tapSystemOutNormalized(skyLaunch::activate)
.split("\n");
final var expectedSize = 3;
final var log1 = getLogContent(logs[0]);
final var expectedLog1 = "Move to ( 0.0, 0.0, 20.0 )";
@ -85,6 +77,11 @@ public class SkyLaunchTest {
Assert.assertEquals(log3, expectedLog3);
}
private String getLogContent(Statement statement) throws Exception {
var log = tapSystemOutNormalized(statement);
return getLogContent(log);
}
private String getLogContent(String log) {
return log.split("-")[1].trim();
}