Browse Source

Tidy SetNickColour, fix unit test.

pull/303/head
Greg Holmes 9 years ago
parent
commit
d36870597a

+ 8
- 12
src/com/dmdirc/commandparser/commands/channel/SetNickColour.java View File

@@ -75,32 +75,28 @@ public class SetNickColour extends Command implements IntelligentCommand {
75 75
             final CommandArguments args, final CommandContext context) {
76 76
         final Channel channel = ((ChannelCommandContext) context).getChannel();
77 77
 
78
-        int offset = 0;
79
-
80
-        if (args.getArguments().length <= offset) {
81
-            showUsage(origin, args.isSilent(), "setnickcolour",
82
-                    "<nick> [colour]");
78
+        if (args.getArguments().length == 0) {
79
+            showUsage(origin, args.isSilent(), INFO.getName(), INFO.getHelp());
83 80
             return;
84 81
         }
85 82
 
86 83
         final Optional<GroupChatUser> target = channel.getUser(channel.getConnection().get()
87
-                .getUser(args.getArguments()[offset]));
88
-        offset++;
84
+                .getUser(args.getArguments()[0]));
89 85
 
90 86
         if (!target.isPresent()) {
91 87
             sendLine(origin, args.isSilent(), FORMAT_ERROR, "No such nickname ("
92
-                    + args.getArguments()[offset - 1] + ")!");
93
-        } else if (args.getArguments().length <= offset) {
88
+                    + args.getArguments()[0] + ")!");
89
+        } else if (args.getArguments().length == 1) {
94 90
             // We're removing the colour
95 91
             target.get().removeDisplayProperty(DisplayProperty.FOREGROUND_COLOUR);
96
-
97 92
             channel.refreshClients();
98 93
         } else {
99 94
             // We're setting the colour
100 95
             final Colour newColour = colourManagerFactory.getColourManager(origin.getConfigManager())
101
-                    .getColourFromString(args.getArguments()[offset], null);
96
+                    .getColourFromString(args.getArguments()[1], null);
102 97
             if (newColour == null) {
103
-                sendLine(origin, args.isSilent(), FORMAT_ERROR, "Invalid colour specified.");
98
+                sendLine(origin, args.isSilent(), FORMAT_ERROR, "Invalid colour specified ("
99
+                        + args.getArguments()[1] + ").");
104 100
                 return;
105 101
             }
106 102
 

+ 47
- 6
test/com/dmdirc/commandparser/commands/channel/SetNickColourTest.java View File

@@ -25,8 +25,16 @@ import com.dmdirc.Channel;
25 25
 import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.commandparser.CommandArguments;
27 27
 import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
28
+import com.dmdirc.events.DisplayProperty;
28 29
 import com.dmdirc.interfaces.CommandController;
30
+import com.dmdirc.interfaces.Connection;
31
+import com.dmdirc.interfaces.GroupChatUser;
32
+import com.dmdirc.interfaces.User;
33
+import com.dmdirc.ui.messages.ColourManager;
29 34
 import com.dmdirc.ui.messages.ColourManagerFactory;
35
+import com.dmdirc.util.colours.Colour;
36
+
37
+import java.util.Optional;
30 38
 
31 39
 import org.junit.Before;
32 40
 import org.junit.Test;
@@ -34,18 +42,25 @@ import org.junit.runner.RunWith;
34 42
 import org.mockito.Mock;
35 43
 import org.mockito.runners.MockitoJUnitRunner;
36 44
 
45
+import static org.mockito.Matchers.any;
37 46
 import static org.mockito.Mockito.anyChar;
38 47
 import static org.mockito.Mockito.anyString;
39 48
 import static org.mockito.Mockito.eq;
40 49
 import static org.mockito.Mockito.mock;
50
+import static org.mockito.Mockito.never;
41 51
 import static org.mockito.Mockito.verify;
42 52
 import static org.mockito.Mockito.when;
43 53
 
44 54
 @RunWith(MockitoJUnitRunner.class)
45 55
 public class SetNickColourTest {
46 56
 
57
+    @Mock private Connection connection;
58
+    @Mock private User user1;
59
+    @Mock private User user2;
60
+    @Mock private GroupChatUser groupChatUser;
47 61
     @Mock private Channel channel;
48 62
     @Mock private ColourManagerFactory colourManagerFactory;
63
+    @Mock private ColourManager colourManager;
49 64
     @Mock private CommandController controller;
50 65
     private SetNickColour command;
51 66
 
@@ -54,6 +69,13 @@ public class SetNickColourTest {
54 69
         command = new SetNickColour(controller, colourManagerFactory);
55 70
         when(controller.getCommandChar()).thenReturn('/');
56 71
         when(controller.getSilenceChar()).thenReturn('.');
72
+        when(channel.getConnection()).thenReturn(Optional.of(connection));
73
+        when(connection.getUser("moo")).thenReturn(user1);
74
+        when(connection.getUser("foo")).thenReturn(user2);
75
+        when(channel.getUser(user1)).thenReturn(Optional.of(groupChatUser));
76
+        when(channel.getUser(user2)).thenReturn(Optional.empty());
77
+        when(colourManagerFactory.getColourManager(any())).thenReturn(colourManager);
78
+        when(colourManager.getColourFromString(eq("4"), any())).thenReturn(Colour.RED);
57 79
     }
58 80
 
59 81
     @Test
@@ -66,21 +88,40 @@ public class SetNickColourTest {
66 88
     }
67 89
 
68 90
     @Test
69
-    public void testUsageNicklist() {
91
+    public void testUsageNicknameValid() {
70 92
         final FrameContainer tiw = mock(FrameContainer.class);
71
-        command.execute(tiw, new CommandArguments(controller, "/foo --nicklist"),
93
+        command.execute(tiw, new CommandArguments(controller, "/foo moo"),
72 94
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
95
+        verify(channel).refreshClients();
96
+        verify(groupChatUser).removeDisplayProperty(DisplayProperty.FOREGROUND_COLOUR);
97
+    }
73 98
 
74
-        verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
99
+    @Test
100
+    public void testUsageNicknameInvalid() {
101
+        final FrameContainer tiw = mock(FrameContainer.class);
102
+        command.execute(tiw, new CommandArguments(controller, "/foo foo"),
103
+                new ChannelCommandContext(null, SetNickColour.INFO, channel));
104
+        verify(channel, never()).refreshClients();
105
+        verify(groupChatUser, never()).removeDisplayProperty(DisplayProperty.FOREGROUND_COLOUR);
106
+        verify(tiw).addLine(eq("commandError"), eq("No such nickname (foo)!"));
75 107
     }
76 108
 
77 109
     @Test
78
-    public void testUsageText() {
110
+    public void testUsageInvalidColour() {
79 111
         final FrameContainer tiw = mock(FrameContainer.class);
80
-        command.execute(tiw, new CommandArguments(controller, "/foo --text"),
112
+        command.execute(tiw, new CommandArguments(controller, "/foo moo omg"),
81 113
                 new ChannelCommandContext(null, SetNickColour.INFO, channel));
82 114
 
83
-        verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
115
+        verify(tiw).addLine(eq("commandError"), eq("Invalid colour specified (omg)."));
116
+    }
117
+
118
+    @Test
119
+    public void testUsageValidColour() {
120
+        final FrameContainer tiw = mock(FrameContainer.class);
121
+        command.execute(tiw, new CommandArguments(controller, "/foo moo 4"),
122
+                new ChannelCommandContext(null, SetNickColour.INFO, channel));
123
+        verify(channel).refreshClients();
124
+        verify(groupChatUser).setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, Colour.RED);
84 125
     }
85 126
 
86 127
 }

Loading…
Cancel
Save