using System; using NSubstitute; using Octokit.Reactive; using Xunit; namespace Octokit.Tests.Reactive { public class ObservableReferencesClientTests { public class TheCtor { [Fact] public void EnsuresNonNullArguments() { Assert.Throws(() => new ObservableReferencesClient(null)); } } public class TheGetMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableReferencesClient(Substitute.For()); Assert.Throws(() => client.Get(null, "name", "heads/develop")); Assert.Throws(() => client.Get("owner", null, "heads/develop")); Assert.Throws(() => client.Get("owner", "name", null)); Assert.Throws(() => client.Get(1, null)); Assert.Throws(() => client.Get("", "name", "heads/develop")); Assert.Throws(() => client.Get("owner", "", "heads/develop")); Assert.Throws(() => client.Get("owner", "name", "")); Assert.Throws(() => client.Get(1, "")); } [Fact] public void RequestsCorrectUrl() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Get("owner", "repo", "heads/develop"); gitHubClient.Received().Git.Reference.Get("owner", "repo", "heads/develop"); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Get(1, "heads/develop"); gitHubClient.Received().Git.Reference.Get(1, "heads/develop"); } } public class TheGetAllMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableReferencesClient(Substitute.For()); Assert.Throws(() => client.GetAll(null, "name")); Assert.Throws(() => client.GetAll("owner", null)); Assert.Throws(() => client.GetAll("", "name")); Assert.Throws(() => client.GetAll("owner", "")); } [Fact] public void RequestsCorrectUrl() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.GetAll("owner", "repo"); gitHubClient.Received().Git.Reference.GetAll("owner", "repo"); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.GetAll(1); gitHubClient.Received().Git.Reference.GetAll(1); } } public class TheGetAllForSubNamespaceMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableReferencesClient(Substitute.For()); Assert.Throws(() => client.GetAllForSubNamespace(null, "name", "heads")); Assert.Throws(() => client.GetAllForSubNamespace("owner", null, "heads")); Assert.Throws(() => client.GetAllForSubNamespace("owner", "name", null)); Assert.Throws(() => client.GetAllForSubNamespace(1, null)); Assert.Throws(() => client.GetAllForSubNamespace("", "name", "heads")); Assert.Throws(() => client.GetAllForSubNamespace("owner", "", "heads")); Assert.Throws(() => client.GetAllForSubNamespace("owner", "name", "")); Assert.Throws(() => client.GetAllForSubNamespace(1, "")); } [Fact] public void RequestsCorrectUrl() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.GetAllForSubNamespace("owner", "repo", "heads"); gitHubClient.Received().Git.Reference.GetAllForSubNamespace("owner", "repo", "heads"); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.GetAllForSubNamespace(1, "heads"); gitHubClient.Received().Git.Reference.GetAllForSubNamespace(1, "heads"); } } public class TheCreateMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableReferencesClient(Substitute.For()); Assert.Throws(() => client.Create(null, "name", new NewReference("heads/develop", "sha"))); Assert.Throws(() => client.Create("owner", null, new NewReference("heads/develop", "sha"))); Assert.Throws(() => client.Create("owner", "name", null)); Assert.Throws(() => client.Create(1, null)); Assert.Throws(() => client.Create("", "name", new NewReference("heads/develop", "sha"))); Assert.Throws(() => client.Create("owner", "", new NewReference("heads/develop", "sha"))); } [Fact] public void PostsToCorrectUrl() { var newReference = new NewReference("heads/develop", "sha"); var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Create("owner", "repo", newReference); gitHubClient.Received().Git.Reference.Create("owner", "repo", newReference); } [Fact] public void PostsToCorrectUrlWithRepositoryId() { var newReference = new NewReference("heads/develop", "sha"); var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Create(1, newReference); gitHubClient.Received().Git.Reference.Create(1, newReference); } } public class TheUpdateMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableReferencesClient(Substitute.For()); Assert.Throws(() => client.Update(null, "name", "heads/develop", new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update("owner", null, "heads/develop", new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update("owner", "name", null, new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update("owner", "name", "heads/develop", null)); Assert.Throws(() => client.Update(1, null, new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update(1, "heads/develop", null)); Assert.Throws(() => client.Update("", "name", "heads/develop", new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update("owner", "", "heads/develop", new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update("owner", "name", "", new ReferenceUpdate("sha"))); Assert.Throws(() => client.Update(1, "", new ReferenceUpdate("sha"))); } [Fact] public void PostsToCorrectUrl() { var referenceUpdate = new ReferenceUpdate("sha"); var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Update("owner", "repo", "heads/develop", referenceUpdate); gitHubClient.Received().Git.Reference.Update("owner", "repo", "heads/develop", referenceUpdate); } [Fact] public void PostsToCorrectUrlWithRepositoryId() { var referenceUpdate = new ReferenceUpdate("sha"); var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Update(1, "heads/develop", referenceUpdate); gitHubClient.Received().Git.Reference.Update(1, "heads/develop", referenceUpdate); } } public class TheDeleteMethod { [Fact] public void EnsuresNonNullArguments() { var client = new ObservableReferencesClient(Substitute.For()); Assert.Throws(() => client.Delete(null, "name", "heads/develop")); Assert.Throws(() => client.Delete("owner", null, "heads/develop")); Assert.Throws(() => client.Delete("owner", "name", null)); Assert.Throws(() => client.Delete(1, null)); Assert.Throws(() => client.Delete("", "name", "heads/develop")); Assert.Throws(() => client.Delete("owner", "", "heads/develop")); Assert.Throws(() => client.Delete("owner", "name", "")); Assert.Throws(() => client.Delete(1, "")); } [Fact] public void RequestsCorrectUrl() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Delete("owner", "repo", "heads/develop"); gitHubClient.Received().Git.Reference.Delete("owner", "repo", "heads/develop"); } [Fact] public void RequestsCorrectUrlWithRepositoryId() { var gitHubClient = Substitute.For(); var client = new ObservableReferencesClient(gitHubClient); client.Delete(1, "heads/develop"); gitHubClient.Received().Git.Reference.Delete(1, "heads/develop"); } } } }