コード例 #1
0
ファイル: UserRepository.cs プロジェクト: cashe/Bleater
        /// <summary>
        /// Creates a user if one doesn't already exist
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public User Get(string name)
        {
            if (_users.ContainsKey(name))
            {
                return _users[name];
            }

            var user = new User(name);
            _users[name] = user;
            return user;
        }
コード例 #2
0
ファイル: PostsFormatterTests.cs プロジェクト: cashe/Bleater
        public void Format_RelativeTime_OutputsCorrectTime()
        {
            //Arrange
            var now = DateTime.Now;
            var user = new User("Chris");
            var post = user.Post("Hello, world!", now - TimeSpan.FromSeconds(2));

            //Act
            var postsFormatter = new PostsFormatter();
            var results = postsFormatter.Format(false, new[] { post }, now);

            //Assert
            Assert.AreEqual("Hello, world! (2 seconds ago)", results.First());
        }
コード例 #3
0
ファイル: PostsFormatterTests.cs プロジェクト: cashe/Bleater
        public void Format_DisplayNamesTrue_IncludesUserNames()
        {
            //Arrange
            const string userName = "******";
            var user = new User(userName);
            var post = user.Post("Hello, world!");

            //Act
            var postsFormatter = new PostsFormatter();
            var results = postsFormatter.Format(true, new[] { post });

            //Assert
            Assert.IsTrue(results.First().Contains(userName), "User name should be in the output.");
        }
コード例 #4
0
ファイル: PostsFormatterTests.cs プロジェクト: cashe/Bleater
        public void Format_MultiplePosts_NewestPostFirst()
        {
            //Arrange
            var now = DateTime.Now;
            var user = new User("Chris");
            var middlePost = user.Post("Middle", now - TimeSpan.FromSeconds(2));
            var newestPost = user.Post("Newest", now);
            var oldestPost = user.Post("Oldest", now - TimeSpan.FromSeconds(4));

            //Act
            var postsFormatter = new PostsFormatter();
            var results = postsFormatter.Format(false, new[] { middlePost, newestPost, oldestPost }, now);

            //Assert
            Assert.IsTrue(results.First().Contains("Newest"), "Expected newest post to be first");
        }
コード例 #5
0
ファイル: CommandExecutorTests.cs プロジェクト: cashe/Bleater
        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);
        }
コード例 #6
0
ファイル: CommandExecutorTests.cs プロジェクト: cashe/Bleater
        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);
        }
コード例 #7
0
ファイル: CommandExecutorTests.cs プロジェクト: cashe/Bleater
        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);
        }
コード例 #8
0
ファイル: CommandExecutorTests.cs プロジェクト: cashe/Bleater
        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);
        }
コード例 #9
0
ファイル: Post.cs プロジェクト: cashe/Bleater
 public Post(User user, string message, DateTime? dateCreated = null)
 {
     User = user;
     Message = message;
     DateCreated = dateCreated ?? DateTime.Now;
 }