using System; using System.Collections.Generic; using NSubstitute; using Octokit.Reactive; using Xunit; namespace Octokit.Tests.Reactive { public class ObservableWatchedClientTests { public class TheCtor { [Fact] public void EnsuresNonNullArguments() { Assert.Throws(() => new ObservableWatchedClient(null)); } } public class TheGetAllForCurrentMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); client.GetAllForCurrent(); connection.Received().Get>(ApiUrls.Watched(), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithApiOptions() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); var options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllForCurrent(options); connection.Received().Get>(ApiUrls.Watched(), Arg.Is>(d => d.Count == 2)); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); Assert.Throws(() => client.GetAllForCurrent(null)); } } public class TheGetAllForUserMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); client.GetAllForUser("jugglingnutcase"); connection.Received().Get>(ApiUrls.WatchedByUser("jugglingnutcase"), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithApiOptions() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); var options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllForUser("jugglingnutcase", options); connection.Received().Get>(ApiUrls.WatchedByUser("jugglingnutcase"), Arg.Is>(d => d.Count == 2)); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); Assert.Throws(() => client.GetAllForUser(null)); Assert.Throws(() => client.GetAllForUser(null, ApiOptions.None)); Assert.Throws(() => client.GetAllForUser("user", null)); Assert.Throws(() => client.GetAllForUser("")); Assert.Throws(() => client.GetAllForUser("", ApiOptions.None)); } } public class TheGetAllWatchersMethod { [Fact] public void RequestsCorrectUrl() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); client.GetAllWatchers("jugglingnutcase", "katiejamie"); connection.Received().Get>(ApiUrls.Watchers("jugglingnutcase", "katiejamie"), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); client.GetAllWatchers(1); connection.Received().Get>(ApiUrls.Watchers(1), Args.EmptyDictionary); } [Fact] public void RequestsCorrectUrlWithApiOptions() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); ApiOptions options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllWatchers("jugglingnutcase", "katiejamie", options); connection.Received().Get>(ApiUrls.Watchers("jugglingnutcase", "katiejamie"), Arg.Is>(d => d.Count == 2)); } [Fact] public void RequestsCorrectUrlWithRepositoryIdWithApiOptions() { var connection = Substitute.For(); var gitHubClient = Substitute.For(); gitHubClient.Connection.Returns(connection); var client = new ObservableWatchedClient(gitHubClient); ApiOptions options = new ApiOptions { StartPage = 1, PageCount = 1, PageSize = 1 }; client.GetAllWatchers(1, options); connection.Received().Get>(ApiUrls.Watchers(1), Arg.Is>(d => d.Count == 2)); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); Assert.Throws(() => client.GetAllWatchers(null, "name")); Assert.Throws(() => client.GetAllWatchers("owner", null)); Assert.Throws(() => client.GetAllWatchers(null, "name", ApiOptions.None)); Assert.Throws(() => client.GetAllWatchers("owner", null, ApiOptions.None)); Assert.Throws(() => client.GetAllWatchers("owner", "name", null)); Assert.Throws(() => client.GetAllWatchers(1, null)); Assert.Throws(() => client.GetAllWatchers("", "name")); Assert.Throws(() => client.GetAllWatchers("owner", "")); Assert.Throws(() => client.GetAllWatchers("", "name", ApiOptions.None)); Assert.Throws(() => client.GetAllWatchers("owner", "", ApiOptions.None)); } } public class TheCheckWatchedMethod { [Fact] public void RequestsCorrectUrl() { var gitHub = Substitute.For(); var client = new ObservableWatchedClient(gitHub); client.CheckWatched("owner", "name"); gitHub.Activity.Watching.Received().CheckWatched("owner", "name"); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHub = Substitute.For(); var client = new ObservableWatchedClient(gitHub); client.CheckWatched(1); gitHub.Activity.Watching.Received().CheckWatched(1); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); Assert.Throws(() => client.CheckWatched(null, "name")); Assert.Throws(() => client.CheckWatched("owner", null)); Assert.Throws(() => client.CheckWatched("", "name")); Assert.Throws(() => client.CheckWatched("owner", "")); } } public class TheWatchRepoMethod { [Fact] public void RequestsCorrectUrl() { var gitHub = Substitute.For(); var client = new ObservableWatchedClient(gitHub); client.WatchRepo("owner", "name", new NewSubscription()); gitHub.Activity.Watching.Received().WatchRepo("owner", "name", Arg.Any()); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHub = Substitute.For(); var client = new ObservableWatchedClient(gitHub); client.WatchRepo(1, new NewSubscription()); gitHub.Activity.Watching.Received().WatchRepo(1, Arg.Any()); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); var subscription = new NewSubscription(); Assert.Throws(() => client.WatchRepo(null, "name", subscription)); Assert.Throws(() => client.WatchRepo("owner", null, subscription)); Assert.Throws(() => client.WatchRepo("owner", "name", null)); Assert.Throws(() => client.WatchRepo(1, null)); Assert.Throws(() => client.WatchRepo("", "name", subscription)); Assert.Throws(() => client.WatchRepo("owner", "", subscription)); } } public class TheUnWatchRepoMethod { [Fact] public void RequestsCorrectUrl() { var gitHub = Substitute.For(); var client = new ObservableWatchedClient(gitHub); client.UnwatchRepo("owner", "name"); gitHub.Activity.Watching.Received().UnwatchRepo("owner", "name"); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHub = Substitute.For(); var client = new ObservableWatchedClient(gitHub); client.UnwatchRepo(1); gitHub.Activity.Watching.Received().UnwatchRepo(1); } [Fact] public void EnsuresNonNullArguments() { var client = new ObservableWatchedClient(Substitute.For()); Assert.Throws(() => client.UnwatchRepo(null, "name")); Assert.Throws(() => client.UnwatchRepo("owner", null)); Assert.Throws(() => client.UnwatchRepo("", "name")); Assert.Throws(() => client.UnwatchRepo("owner", "")); } } } }