You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EventFormatterTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.events.ChannelMessageEvent;
  25. import com.dmdirc.events.DisplayPropertyMap;
  26. import java.util.Optional;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static org.junit.Assert.assertEquals;
  33. import static org.mockito.Mockito.when;
  34. @RunWith(MockitoJUnitRunner.class)
  35. public class EventFormatterTest {
  36. @Mock private EventPropertyManager propertyManager;
  37. @Mock private EventFormatProvider templateProvider;
  38. @Mock private Channel channel;
  39. private ChannelMessageEvent messageEvent;
  40. private EventFormatter formatter;
  41. @Before
  42. public void setup() {
  43. formatter = new EventFormatter(propertyManager, templateProvider);
  44. }
  45. @Test
  46. public void testBasicFormat() {
  47. messageEvent = new ChannelMessageEvent(channel, null, null);
  48. when(templateProvider.getFormat(ChannelMessageEvent.class))
  49. .thenReturn(Optional.of(
  50. EventFormat.create(
  51. "Template {{channel}} meep",
  52. Optional.empty(),
  53. Optional.empty(),
  54. Optional.empty(),
  55. new DisplayPropertyMap())));
  56. when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
  57. .thenReturn(Optional.of("MONKEY"));
  58. assertEquals("Template MONKEY meep", formatter.format(messageEvent).orElse(null));
  59. }
  60. @Test
  61. public void testBeforeAndAfterFormat() {
  62. messageEvent = new ChannelMessageEvent(channel, null, null);
  63. when(templateProvider.getFormat(ChannelMessageEvent.class))
  64. .thenReturn(Optional.of(
  65. EventFormat.create(
  66. "Template {{channel}} meep",
  67. Optional.of("Before!"),
  68. Optional.of("After!"),
  69. Optional.empty(),
  70. new DisplayPropertyMap())));
  71. when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
  72. .thenReturn(Optional.of("MONKEY"));
  73. assertEquals(
  74. "Before!\nTemplate MONKEY meep\nAfter!",
  75. formatter.format(messageEvent).orElse(null));
  76. }
  77. @Test
  78. public void testFormatWithFunction() {
  79. messageEvent = new ChannelMessageEvent(channel, null, null);
  80. when(templateProvider.getFormat(ChannelMessageEvent.class))
  81. .thenReturn(Optional.of(
  82. EventFormat.create(
  83. "Template {{channel|lowercase}} meep",
  84. Optional.empty(),
  85. Optional.empty(),
  86. Optional.empty(),
  87. new DisplayPropertyMap())));
  88. when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
  89. .thenReturn(Optional.of("MONKEY"));
  90. when(propertyManager.applyFunction("MONKEY", "lowercase")).thenReturn("monkey");
  91. assertEquals("Template monkey meep", formatter.format(messageEvent).orElse(null));
  92. }
  93. @Test
  94. public void testFormatWithNestedSubstitutions() {
  95. messageEvent = new ChannelMessageEvent(channel, null, "{{channel}}");
  96. when(templateProvider.getFormat(ChannelMessageEvent.class))
  97. .thenReturn(Optional.of(
  98. EventFormat.create(
  99. "Template {{message}} meep",
  100. Optional.empty(),
  101. Optional.empty(),
  102. Optional.empty(),
  103. new DisplayPropertyMap())));
  104. when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "message"))
  105. .thenReturn(Optional.of("{{channel}}"));
  106. assertEquals("Template {{channel}} meep", formatter.format(messageEvent).orElse(null));
  107. }
  108. }