Skip to content
Permalink
Browse files

refactor to use parameterized tests and code cleanup

  • Loading branch information...
bigloser committed Jun 26, 2017
1 parent 5b4f3b8 commit e93275151f25330db760bc9fb9972e6ecf4b2114
@@ -5,3 +5,4 @@ java/.idea
/.idea
twitter-text.iml
.DS_Store
*.sw?
@@ -56,6 +56,7 @@
<configuration>
<source>1.5</source>
<target>1.5</target>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
<plugin>
@@ -257,14 +258,14 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.12</version>
<version>1.18</version>
<scope>test</scope>
</dependency>

@@ -29,12 +29,12 @@
/** Default attribute for invisible span tag */
public static final String DEFAULT_INVISIBLE_TAG_ATTRS = "style='position:absolute;left:-9999px;'";

public static interface LinkAttributeModifier {
public void modify(Entity entity, Map<String, String> attributes);
};
public interface LinkAttributeModifier {
void modify(Entity entity, Map<String, String> attributes);
}

public static interface LinkTextModifier {
public CharSequence modify(Entity entity, CharSequence text);
public interface LinkTextModifier {
CharSequence modify(Entity entity, CharSequence text);
}

protected String urlClass = null;
@@ -74,6 +74,10 @@ private static CharSequence escapeHTML(CharSequence text) {
}

public Autolink() {
this(true);
}

public Autolink(boolean noFollow) {
urlClass = null;
listClass = DEFAULT_LIST_CLASS;
usernameClass = DEFAULT_USERNAME_CLASS;
@@ -86,6 +90,7 @@ public Autolink() {
invisibleTagAttrs = DEFAULT_INVISIBLE_TAG_ATTRS;

extractor.setExtractURLWithoutProtocol(false);
this.noFollow = noFollow;
}

public String escapeBrackets(String text) {
@@ -1,8 +1,12 @@

package com.twitter;

import java.util.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;

/**
* A class to extract usernames, lists, hashtags and URLs from Tweet text.
@@ -55,14 +59,10 @@ public boolean equals(Object obj) {

Entity other = (Entity)obj;

if (this.type.equals(other.type) &&
return this.type.equals(other.type) &&
this.start == other.start &&
this.end == other.end &&
this.value.equals(other.value)) {
return true;
} else {
return false;
}
this.value.equals(other.value);
}

@Override
@@ -122,7 +122,7 @@ public Extractor() {

private void removeOverlappingEntities(List<Entity> entities) {
// sort by index
Collections.<Entity>sort(entities, new Comparator<Entity>() {
Collections.sort(entities, new Comparator<Entity>() {
public int compare(Entity e1, Entity e2) {
return e1.start - e2.start;
}
@@ -180,7 +180,7 @@
private static final String URL_VALID_URL_QUERY_ENDING_CHARS = "[a-z0-9_&=#/]";
private static final String VALID_URL_PATTERN_STRING =
"(" + // $1 total match
"(" + URL_VALID_PRECEEDING_CHARS + ")" + // $2 Preceeding chracter
"(" + URL_VALID_PRECEEDING_CHARS + ")" + // $2 Preceding character
"(" + // $3 URL
"(https?://)?" + // $4 Protocol (optional)
"(" + URL_VALID_DOMAIN + ")" + // $5 Domain(s)
@@ -1,7 +1,9 @@
package com.twitter;

import java.io.File;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

/**
* Micro benchmark for discovering hotspots in our autolinker.
@@ -10,10 +12,10 @@

private static final int AUTO_LINK_TESTS = 10000;
private static final int ITERATIONS = 10;
private static final Autolink linker = new Autolink(false);

public double testBenchmarkAutolinking() throws Exception {
File yamlFile = new File(conformanceDir, "autolink.yml");
List testCases = loadConformanceData(yamlFile, "all");
List<Map> testCases = loadConformanceData("autolink.yml", "all");
autolink(testCases);
long start = System.currentTimeMillis();
for (int i = 0; i < AUTO_LINK_TESTS; i++) {
@@ -25,9 +27,16 @@ public double testBenchmarkAutolinking() throws Exception {
return autolinksPerMS;
}

private void autolink(List<Map> testCases) {
for (Map testCase : testCases) {
assertEquals((String)testCase.get(KEY_DESCRIPTION),
testCase.get(KEY_EXPECTED_OUTPUT),
linker.autoLink((String) testCase.get(KEY_INPUT)));
}
}

public static void main(String[] args) throws Exception {
Benchmark benchmark = new Benchmark();
benchmark.setUp();
double total = 0;
double best = Double.MAX_VALUE;
double worst = 0;
@@ -39,7 +48,6 @@ public static void main(String[] args) throws Exception {
}
// Drop worst and best
total -= best + worst;
System.out.println("Average: " + (total/(ITERATIONS - 2)));
benchmark.tearDown();
System.out.println("Average: " + (total / (ITERATIONS - 2)));
}
}

0 comments on commit e932751

Please sign in to comment.
You can’t perform that action at this time.