Merge pull request #122 from atshaw/120118_as_regex_fix

Fixed regular expression error
pull/14/head
Tim Su 13 years ago
commit 7412481730

@ -29,19 +29,28 @@ public class TitleParser {
priorityHelper(task);
}
private static void listHelper(Task task, ArrayList<String> tags) {
public static String trimParenthesis(String pattern){
if (pattern.charAt(0) == '#' || pattern.charAt(0) == '@') {
pattern = pattern.substring(1);
}
if ('(' == pattern.charAt(0)) {
return pattern.substring(1, pattern.length()-2);
}
return pattern;
}
public static void listHelper(Task task, ArrayList<String> tags) {
String inputText = task.getValue(Task.TITLE);
Pattern tagPattern = Pattern.compile("(\\s|^)#([^\\s]+)");
Pattern contextPattern = Pattern.compile("(\\s|^)(@[^\\s]+)");
Pattern tagPattern = Pattern.compile("(\\s|^)#(\\(.*\\)|[^\\s]+)");
Pattern contextPattern = Pattern.compile("(\\s|^)@(\\(.*\\)|[^\\s]+)");
while(true) {
Matcher m = tagPattern.matcher(inputText);
if(m.find()) {
tags.add(m.group(2));
tags.add(TitleParser.trimParenthesis(m.group(2)));
} else {
m = contextPattern.matcher(inputText);
if(m.find()) {
tags.add(m.group(2));
tags.add(TitleParser.trimParenthesis(m.group(2)));
}else{
break;
}

@ -1,6 +1,7 @@
package com.todoroo.andlib.utility;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -10,6 +11,7 @@ import com.timsu.astrid.R;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.test.DatabaseTestCase;
import com.todoroo.astrid.utility.TitleParser;
public class TitleParserTest extends DatabaseTestCase {
@ -504,6 +506,48 @@ public class TitleParserTest extends DatabaseTestCase {
//----------------Repeats end----------------//
//----------------Tags begin----------------//
/** tests all words using priority 0 */
public void testTagsPound() throws Exception {
String[] acceptedStrings = {
"#tag",
"#a",
"#(a cool tag)",
"#(cool)"
};
TaskService taskService = new TaskService();
Task task = new Task();
for (String acceptedString:acceptedStrings){
task = new Task();
task.setValue(Task.TITLE, "Jog " + acceptedString); //test at end of task. should set importance.
ArrayList<String> tags = new ArrayList<String>();
TitleParser.listHelper(task, tags);
assertTrue("test pound at failed for string: " + acceptedString + " for tags: " + tags.toString(),tags.contains(TitleParser.trimParenthesis(acceptedString)));
}
}
/** tests all words using priority 0 */
public void testTagsAt() throws Exception {
String[] acceptedStrings = {
"@tag",
"@a",
"@(a cool tag)",
"@(cool)"
};
TaskService taskService = new TaskService();
Task task = new Task();
for (String acceptedString:acceptedStrings){
task = new Task();
task.setValue(Task.TITLE, "Jog " + acceptedString); //test at end of task. should set importance.
ArrayList<String> tags = new ArrayList<String>();
TitleParser.listHelper(task, tags);
assertTrue("testTagsAt failed for string: " + acceptedString+ " for tags: " + tags.toString(),tags.contains(TitleParser.trimParenthesis(acceptedString)));
}
}
//----------------Priority end----------------//
}

Loading…
Cancel
Save