using System; using System.Collections.Generic; using System.Reactive.Threading.Tasks; using System.Threading.Tasks; using NSubstitute; using Octokit.Reactive; using Xunit; namespace Octokit.Tests.Reactive { public class ObservableRepositoryCommentsClientTests { public class TheCtor { [Fact] public void EnsuresNonNullArguments() { Assert.Throws(() => new ObservableRepositoryCommentsClient(null)); } } public class TheGetMethod { [Fact] public void RequestsCorrectUrl() { var gitHub = Substitute.For(); var client = new ObservableRepositoryCommentsClient(gitHub); client.Get("fake", "repo", 42); gitHub.Received().Repository.Comment.Get("fake", "repo", 42); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHub = Substitute.For(); var client = new ObservableRepositoryCommentsClient(gitHub); client.Get(1, 42); gitHub.Received().Repository.Comment.Get(1, 42); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableRepositoryCommentsClient(Substitute.For()); Assert.Throws(() => client.Get(null, "name", 1)); Assert.Throws(() => client.Get("owner", null, 1)); Assert.Throws(() => client.Get("", "name", 1)); Assert.Throws(() => client.Get("owner", "", 1)); } } public class TheGetAllForRepositoryMethod { [Fact] public void RequestsCorrectUrl() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.GetAllForRepository("fake", "repo"); githubClient.Connection.Received(1).Get>(Arg.Is(uri => uri.ToString() == "repos/fake/repo/comments"), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.GetAllForRepository(1); githubClient.Connection.Received(1).Get>(Arg.Is(uri => uri.ToString() == "repositories/1/comments"), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithApiOptions() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); var options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllForRepository("fake", "repo", options); githubClient.Connection.Received(1).Get>(Arg.Is(uri => uri.ToString() == "repos/fake/repo/comments"), Arg.Is>(dictionary => dictionary.Count == 2)); } [Fact] public void RequestsCorrectUrlWithRepositoryIdWithApiOptions() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); var options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllForRepository(1, options); githubClient.Connection.Received(1).Get>(Arg.Is(uri => uri.ToString() == "repositories/1/comments"), Arg.Is>(dictionary => dictionary.Count == 2)); } [Fact] public void EnsuresNonNullArguments() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); Assert.Throws(() => client.GetAllForRepository(null, "name", ApiOptions.None)); Assert.Throws(() => client.GetAllForRepository("owner", null, ApiOptions.None)); Assert.Throws(() => client.GetAllForRepository("owner", "name", null)); Assert.Throws(() => client.GetAllForRepository(null, "name")); Assert.Throws(() => client.GetAllForRepository("owner", null)); Assert.Throws(() => client.GetAllForRepository(1, null)); Assert.Throws(() => client.GetAllForRepository("", "name")); Assert.Throws(() => client.GetAllForRepository("owner", "")); Assert.Throws(() => client.GetAllForRepository("", "name", ApiOptions.None)); Assert.Throws(() => client.GetAllForRepository("owner", "", ApiOptions.None)); } } public class TheGetAllForCommitMethod { [Fact] public void RequestsCorrectUrl() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.GetAllForCommit("fake", "repo", "sha"); githubClient.Connection.Received().Get>(Arg.Is(new Uri("repos/fake/repo/commits/sha/comments", UriKind.Relative)), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.GetAllForCommit(1, "sha"); githubClient.Connection.Received().Get>(Arg.Is(new Uri("repositories/1/commits/sha/comments", UriKind.Relative)), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithApiOptions() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); var options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllForCommit("fake", "repo", "sha", options); githubClient.Connection.Received().Get>(Arg.Is(new Uri("repos/fake/repo/commits/sha/comments", UriKind.Relative)), Arg.Is>(d => d.Count == 2)); } [Fact] public void RequestsCorrectUrlWithRepositoryIdWithApiOptions() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); var options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllForCommit(1, "sha", options); githubClient.Connection.Received().Get>(Arg.Is(new Uri("repositories/1/commits/sha/comments", UriKind.Relative)), Arg.Is>(d => d.Count == 2)); } [Fact] public void EnsuresNonNullArguments() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); Assert.Throws(() => client.GetAllForCommit(null, "name", "sha")); Assert.Throws(() => client.GetAllForCommit("owner", null, "sha")); Assert.Throws(() => client.GetAllForCommit("owner", "name", null)); Assert.Throws(() => client.GetAllForCommit(null, "name", "sha", ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit("owner", null, "sha", ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit("owner", "name", null, ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit("owner", "name", "sha", null)); Assert.Throws(() => client.GetAllForCommit(1, null, ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit(1, "sha", null)); Assert.Throws(() => client.GetAllForCommit("", "name", "sha")); Assert.Throws(() => client.GetAllForCommit("owner", "", "sha")); Assert.Throws(() => client.GetAllForCommit("owner", "name", "")); Assert.Throws(() => client.GetAllForCommit("", "name", "sha", ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit("owner", "", "sha", ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit("owner", "name", "", ApiOptions.None)); Assert.Throws(() => client.GetAllForCommit(1, "", ApiOptions.None)); } } public class TheCreateMethod { [Fact] public void PostsToCorrectUrl() { var newComment = new NewCommitComment("body"); var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.Create("fake", "repo", "sha", newComment); githubClient.Repository.Comment.Received().Create("fake", "repo", "sha", newComment); } [Fact] public void PostsToCorrectUrlWithRepositoryId() { var newComment = new NewCommitComment("body"); var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.Create(1, "sha", newComment); githubClient.Repository.Comment.Received().Create(1, "sha", newComment); } [Fact] public async Task EnsuresNonNullArguments() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); await Assert.ThrowsAsync(() => client.Create(null, "name", "sha", new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create("owner", null, "sha", new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create("owner", "name", null, new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create("owner", "name", "sha", null).ToTask()); await Assert.ThrowsAsync(() => client.Create(1, null, new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create(1, "sha", null).ToTask()); await Assert.ThrowsAsync(() => client.Create("", "name", "sha", new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create("owner", "", "sha", new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create("owner", "name", "", new NewCommitComment("body")).ToTask()); await Assert.ThrowsAsync(() => client.Create(1, "", new NewCommitComment("body")).ToTask()); } } public class TheUpdateMethod { [Fact] public void PostsToCorrectUrl() { const string issueCommentUpdate = "updated comment"; var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.Update("fake", "repo", 42, issueCommentUpdate); githubClient.Repository.Comment.Received().Update("fake", "repo", 42, issueCommentUpdate); } [Fact] public void PostsToCorrectUrlWithRepositoryId() { const string issueCommentUpdate = "updated comment"; var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.Update(1, 42, issueCommentUpdate); githubClient.Repository.Comment.Received().Update(1, 42, issueCommentUpdate); } [Fact] public void EnsuresNonNullArguments() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); Assert.Throws(() => client.Update(null, "name", 42, "updated comment")); Assert.Throws(() => client.Update("owner", null, 42, "updated comment")); Assert.Throws(() => client.Update("owner", "name", 42, null)); Assert.Throws(() => client.Update(1, 42, null)); Assert.Throws(() => client.Update("", "name", 42, "updated comment")); Assert.Throws(() => client.Update("owner", "", 42, "updated comment")); } } public class TheDeleteMethod { [Fact] public void DeletesCorrectUrl() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.Delete("fake", "repo", 42); githubClient.Repository.Comment.Received().Delete("fake", "repo", 42); } [Fact] public void DeletesCorrectUrlWithRepositoryId() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); client.Delete(1, 42); githubClient.Repository.Comment.Received().Delete(1, 42); } [Fact] public void EnsuresNonNullArgumentsOrEmpty() { var githubClient = Substitute.For(); var client = new ObservableRepositoryCommentsClient(githubClient); Assert.Throws(() => client.Delete(null, "name", 42)); Assert.Throws(() => client.Delete("owner", null, 42)); Assert.Throws(() => client.Delete("", "name", 42)); Assert.Throws(() => client.Delete("owner", "", 42)); } } } }