Exemplo n.º 1
0
        public void Program_Matches_Spec()
        {
            //We freezing time so that code execution speed doesn't have an impact
            //on the timestamps in our output (eg 1 second ago, 5 minutes ago)
            var now = DateTime.Now;

            var userRepository = new UserRepository();
            var postsFormatter = new PostsFormatter();
            var output = Enumerable.Empty<string>();
            var commandExecutor = new CommandExecutor(userRepository, postsFormatter, o => output = o);
            var commandParser = new CommandParser();

            var alicePost1Command = commandParser.Parse("Alice -> I love the weather today", now - TimeSpan.FromMinutes(5));
            commandExecutor.Execute(alicePost1Command);

            var bobPost1Command = commandParser.Parse("Bob -> Damn! We lost!", now - TimeSpan.FromMinutes(2));
            commandExecutor.Execute(bobPost1Command);

            var bobPost2Command = commandParser.Parse("Bob -> Good game though.", now - TimeSpan.FromMinutes(1));
            commandExecutor.Execute(bobPost2Command);

            var readAliceCommand = commandParser.Parse("Alice", now);
            commandExecutor.Execute(readAliceCommand, now);
            Assert.AreEqual(1, output.ToArray().Length, "Expected 1 post");
            Assert.AreEqual("I love the weather today (5 minutes ago)", output.First());

            var readBobCommand = commandParser.Parse("Bob", now);
            commandExecutor.Execute(readBobCommand, now);
            Assert.AreEqual(2, output.ToArray().Length, "Expected 2 posts");
            Assert.AreEqual("Good game though. (1 minute ago)", output.First());
            Assert.AreEqual("Damn! We lost! (2 minutes ago)", output.ElementAt(1));

            var charliePostCommand = commandParser.Parse("Charlie -> I'm in New York today! Anyone want to have a coffee?", now - TimeSpan.FromSeconds(2));
            commandExecutor.Execute(charliePostCommand);

            var charlieFollowsAliceCommand = commandParser.Parse("Charlie follows Alice");
            commandExecutor.Execute(charlieFollowsAliceCommand);

            var charlieWallCommand = commandParser.Parse("Charlie wall", now);
            commandExecutor.Execute(charlieWallCommand, now);
            Assert.AreEqual(2, output.ToArray().Length, "Expected 2 posts");
            Assert.AreEqual("Charlie - I'm in New York today! Anyone want to have a coffee? (2 seconds ago)", output.First());
            Assert.AreEqual("Alice - I love the weather today (5 minutes ago)", output.ElementAt(1));

            var charlieFollowsBobCommand = commandParser.Parse("Charlie follows Bob");
            commandExecutor.Execute(charlieFollowsBobCommand);

            var charlieWall2Command = commandParser.Parse("Charlie wall", now);
            commandExecutor.Execute(charlieWall2Command, now);
            Assert.AreEqual(4, output.ToArray().Length, "Expected 4 posts");
            Assert.AreEqual("Charlie - I'm in New York today! Anyone want to have a coffee? (2 seconds ago)", output.First());
            Assert.AreEqual("Bob - Good game though. (1 minute ago)", output.ElementAt(1));
            Assert.AreEqual("Bob - Damn! We lost! (2 minutes ago)", output.ElementAt(2));
            Assert.AreEqual("Alice - I love the weather today (5 minutes ago)", output.ElementAt(3));
        }
