diff --git a/astrid/src/com/todoroo/astrid/utility/TitleParser.java b/astrid/src/com/todoroo/astrid/utility/TitleParser.java index 33a1f918a..556f5916a 100644 --- a/astrid/src/com/todoroo/astrid/utility/TitleParser.java +++ b/astrid/src/com/todoroo/astrid/utility/TitleParser.java @@ -29,19 +29,28 @@ public class TitleParser { priorityHelper(task); } - private static void listHelper(Task task, ArrayList 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 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; } diff --git a/tests/src/com/todoroo/andlib/utility/TitleParserTest.java b/tests/src/com/todoroo/andlib/utility/TitleParserTest.java index 27ab7a4ed..704b0d3ed 100644 --- a/tests/src/com/todoroo/andlib/utility/TitleParserTest.java +++ b/tests/src/com/todoroo/andlib/utility/TitleParserTest.java @@ -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 tags = new ArrayList(); + 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 tags = new ArrayList(); + TitleParser.listHelper(task, tags); + assertTrue("testTagsAt failed for string: " + acceptedString+ " for tags: " + tags.toString(),tags.contains(TitleParser.trimParenthesis(acceptedString))); + } + } + + + + //----------------Priority end----------------// }