Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88a0451

Browse files
Move option parsing to InstrumentationArguments
This removes some code duplication in CucumberInstrumentation and InstrumentationArguments.
1 parent 72500a0 commit 88a0451

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

android/src/main/java/cucumber/api/android/CucumberInstrumentation.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,34 +44,20 @@ public class CucumberInstrumentation extends Instrumentation {
4444
public static final String TAG = "cucumber-android";
4545

4646
private final Bundle results = new Bundle();
47-
private boolean justCount;
4847
private int testCount;
49-
private boolean coverage;
50-
private String coverageFilePath;
5148

5249
private RuntimeOptions runtimeOptions;
5350
private ResourceLoader resourceLoader;
5451
private ClassLoader classLoader;
5552
private Runtime runtime;
56-
private boolean debug;
5753
private List<CucumberFeature> cucumberFeatures;
54+
InstrumentationArguments instrumentationArguments;
5855

5956
@Override
6057
public void onCreate(Bundle arguments) {
6158
super.onCreate(arguments);
6259

63-
if (arguments != null) {
64-
debug = getBooleanArgument(arguments, "debug");
65-
boolean logOnly = getBooleanArgument(arguments, "log");
66-
if (logOnly && arguments.getString("dryRun") == null) {
67-
arguments.putString("dryRun", "true");
68-
}
69-
justCount = getBooleanArgument(arguments, "count");
70-
coverage = getBooleanArgument(arguments, "coverage");
71-
coverageFilePath = arguments.getString("coverageFile");
72-
}
73-
74-
InstrumentationArguments instrumentationArguments = new InstrumentationArguments(arguments);
60+
instrumentationArguments = new InstrumentationArguments(arguments);
7561

7662
Context context = getContext();
7763
classLoader = context.getClassLoader();
@@ -126,12 +112,12 @@ private DexFile newDexFile(String apkPath) {
126112
public void onStart() {
127113
Looper.prepare();
128114

129-
if (justCount) {
115+
if (instrumentationArguments.isCountEnabled()) {
130116
results.putString(Instrumentation.REPORT_KEY_IDENTIFIER, REPORT_VALUE_ID);
131117
results.putInt(REPORT_KEY_NUM_TOTAL, testCount);
132118
finish(Activity.RESULT_OK, results);
133119
} else {
134-
if (debug) {
120+
if (instrumentationArguments.isDebugEnabled()) {
135121
Debug.waitForDebugger();
136122
}
137123

@@ -150,7 +136,7 @@ public void onStart() {
150136

151137
printSummary();
152138

153-
if (coverage) {
139+
if (instrumentationArguments.isCoverageEnabled()) {
154140
generateCoverageReport();
155141
}
156142

@@ -168,11 +154,6 @@ private void printSummary() {
168154
}
169155
}
170156

171-
private boolean getBooleanArgument(Bundle arguments, String tag) {
172-
String tagString = arguments.getString(tag);
173-
return tagString != null && Boolean.parseBoolean(tagString);
174-
}
175-
176157
private void generateCoverageReport() {
177158
// use reflection to call emma dump coverage method, to avoid
178159
// always statically compiling against emma jar
@@ -209,6 +190,7 @@ private void generateCoverageReport() {
209190
}
210191

211192
private String getCoverageFilePath() {
193+
String coverageFilePath = instrumentationArguments.getCoverageFilePath();
212194
if (coverageFilePath == null) {
213195
return getTargetContext().getFilesDir().getAbsolutePath() + File.separator +
214196
DEFAULT_COVERAGE_FILE_NAME;

android/src/main/java/cucumber/runtime/android/InstrumentationArguments.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
* when the instrumentation was started.
88
*/
99
public final class InstrumentationArguments {
10+
private static final String KEY_DEBUG = "debug";
11+
private static final String KEY_LOG = "log";
12+
private static final String KEY_COUNT = "count";
13+
private static final String KEY_COVERAGE = "coverage";
14+
private static final String KEY_COVERAGE_FILE_PATH = "coverageFile";
1015
private static final String VALUE_SEPARATOR = "--";
1116

1217
private Bundle arguments;
@@ -56,6 +61,8 @@ public String getCucumberOptionsString() {
5661
appendOption(sb, "--name", arguments.getString(key));
5762
} else if ("dryRun".equals(key) && getBooleanArgument(key)) {
5863
appendOption(sb, "--dry-run", "");
64+
} else if ("log".equals(key) && getBooleanArgument(key)) {
65+
appendOption(sb, "--dry-run", "");
5966
} else if ("noDryRun".equals(key) && getBooleanArgument(key)) {
6067
appendOption(sb, "--no-dry-run", "");
6168
} else if ("monochrome".equals(key) && getBooleanArgument(key)) {
@@ -79,4 +86,24 @@ public String getCucumberOptionsString() {
7986
appendOption(sb, "", features);
8087
return sb.toString();
8188
}
89+
90+
public boolean isDebugEnabled() {
91+
return getBooleanArgument(KEY_DEBUG);
92+
}
93+
94+
public boolean isLogEnabled() {
95+
return getBooleanArgument(KEY_LOG);
96+
}
97+
98+
public boolean isCountEnabled() {
99+
return getBooleanArgument(KEY_COUNT);
100+
}
101+
102+
public boolean isCoverageEnabled() {
103+
return getBooleanArgument(KEY_COVERAGE);
104+
}
105+
106+
public String getCoverageFilePath() {
107+
return arguments.getString(KEY_COVERAGE_FILE_PATH);
108+
}
82109
}

0 commit comments

Comments
 (0)