Browse Source

More formatter work.

Implement functions, add test.

Functions don't hugely need to be optional, we can just carry on
if the function is invalid.
pull/118/head
Chris Smith 9 years ago
parent
commit
609bd1e94e

+ 2
- 7
src/com/dmdirc/ui/messages/EventFormatter.java View File

@@ -34,7 +34,7 @@ import java.util.Optional;
34 34
  * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
35 35
  * be applied to the result of this, to change the appearance of the output. Functions are
36 36
  * separated from their argument with a <code>|</code> character,
37
- * e.g. <code>{{user.hostname|toCapitals}}</code>.
37
+ * e.g. <code>{{user.hostname|uppercase}}</code>.
38 38
  *
39 39
  * <p>Properties and functions are case-insensitive.
40 40
  */
@@ -86,12 +86,7 @@ public class EventFormatter {
86 86
 
87 87
         String value = target.toString();
88 88
         for (int i = 1; i < functionParts.length; i++) {
89
-            final Optional<String> result = propertyManager.applyFunction(value, functionParts[i]);
90
-            if (result.isPresent()) {
91
-                value = result.get();
92
-            } else {
93
-                return ERROR_STRING;
94
-            }
89
+            value = propertyManager.applyFunction(value, functionParts[i]);
95 90
         }
96 91
 
97 92
         return value;

+ 16
- 3
src/com/dmdirc/ui/messages/EventPropertyManager.java View File

@@ -27,17 +27,25 @@ import com.dmdirc.events.UserErrorEvent;
27 27
 import com.dmdirc.logger.ErrorLevel;
28 28
 
29 29
 import java.lang.reflect.Method;
30
+import java.util.HashMap;
31
+import java.util.Map;
30 32
 import java.util.Optional;
33
+import java.util.function.Function;
31 34
 
32 35
 import javax.inject.Inject;
33 36
 
34 37
 public class EventPropertyManager {
35 38
 
36 39
     private final DMDircMBassador eventBus;
40
+    private final Map<String, Function<String, String>> functions = new HashMap<>();
37 41
 
38 42
     @Inject
39 43
     public EventPropertyManager(final DMDircMBassador eventBus) {
40 44
         this.eventBus = eventBus;
45
+
46
+        functions.put("uppercase", String::toUpperCase);
47
+        functions.put("lowercase", String::toLowerCase);
48
+        functions.put("trim", String::trim);
41 49
     }
42 50
 
43 51
     public <S> Optional<Object> getProperty(final S object, final Class<? extends S> type,
@@ -48,15 +56,20 @@ public class EventPropertyManager {
48 56
             final Method method = type.getMethod(methodName);
49 57
             return Optional.ofNullable(method.invoke(object));
50 58
         } catch (ReflectiveOperationException ex) {
51
-            eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
59
+            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
52 60
                     "Unable to format event: could not retrieve property " + property,
53 61
                     ex.getMessage()));
54 62
         }
55 63
         return Optional.empty();
56 64
     }
57 65
 
58
-    public Optional<String> applyFunction(final String input, final String function) {
59
-        return Optional.empty();
66
+    public String applyFunction(final String input, final String function) {
67
+        if (functions.containsKey(function)) {
68
+            return functions.get(function).apply(input);
69
+        }
70
+        eventBus.publishAsync(new UserErrorEvent(ErrorLevel.LOW, null,
71
+                "Unable to format event: no such function " + function, null));
72
+        return input;
60 73
     }
61 74
 
62 75
 }

+ 16
- 2
test/com/dmdirc/ui/messages/EventFormatterTest.java View File

@@ -48,17 +48,31 @@ public class EventFormatterTest {
48 48
     @Before
49 49
     public void setup() {
50 50
         formatter = new EventFormatter(propertyManager, templateProvider);
51
+    }
52
+
53
+    @Test
54
+    public void testBasicFormat() {
51 55
         messageEvent = new ChannelMessageEvent(channel, null, null);
52 56
 
53 57
         when(templateProvider.getTemplate(ChannelMessageEvent.class))
54 58
                 .thenReturn(Optional.ofNullable("Template {{channel}} meep"));
55 59
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
56 60
                 .thenReturn(Optional.of("MONKEY"));
61
+
62
+        assertEquals("Template MONKEY meep", formatter.format(messageEvent).orElse(null));
57 63
     }
58 64
 
59 65
     @Test
60
-    public void testBasicFormat() {
61
-        assertEquals("Template MONKEY meep", formatter.format(messageEvent).orElse(null));
66
+    public void testFormatWithFunction() {
67
+        messageEvent = new ChannelMessageEvent(channel, null, null);
68
+
69
+        when(templateProvider.getTemplate(ChannelMessageEvent.class))
70
+                .thenReturn(Optional.ofNullable("Template {{channel|lowercase}} meep"));
71
+        when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
72
+                .thenReturn(Optional.of("MONKEY"));
73
+        when(propertyManager.applyFunction("MONKEY", "lowercase")).thenReturn("monkey");
74
+
75
+        assertEquals("Template monkey meep", formatter.format(messageEvent).orElse(null));
62 76
     }
63 77
 
64 78
 }

Loading…
Cancel
Save