|
@@ -21,68 +21,66 @@
|
21
|
21
|
*/
|
22
|
22
|
package com.dmdirc.ui.messages;
|
23
|
23
|
|
24
|
|
-import com.dmdirc.config.ConfigManager;
|
25
|
|
-import com.dmdirc.config.IdentityManager;
|
26
|
|
-import com.dmdirc.harness.TestConfigManagerOptionToggle;
|
27
|
|
-import com.dmdirc.interfaces.config.ConfigProvider;
|
28
|
|
-
|
29
|
|
-import java.util.Collections;
|
|
24
|
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
30
|
25
|
|
31
|
26
|
import org.junit.Before;
|
32
|
27
|
import org.junit.Test;
|
33
|
28
|
import org.junit.runner.RunWith;
|
34
|
29
|
import org.mockito.Mock;
|
|
30
|
+import org.mockito.invocation.InvocationOnMock;
|
35
|
31
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
32
|
+import org.mockito.stubbing.Answer;
|
36
|
33
|
|
37
|
34
|
import static org.junit.Assert.*;
|
38
|
35
|
import static org.mockito.Matchers.any;
|
|
36
|
+import static org.mockito.Matchers.startsWith;
|
39
|
37
|
import static org.mockito.Mockito.when;
|
40
|
38
|
|
41
|
39
|
@RunWith(MockitoJUnitRunner.class)
|
42
|
40
|
public class FormatterTest {
|
43
|
41
|
|
44
|
|
- private TestConfigManagerOptionToggle mcm;
|
45
|
|
-
|
46
|
|
- @Mock private IdentityManager identityManager;
|
|
42
|
+ @Mock private AggregateConfigProvider configProvider;
|
47
|
43
|
|
48
|
44
|
@Before
|
49
|
45
|
public void setup() {
|
50
|
|
- IdentityManager.setIdentityManager(identityManager);
|
51
|
|
- when(identityManager.getIdentitiesForManager(any(ConfigManager.class)))
|
52
|
|
- .thenReturn(Collections.<ConfigProvider>emptyList());
|
53
|
|
-
|
54
|
|
- mcm = new TestConfigManagerOptionToggle();
|
|
46
|
+ when(configProvider.hasOptionString(any(String.class), startsWith("1"))).thenReturn(true);
|
|
47
|
+ when(configProvider.getOption(any(String.class), startsWith("1"))).thenAnswer(new Answer<String> () {
|
|
48
|
+ @Override
|
|
49
|
+ public String answer(final InvocationOnMock invocation) throws Throwable {
|
|
50
|
+ return invocation.getArguments()[1].toString().substring(1);
|
|
51
|
+ }
|
|
52
|
+ });
|
55
|
53
|
}
|
56
|
54
|
|
57
|
55
|
@Test
|
58
|
56
|
public void testBasicFormats() {
|
59
|
|
- assertEquals("Hello!", Formatter.formatMessage(mcm, "1%1$s", "Hello!"));
|
60
|
|
- assertEquals("Hello!", Formatter.formatMessage(mcm, "1%1$s", "Hello!", "Moo!", "Bar!"));
|
61
|
|
- assertTrue(Formatter.formatMessage(mcm, "0%1$s", "Hello!")
|
|
57
|
+ assertEquals("Hello!", Formatter.formatMessage(configProvider, "1%1$s", "Hello!"));
|
|
58
|
+ assertEquals("Hello!", Formatter.formatMessage(configProvider, "1%1$s", "Hello!", "Moo!", "Bar!"));
|
|
59
|
+ assertTrue(Formatter.formatMessage(configProvider, "0%1$s", "Hello!")
|
62
|
60
|
.toLowerCase().indexOf("no format string") > -1);
|
63
|
|
- assertTrue(Formatter.formatMessage(mcm, "1%5$s", "Hello!")
|
|
61
|
+ assertTrue(Formatter.formatMessage(configProvider, "1%5$s", "Hello!")
|
64
|
62
|
.toLowerCase().indexOf("invalid format string") > -1);
|
65
|
|
- assertTrue(Formatter.formatMessage(mcm, "1%1$Z", "Hello!")
|
|
63
|
+ assertTrue(Formatter.formatMessage(configProvider, "1%1$Z", "Hello!")
|
66
|
64
|
.toLowerCase().indexOf("invalid format string") > -1);
|
67
|
65
|
}
|
68
|
66
|
|
69
|
67
|
@Test
|
70
|
68
|
public void testCasting() {
|
71
|
|
- assertEquals("H", Formatter.formatMessage(mcm, "1%1$c", "Hello!"));
|
72
|
|
- assertEquals("10", Formatter.formatMessage(mcm, "1%1$d", "10"));
|
73
|
|
- assertEquals("111999", Formatter.formatMessage(mcm, "1%1$s", "111999"));
|
|
69
|
+ assertEquals("H", Formatter.formatMessage(configProvider, "1%1$c", "Hello!"));
|
|
70
|
+ assertEquals("10", Formatter.formatMessage(configProvider, "1%1$d", "10"));
|
|
71
|
+ assertEquals("111999", Formatter.formatMessage(configProvider, "1%1$s", "111999"));
|
74
|
72
|
}
|
75
|
73
|
|
76
|
74
|
@Test
|
77
|
75
|
public void testCaching() {
|
78
|
|
- assertEquals("H", Formatter.formatMessage(mcm, "1%1$C", "Hello!"));
|
79
|
|
- assertEquals("H", Formatter.formatMessage(mcm, "1%1$C", "Hello!", 123, null));
|
80
|
|
- assertEquals("HELLO!", Formatter.formatMessage(mcm, "1%1$S", "Hello!", 123, null));
|
81
|
|
- assertEquals("HELLO!", Formatter.formatMessage(mcm, "1%1$S", "Hello!"));
|
|
76
|
+ assertEquals("H", Formatter.formatMessage(configProvider, "1%1$C", "Hello!"));
|
|
77
|
+ assertEquals("H", Formatter.formatMessage(configProvider, "1%1$C", "Hello!", 123, null));
|
|
78
|
+ assertEquals("HELLO!", Formatter.formatMessage(configProvider, "1%1$S", "Hello!", 123, null));
|
|
79
|
+ assertEquals("HELLO!", Formatter.formatMessage(configProvider, "1%1$S", "Hello!"));
|
82
|
80
|
}
|
83
|
81
|
|
84
|
82
|
@Test
|
85
|
83
|
public void testFormatDuration() {
|
86
|
|
- assertEquals("1 minute, 1 second", Formatter.formatMessage(mcm, "1%1$u", "61"));
|
|
84
|
+ assertEquals("1 minute, 1 second", Formatter.formatMessage(configProvider, "1%1$u", "61"));
|
87
|
85
|
}
|
88
|
86
|
}
|