Exemplo n.º 2
0
        static void Main()
        {
            System.Console.WriteLine(@"

            WELCOME TO BLEATER!!!

                     _,._
                 __.'   _)
                <_,)'.-""a\
                  /' (    \
              _.-----..,-'   (`""--^ (BLEAT!!!)
             //              |
            (|   `;      ,   |
              \   ;.----/  ,/
               ) // /   | |\ \
               \ \\`\   | |/ /
            \ \\ \  | |\/
             `"" `""  `""`

            o posting: [user name] -> [message]

            o reading: [user name]

            o following: [user name] follows [another user]

            o wall:	[user name] wall

            o (press CTRL+Z to exit)

            ");

            var commandParser = new CommandParser();
            var commandExecutor = new CommandExecutor(new UserRepository(), new PostsFormatter(), WriteLines);

            string stringCommand;
            do
            {
                System.Console.Write("> ");
                stringCommand = System.Console.ReadLine();
                if (stringCommand != null)
                {
                    var command = commandParser.Parse(stringCommand);
                    commandExecutor.Execute(command);
                }

            } while (stringCommand != null);
        }
Exemplo n.º 3
0
        public void Execute_PostCommand_PopulatesUsersPosts()
        {
            //Arrange - Create our user
            const string userName = "******";
            var user = new User(userName);

            //Arrange - the user repo should return our user
            var mockRepository = new Mock<IUserRepository>();
            mockRepository.Setup((r => r.Get(userName))).Returns(user);

            var commandExecutor = new CommandExecutor(mockRepository.Object, new Mock<IPostsFormatter>().Object, WriteLines);

            //Act
            const string message = "Hello, world!";
            commandExecutor.Execute(new PostCommand(userName, message));

            //Assert
            Assert.IsTrue(user.Posts.Any(p => p.Message == message), "Expected post with text '{0}'.", message);
        }
Exemplo n.º 4
0
        public void Execute_FollowCommand_PopulatesUsersFollowing()
        {
            //Arrange - Create our users
            const string userName = "******";
            const string followedUserName = "******";
            var user = new User(userName);
            var followedUser = new User(followedUserName);

            //Arrange - the user repo should return our users
            var mockRepository = new Mock<IUserRepository>();
            mockRepository.Setup((r => r.Get(userName))).Returns(user);
            mockRepository.Setup((r => r.Get(followedUserName))).Returns(followedUser);

            var commandExecutor = new CommandExecutor(mockRepository.Object, new Mock<IPostsFormatter>().Object, WriteLines);

            //Act
            commandExecutor.Execute(new FollowCommand(userName, followedUserName));

            //Assert
            Assert.IsTrue(user.Following.Contains(followedUser), "Expected {0} to be following {1}.", user, followedUser);
        }
Exemplo n.º 5
0
        public void Execute_ReadCommand_ReturnsUsersPosts()
        {
            //Arrange - Create our user with 2 posts
            const string userName = "******";
            var user = new User(userName);
            const string firstPostMessage = "Hello, world!";
            const string secondPostMessage = "Goodbye, world!";
            user.Post(firstPostMessage);
            user.Post(secondPostMessage);

            //Arrange - the user repo should return our user
            var mockRepository = new Mock<IUserRepository>();
            mockRepository.Setup((r => r.Get(userName))).Returns(user);

            var output = Enumerable.Empty<string>();
            var commandExecutor = new CommandExecutor(mockRepository.Object, new PostsFormatterMock(), o => output = o);

            //Act
            commandExecutor.Execute(new ReadCommand(userName));

            //Assert
            Assert.IsTrue(output.Contains(firstPostMessage), "Expected post with text '{0}'.", firstPostMessage);
            Assert.IsTrue(output.Contains(secondPostMessage), "Expected post with text '{0}'.", secondPostMessage);
        }
Exemplo n.º 6
0
        public void Execute_WallCommand_ReturnsCurrentUsersAndFollowingUsersPosts()
        {
            //Arrange - Create user
            const string userName = "******";
            var user = new User(userName);
            const string userPostMessage = "I like Sophie";
            user.Post(userPostMessage);

            //Arrange - Created followed user
            const string followedUserName = "******";
            var followedUser = new User(followedUserName);
            const string followedUserPostMessage = "I like Chris";
            followedUser.Post(followedUserPostMessage);

            //Arrange - Set up following
            user.Following.Add(followedUser);

            //Arrange - the user repo should return our users
            var mockRepository = new Mock<IUserRepository>();
            mockRepository.Setup((r => r.Get(userName))).Returns(user);
            mockRepository.Setup((r => r.Get(followedUserName))).Returns(followedUser);

            var output = Enumerable.Empty<string>();
            var commandExecutor = new CommandExecutor(mockRepository.Object, new PostsFormatterMock(), o => output = o);

            //Act
            commandExecutor.Execute(new WallCommand(userName));

            //Assert
            Assert.IsTrue(output.Contains(userPostMessage), "Expected reults to contain user's post, i.e. '{0}'.", userPostMessage);
            Assert.IsTrue(output.Contains(followedUserPostMessage), "Expected reults to contain followed user's post, i.e. '{0}'.", followedUserPostMessage);
        }