diff --git a/src/checkers/inference/InferenceLauncher.java b/src/checkers/inference/InferenceLauncher.java index 4c03a107..3301dc2a 100644 --- a/src/checkers/inference/InferenceLauncher.java +++ b/src/checkers/inference/InferenceLauncher.java @@ -53,19 +53,7 @@ protected void initInferenceOptions(String[] args) { public void launch(String[] args) { initInferenceOptions(args); - Mode mode = null; - try { - mode = Mode.valueOf(InferenceOptions.mode); - - } catch (IllegalArgumentException iexc) { - outStream.println( - "Could not recognize mode: " - + InferenceOptions.mode - + "\n" - + "valid modes: " - + StringsPlume.join(", ", Mode.values())); - System.exit(1); - } + Mode mode = InferenceOptions.mode; switch (mode) { case TYPECHECK: @@ -221,7 +209,7 @@ public void infer() { addIfTrue("--hacks", InferenceOptions.hacks, argList); - Mode mode = Mode.valueOf(InferenceOptions.mode); + Mode mode = InferenceOptions.mode; if (InferenceOptions.makeDefaultsExplicit && (mode == Mode.ROUNDTRIP || mode == Mode.ROUNDTRIP_TYPECHECK)) { // Two conditions have to be met to make defaults explicit: diff --git a/src/checkers/inference/InferenceOptions.java b/src/checkers/inference/InferenceOptions.java index e187161d..54a3b133 100644 --- a/src/checkers/inference/InferenceOptions.java +++ b/src/checkers/inference/InferenceOptions.java @@ -38,10 +38,8 @@ public class InferenceOptions { // ------------------------------------------------------ @OptionGroup("General Options") - - // TODO: The mode variable should be an enum rather than a string. @Option(value = "-m Modes of operation: TYPECHECK, INFER, ROUNDTRIP, ROUNDTRIP_TYPECHECK") - public static String mode; + public static Mode mode; @Option("Should we log certain exceptions rather than crash") public static boolean hacks; @@ -185,21 +183,7 @@ public static InitStatus init(String[] args, boolean requireMode) { } } else { - mode = mode.toUpperCase(); - - Mode modeEnum = null; - try { - modeEnum = Mode.valueOf(InferenceOptions.mode); - - } catch (IllegalArgumentException iexc) { - System.out.println( - "Could not recognize mode: " - + InferenceOptions.mode - + "\n" - + "valid modes: " - + StringsPlume.join(", ", Mode.values())); - System.exit(1); - } + Mode modeEnum = InferenceOptions.mode; if (modeEnum != Mode.TYPECHECK) { if (solver == null) { diff --git a/tests/checkers/inference/InferenceOptionsTest.java b/tests/checkers/inference/InferenceOptionsTest.java new file mode 100644 index 00000000..99d7149e --- /dev/null +++ b/tests/checkers/inference/InferenceOptionsTest.java @@ -0,0 +1,84 @@ +package checkers.inference; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; + +import checkers.inference.InferenceLauncher.Mode; + +public class InferenceOptionsTest { + + @Before + public void resetOptions() { + InferenceOptions.mode = null; + InferenceOptions.hacks = false; + InferenceOptions.typesystem = null; + InferenceOptions.jaifFile = InferenceOptions.DEFAULT_JAIF; + InferenceOptions.checker = null; + InferenceOptions.solver = null; + InferenceOptions.targetclasspath = "."; + InferenceOptions.solverArgs = null; + InferenceOptions.cfArgs = null; + InferenceOptions.jsonFile = null; + InferenceOptions.pathToAfuScripts = null; + InferenceOptions.afuOutputDir = null; + InferenceOptions.inPlace = false; + InferenceOptions.afuOptions = null; + InferenceOptions.makeDefaultsExplicit = false; + InferenceOptions.version = false; + InferenceOptions.help = false; + InferenceOptions.logLevel = null; + InferenceOptions.printCommands = false; + InferenceOptions.debug = null; + InferenceOptions.javacOptions = new ArrayList(); + InferenceOptions.javaFiles = new String[0]; + } + + @Test + public void parsesUppercaseMode() { + InferenceOptions.InitStatus status = + InferenceOptions.init( + new String[] { + "--mode=TYPECHECK", "--checker", ostrusted.OsTrustedChecker.class.getName() + }, + true); + + assertTrue(status.errors.toString(), status.errors.isEmpty()); + assertEquals(Mode.TYPECHECK, InferenceOptions.mode); + } + + @Test + public void parsesLowercaseMode() { + InferenceOptions.InitStatus status = + InferenceOptions.init( + new String[] { + "--mode=typecheck", "--checker", ostrusted.OsTrustedChecker.class.getName() + }, + true); + + assertTrue(status.errors.toString(), status.errors.isEmpty()); + assertEquals(Mode.TYPECHECK, InferenceOptions.mode); + } + + @Test + public void parsesHyphenatedMode() { + InferenceOptions.InitStatus status = + InferenceOptions.init( + new String[] { + "--mode=roundtrip-typecheck", + "--checker", + ostrusted.OsTrustedChecker.class.getName(), + "--jsonFile", + "constraints.json", + "--inPlace" + }, + true); + + assertTrue(status.errors.toString(), status.errors.isEmpty()); + assertEquals(Mode.ROUNDTRIP_TYPECHECK, InferenceOptions.mode); + } +}