Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Unified Diff: testing/android/java/src/org/chromium/testing/local/GtestFilter.java

Issue 574433003: [Android] JUnit runner + gyp changes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@deps-changes
Patch Set: rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: testing/android/java/src/org/chromium/testing/local/GtestFilter.java
diff --git a/testing/android/java/src/org/chromium/testing/local/GtestFilter.java b/testing/android/java/src/org/chromium/testing/local/GtestFilter.java
new file mode 100644
index 0000000000000000000000000000000000000000..2fac9cfcd23ca322d58e028d38c59ab7cc30b878
--- /dev/null
+++ b/testing/android/java/src/org/chromium/testing/local/GtestFilter.java
@@ -0,0 +1,95 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.testing.local;
+
+import org.junit.runner.Description;
+import org.junit.runner.manipulation.Filter;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+/**
+ * Filters tests based on a googletest-style filter string.
+ */
+class GtestFilter extends Filter {
nyquist 2014/09/19 01:26:03 I know this is meta, but is there any way we could
jbudorick 2014/09/19 20:09:06 Added tests for this, PackageFilter, RunnerFilter,
+
+ private final String mFilterString;
+
+ private Set<String> mPositiveRegexes;
nyquist 2014/09/19 01:26:03 final for both regexes
jbudorick 2014/09/19 20:09:06 Done.
+ private Set<String> mNegativeRegexes;
nyquist 2014/09/19 01:26:02 Can these be Set<Pattern> ? Then using Pattern.com
jbudorick 2014/09/19 20:09:06 Done.
+
+ /**
+ * Creates the filter and converts the provided googletest-style filter
+ * string into positive and negative regexes.
+ */
+ public GtestFilter(String filterString) {
+ mFilterString = filterString;
+ mPositiveRegexes = new HashSet<String>();
+ mNegativeRegexes = new HashSet<String>();
+
+ String[] filterStrings = filterString.split(":");
nyquist 2014/09/19 01:26:02 Regexpes are better to compile up front instead of
jbudorick 2014/09/19 20:09:06 These are regular string operations, not regexes.
nyquist 2014/09/19 22:21:45 Well, that's the same thing though. Behind the sce
jbudorick 2014/09/20 00:11:29 Didn't realize that. Switched to your suggestion.
+ for (String f : filterStrings) {
+ String sanitized = f.replaceAll("\\.", "\\\\.");
+ sanitized = sanitized.replaceAll("\\*", ".*");
+ int negIndex = sanitized.indexOf('-');
+ if (negIndex == 0) {
+ mNegativeRegexes.add(sanitized.substring(1));
+ } else if (negIndex != -1) {
+ String[] c = sanitized.split("-", 1);
+ mPositiveRegexes.add(c[0]);
+ mNegativeRegexes.add(c[1]);
+ } else {
+ mPositiveRegexes.add(sanitized);
+ }
+ }
+ }
+
+ /**
+ * Determines whether or not a test with the provided description should
+ * run based on the configured positive and negative regexes.
+ *
+ * A test should run if:
+ * - it's just a class, OR
+ * - it doesn't match any of the negative regexes, AND
+ * - either:
+ * - there are no configured positive regexes, OR
+ * - it matches at least one of the positive regexes.
+ */
+ public boolean shouldRun(Description description) {
+ if (description.getMethodName() == null) {
nyquist 2014/09/19 01:26:02 could you keep all these if-statements with their
jbudorick 2014/09/19 20:09:06 Done.
+ return true;
+ }
+
+ String gtestName =
+ description.getClassName() + "." + description.getMethodName();
nyquist 2014/09/19 01:26:02 single line (java is 100 chars)
jbudorick 2014/09/19 20:09:06 Done.
+ for (String p : mNegativeRegexes) {
+ if (Pattern.matches(p, gtestName)) {
nyquist 2014/09/19 01:26:03 p.matcher(gtestName).matches() seems better to me.
jbudorick 2014/09/19 20:09:07 Done.
+ return false;
+ }
+ }
+
+ if (mPositiveRegexes.isEmpty()) {
+ return true;
+ }
+
+ for (String p : mPositiveRegexes) {
+ if (Pattern.matches(p, gtestName)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Returns a description of this filter.
+ */
+ public String describe() {
+ return new String("gtest-filter: " + mFilterString);
nyquist 2014/09/19 01:26:02 just return "gtest-filter: " + mFilterString;
jbudorick 2014/09/19 20:09:06 Done.
+ }
+
+}
+

Powered by Google App Engine
This is Rietveld 408576698