Skip to content

Commit

Permalink
Merge pull request #17 from Team488/test-only-exceptions
Browse files Browse the repository at this point in the history
Add ability to write test-only assertions in common code
  • Loading branch information
WasabiFan committed Dec 19, 2015
2 parents bcef16c + 106cdcf commit fc49c08
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/xbot/common/command/BaseSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public void setDefaultCommand(Command command) {

@Override
protected void initDefaultCommand() {
// TODO Auto-generated method stub

}

Expand Down
3 changes: 3 additions & 0 deletions src/xbot/common/injection/RobotModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import xbot.common.command.SmartDashboardCommandPutter;
import xbot.common.injection.wpi_factories.RealWPIFactory;
import xbot.common.injection.wpi_factories.WPIFactory;
import xbot.common.logging.RobotAssertionManager;
import xbot.common.logging.SilentRobotAssertionManager;
import xbot.common.properties.DatabaseStorageBase;
import xbot.common.properties.ITableProxy;
import xbot.common.properties.RobotDatabaseStorage;
Expand All @@ -19,6 +21,7 @@ protected void configure() {
this.bind(ITableProxy.class).to(SmartDashboardTableWrapper.class);
this.bind(DatabaseStorageBase.class).to(RobotDatabaseStorage.class);
this.bind(SmartDashboardCommandPutter.class).to(RealSmartDashboardCommandPutter.class);
this.bind(RobotAssertionManager.class).to(SilentRobotAssertionManager.class);
}

}
15 changes: 15 additions & 0 deletions src/xbot/common/logging/LoudRobotAssertionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xbot.common.logging;

public class LoudRobotAssertionManager extends RobotAssertionManager {

@Override
protected void handlePlatformException(RuntimeException e) {
throw e;
}

@Override
public boolean isExceptionsEnabled() {
return true;
}

}
7 changes: 7 additions & 0 deletions src/xbot/common/logging/RobotAssertionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package xbot.common.logging;

public class RobotAssertionException extends RuntimeException {
public RobotAssertionException(String failureCauseCause) {
super("Assertion error: " + failureCauseCause);
}
}
35 changes: 35 additions & 0 deletions src/xbot/common/logging/RobotAssertionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package xbot.common.logging;

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

import org.apache.log4j.Logger;

public abstract class RobotAssertionManager {
static Logger log = Logger.getLogger(RobotAssertionManager.class);

public final void throwException(RuntimeException e) {
log.error("Safe exception encountered (exception throw " + (this.isExceptionsEnabled() ? "enabled" : "disabled") + "): "
+ e.getMessage());
log.error("Stack trace: \n "
+ Arrays.stream(e.getStackTrace())
.map(elem -> elem.toString())
.collect(Collectors.joining("\n ")));

handlePlatformException(e);
}

public final void throwException(String message, Throwable cause) {
throwException(new RuntimeException(message, cause));
}

protected abstract void handlePlatformException(RuntimeException e);

public abstract boolean isExceptionsEnabled();

public final void assertTrue(boolean value, String assertionFaliureCause) {
if(!value) {
throwException(new RobotAssertionException(assertionFaliureCause));
}
}
}
15 changes: 15 additions & 0 deletions src/xbot/common/logging/SilentRobotAssertionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xbot.common.logging;

public class SilentRobotAssertionManager extends RobotAssertionManager {

@Override
protected void handlePlatformException(RuntimeException e) {
// Don't do anything: we don't need to throw
}

@Override
public boolean isExceptionsEnabled() {
return false;
}

}
1 change: 1 addition & 0 deletions tests/xbot/common/injection/BaseWPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.google.inject.Injector;

import xbot.common.controls.MockRobotIO;
import xbot.common.logging.RobotAssertionManager;
import xbot.common.properties.PropertyManager;
import edu.wpi.first.wpilibj.HLUsageReporting;
import edu.wpi.first.wpilibj.MockHLUsageReporting;
Expand Down
4 changes: 4 additions & 0 deletions tests/xbot/common/injection/UnitTestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import xbot.common.command.SmartDashboardCommandPutter;
import xbot.common.injection.wpi_factories.MockWPIFactory;
import xbot.common.injection.wpi_factories.WPIFactory;
import xbot.common.logging.LoudRobotAssertionManager;
import xbot.common.logging.RobotAssertionManager;
import xbot.common.properties.DatabaseStorageBase;
import xbot.common.properties.ITableProxy;
import xbot.common.properties.TableProxy;
Expand All @@ -28,5 +30,7 @@ protected void configure() {
this.bind(DatabaseStorageBase.class).to(OffRobotDatabaseStorage.class).in(Singleton.class);

this.bind(SmartDashboardCommandPutter.class).to(MockSmartDashboardCommandPutter.class);

this.bind(RobotAssertionManager.class).to(LoudRobotAssertionManager.class);
}
}
44 changes: 44 additions & 0 deletions tests/xbot/common/logging/SafeRobotAssertionTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package xbot.common.logging;

import static org.junit.Assert.*;
import org.junit.Test;

import xbot.common.injection.BaseWPITest;

public class SafeRobotAssertionTests extends BaseWPITest {
@Test
public void testNoExceptionOnRobot() {
RobotAssertionManager assertMan = new SilentRobotAssertionManager();

assertMan.throwException(new RuntimeException("Something really bad happened (...but robots never die)"));
}

@Test(expected=RuntimeException.class)
public void testExceptionThrownInTests() {
RobotAssertionManager assertMan = new LoudRobotAssertionManager();

assertMan.throwException(new RuntimeException("Something really bad happened (tests are free to die as necessary)"));
}

@Test
public void testAssertionContinuesOnRobot() {
RobotAssertionManager assertMan = new SilentRobotAssertionManager();

assertMan.assertTrue(true, "The world is ending");
assertMan.assertTrue(false, "false != true");
}

@Test(expected=RobotAssertionException.class)
public void testAssertionFailedInTests() {
RobotAssertionManager assertMan = new LoudRobotAssertionManager();

assertMan.assertTrue(false, "false != true");
}

@Test()
public void testAssertionPassedInTests() {
RobotAssertionManager assertMan = new LoudRobotAssertionManager();

assertMan.assertTrue(true, "The world is ending");
}
}

0 comments on commit fc49c08

Please sign in to comment.