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
  * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
34
  * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
35
  * be applied to the result of this, to change the appearance of the output. Functions are
35
  * be applied to the result of this, to change the appearance of the output. Functions are
36
  * separated from their argument with a <code>|</code> character,
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
  * <p>Properties and functions are case-insensitive.
39
  * <p>Properties and functions are case-insensitive.
40
  */
40
  */
86
 
86
 
87
         String value = target.toString();
87
         String value = target.toString();
88
         for (int i = 1; i < functionParts.length; i++) {
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
         return value;
92
         return value;

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

27
 import com.dmdirc.logger.ErrorLevel;
27
 import com.dmdirc.logger.ErrorLevel;
28
 
28
 
29
 import java.lang.reflect.Method;
29
 import java.lang.reflect.Method;
30
+import java.util.HashMap;
31
+import java.util.Map;
30
 import java.util.Optional;
32
 import java.util.Optional;
33
+import java.util.function.Function;
31
 
34
 
32
 import javax.inject.Inject;
35
 import javax.inject.Inject;
33
 
36
 
34
 public class EventPropertyManager {
37
 public class EventPropertyManager {
35
 
38
 
36
     private final DMDircMBassador eventBus;
39
     private final DMDircMBassador eventBus;
40
+    private final Map<String, Function<String, String>> functions = new HashMap<>();
37
 
41
 
38
     @Inject
42
     @Inject
39
     public EventPropertyManager(final DMDircMBassador eventBus) {
43
     public EventPropertyManager(final DMDircMBassador eventBus) {
40
         this.eventBus = eventBus;
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
     public <S> Optional<Object> getProperty(final S object, final Class<? extends S> type,
51
     public <S> Optional<Object> getProperty(final S object, final Class<? extends S> type,
48
             final Method method = type.getMethod(methodName);
56
             final Method method = type.getMethod(methodName);
49
             return Optional.ofNullable(method.invoke(object));
57
             return Optional.ofNullable(method.invoke(object));
50
         } catch (ReflectiveOperationException ex) {
58
         } catch (ReflectiveOperationException ex) {
51
-            eventBus.publish(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
59
+            eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
52
                     "Unable to format event: could not retrieve property " + property,
60
                     "Unable to format event: could not retrieve property " + property,
53
                     ex.getMessage()));
61
                     ex.getMessage()));
54
         }
62
         }
55
         return Optional.empty();
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
     @Before
48
     @Before
49
     public void setup() {
49
     public void setup() {
50
         formatter = new EventFormatter(propertyManager, templateProvider);
50
         formatter = new EventFormatter(propertyManager, templateProvider);
51
+    }
52
+
53
+    @Test
54
+    public void testBasicFormat() {
51
         messageEvent = new ChannelMessageEvent(channel, null, null);
55
         messageEvent = new ChannelMessageEvent(channel, null, null);
52
 
56
 
53
         when(templateProvider.getTemplate(ChannelMessageEvent.class))
57
         when(templateProvider.getTemplate(ChannelMessageEvent.class))
54
                 .thenReturn(Optional.ofNullable("Template {{channel}} meep"));
58
                 .thenReturn(Optional.ofNullable("Template {{channel}} meep"));
55
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
59
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
56
                 .thenReturn(Optional.of("MONKEY"));
60
                 .thenReturn(Optional.of("MONKEY"));
61
+
62
+        assertEquals("Template MONKEY meep", formatter.format(messageEvent).orElse(null));
57
     }
63
     }
58
 
64
 
59
     @Test
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