Exemple #1
0
        public void ReceiveNewMessagesWhenValidSocketShouldReceiveNewMessagesShowThemToTheUserAndUpdateLastMessage()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("otherUser: Unrelated message.<eof>");
            mockSocket.ListToReceive.Add("userName: Hello!<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("Hello!");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            client.Start();

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>Hello!<sep>server: userName joined the chat.<eof>", item));

            Assert.Collection(mockSocket.ReceivedMessages,
                              item => Assert.Equal("server: userName joined the chat.<eof>", item),
                              item => Assert.Equal("otherUser: Unrelated message.<eof>", item),
                              item => Assert.Equal("userName: Hello!<eof>", item));
        }
Exemple #2
0
        public void StartWhenValidSocketAndDataReaderShouldLogOnAndProcessChat()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("otherUser: Unrelated message.<eof>");
            mockSocket.ListToReceive.Add("userName: Hello!<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("Hello!");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            client.Start();

            Assert.False(mockSocket.Connected);
            Assert.True(mockSocket.Closed);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>Hello!<sep>server: userName joined the chat.<eof>", item));

            Assert.Collection(mockSocket.ReceivedMessages,
                              item => Assert.Equal("server: userName joined the chat.<eof>", item),
                              item => Assert.Equal("otherUser: Unrelated message.<eof>", item),
                              item => Assert.Equal("userName: Hello!<eof>", item));
        }
Exemple #3
0
        public void StartWhenNullSocketShouldThrowException()
        {
            MockClientSocket mockSocket = null;

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.Throws <ArgumentNullException>(() => client.Start());
        }
Exemple #4
0
        public void StartWhenNullDataReaderShouldThrowException()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");

            MockDataReader dataReader = null;

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.Throws <ArgumentNullException>(() => client.Start());
        }
Exemple #5
0
        public void LogOnWhenValidUserNameShouldLogOnReceiveGreetingMessageAndDisconnect()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            Assert.Equal("userName", client.LogOn());

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item));
        }
Exemple #6
0
        public void LogOnWhenUserNameContainsEOFTagShouldSendNothingShowErrorMessageAndRequestForNewUserName()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("user<eof>Name");
            dataReader.ListToRead.Add("userName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            Assert.Equal("userName", client.LogOn());

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item));
        }
Exemple #7
0
        public void SendMessageWhenValidMessageShouldConnectSendMessageToServerAndDisconnect()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("userName: Hello!<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("Hello!");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            client.Start();

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>Hello!<sep>server: userName joined the chat.<eof>", item));
        }
Exemple #8
0
        public void LogOnWhenAlreadyExistingUserNameShouldReceiveErrorMessageAndRequestForNewUserName()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName already exist, choose a different user name.<eof>");
            mockSocket.ListToReceive.Add("server: newUserName joined the chat.<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add("newUserName");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            Assert.True(mockSocket.Connected);

            Assert.Equal("newUserName", client.LogOn());

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("newUserName<sep>logon<sep>NoLastMessage<eof>", item));
        }
Exemple #9
0
        public void SendMessageWhenNullStringShouldSendNothingShowErrorMessageAndRequestForNewMessage()
        {
            MockClientSocket mockSocket = new MockClientSocket();

            mockSocket.ListToReceive.Add("server: userName joined the chat.<eof>");
            mockSocket.ListToReceive.Add("userName: valid message<eof>");

            MockDataReader dataReader = new MockDataReader();

            dataReader.ListToRead.Add("userName");
            dataReader.ListToRead.Add(null);
            dataReader.ListToRead.Add("valid message");
            dataReader.ListToRead.Add("exit");

            ChatClientSide client = new ChatClientSide(mockSocket, dataReader);

            client.Start();

            Assert.False(mockSocket.Connected);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("userName<sep>logon<sep>NoLastMessage<eof>", item),
                              item => Assert.Equal("userName<sep>valid message<sep>server: userName joined the chat.<eof>", item));
        }