|
| 1 | +using GitHub.App.Services; |
| 2 | +using NUnit.Framework; |
| 3 | + |
| 4 | +public class GitHubContextServiceTests |
| 5 | +{ |
| 6 | + public class TheFindContextFromUrlMethod |
| 7 | + { |
| 8 | + [TestCase("https://github.com", null)] |
| 9 | + [TestCase("https://github.com/github", "github")] |
| 10 | + [TestCase("https://github.com/github/VisualStudio", "github")] |
| 11 | + [TestCase("https://github.com/github/VisualStudio/blob/master/README.md", "github")] |
| 12 | + public void Owner(string url, string expectOwner) |
| 13 | + { |
| 14 | + var target = new GitHubContextService(); |
| 15 | + |
| 16 | + var context = target.FindContextFromUrl(url); |
| 17 | + |
| 18 | + Assert.That(context.Owner, Is.EqualTo(expectOwner)); |
| 19 | + } |
| 20 | + |
| 21 | + [TestCase("https://github.com", null)] |
| 22 | + [TestCase("https://github.com/github", null)] |
| 23 | + [TestCase("https://github.com/github/VisualStudio", "VisualStudio")] |
| 24 | + [TestCase("https://github.com/github/VisualStudio/blob/master/README.md", "VisualStudio")] |
| 25 | + public void RepositoryName(string url, string expectRepositoryName) |
| 26 | + { |
| 27 | + var target = new GitHubContextService(); |
| 28 | + |
| 29 | + var context = target.FindContextFromUrl(url); |
| 30 | + |
| 31 | + Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName)); |
| 32 | + } |
| 33 | + |
| 34 | + [TestCase("https://github.com", "github.com")] |
| 35 | + [TestCase("https://github.com/github", "github.com")] |
| 36 | + [TestCase("https://github.com/github/VisualStudio", "github.com")] |
| 37 | + [TestCase("https://github.com/github/VisualStudio/blob/master/README.md", "github.com")] |
| 38 | + public void Host(string url, string expectHost) |
| 39 | + { |
| 40 | + var target = new GitHubContextService(); |
| 41 | + |
| 42 | + var context = target.FindContextFromUrl(url); |
| 43 | + |
| 44 | + Assert.That(context.Host, Is.EqualTo(expectHost)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public class TheFindContextFromWindowTitleMethod |
| 49 | + { |
| 50 | + [TestCase("github/0123456789: Description - Google Chrome", "0123456789")] |
| 51 | + [TestCase("github/abcdefghijklmnopqrstuvwxyz: Description - Google Chrome", "abcdefghijklmnopqrstuvwxyz")] |
| 52 | + [TestCase("github/ABCDEFGHIJKLMNOPQRSTUVWXYZ: Description - Google Chrome", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")] |
| 53 | + [TestCase("github/_: Description - Google Chrome", "_")] |
| 54 | + [TestCase("github/.: Description - Google Chrome", ".")] |
| 55 | + [TestCase("github/-: Description - Google Chrome", "-")] |
| 56 | + [TestCase("github/$: Description - Google Chrome", null, Description = "Must contain only letters, numbers, `_`, `.` or `-`")] |
| 57 | + public void RepositoryName(string windowTitle, string expectRepositoryName) |
| 58 | + { |
| 59 | + var target = new GitHubContextService(); |
| 60 | + |
| 61 | + var context = target.FindContextFromWindowTitle(windowTitle); |
| 62 | + |
| 63 | + Assert.That(context?.RepositoryName, Is.EqualTo(expectRepositoryName)); |
| 64 | + } |
| 65 | + |
| 66 | + [TestCase("0123456789/Repository: Description - Google Chrome", "0123456789")] |
| 67 | + [TestCase("abcdefghijklmnopqrstuvwxyz/Repository: Description - Google Chrome", "abcdefghijklmnopqrstuvwxyz")] |
| 68 | + [TestCase("ABCDEFGHIJKLMNOPQRSTUVWXYZ/Repository: Description - Google Chrome", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")] |
| 69 | + [TestCase("a_/Repository: Description - Google Chrome", "a_")] |
| 70 | + [TestCase("a-/Repository: Description - Google Chrome", "a-")] |
| 71 | + [TestCase("_/Repository: Description - Google Chrome", null, Description = "Must start with letter or number")] |
| 72 | + [TestCase("-/Repository: Description - Google Chrome", null, Description = "Must start with letter or number")] |
| 73 | + public void Owner(string windowTitle, string expectOwner) |
| 74 | + { |
| 75 | + var target = new GitHubContextService(); |
| 76 | + |
| 77 | + var context = target.FindContextFromWindowTitle(windowTitle); |
| 78 | + |
| 79 | + Assert.That(context?.Owner, Is.EqualTo(expectOwner)); |
| 80 | + } |
| 81 | + |
| 82 | + // They can include slash / for hierarchical (directory) grouping |
| 83 | + [TestCase("a/b", "a/b", Description = "")] |
| 84 | + [TestCase("aaa/bbb", "aaa/bbb", Description = "")] |
| 85 | + |
| 86 | + // They cannot have space, tilde ~, caret ^, or colon : anywhere. |
| 87 | + [TestCase("a b", null)] |
| 88 | + [TestCase("a~b", null)] |
| 89 | + [TestCase("a^b", null)] |
| 90 | + [TestCase("a:b", null)] |
| 91 | + |
| 92 | + // They cannot have question-mark ?, asterisk *, or open bracket [ anywhere. |
| 93 | + [TestCase("a?b", null)] |
| 94 | + [TestCase("a*b", null)] |
| 95 | + [TestCase("a[b", null)] |
| 96 | + |
| 97 | + [TestCase(@"a\b", null, Description = @"They cannot contain a \")] |
| 98 | + |
| 99 | + // Simple case |
| 100 | + [TestCase("master", "master")] |
| 101 | + |
| 102 | + // There are many symbols they can contain |
| 103 | + [TestCase("!@#$%&()_+-=", "!@#$%&()_+-=")] |
| 104 | + |
| 105 | + [TestCase("/a", null, Description = "They cannot begin a slash")] |
| 106 | + [TestCase("a/", null, Description = "They cannot end with a slash")] |
| 107 | + [TestCase("../b", null, Description = "no slash-separated component can begin with a dot")] |
| 108 | + [TestCase(".a/b", null, Description = "no slash-separated component can begin with a dot")] |
| 109 | + [TestCase("a/.b", null, Description = "no slash-separated component can begin with a dot")] |
| 110 | + |
| 111 | + // There are some checks we aren't doing, see https://git-scm.com/docs/git-check-ref-format |
| 112 | + // They cannot have ASCII control characters(i.e.bytes whose values are lower than \040, or \177 DEL) |
| 113 | + // [TestCase("a/b.lock", null, Description = "or end with the sequence.lock")] |
| 114 | + // [TestCase("a..b", null, Description = "They cannot have two consecutive dots..anywhere")] |
| 115 | + // [TestCase("a.", null, Description = "They cannot end with a dot")] |
| 116 | + // [TestCase("@{a", null, Description = "They cannot contain a sequence @{")] |
| 117 | + // [TestCase("@", null, Description = "They cannot be the single character @")] |
| 118 | + public void Branch(string branch, string expectBranch) |
| 119 | + { |
| 120 | + var windowTitle = $"VisualStudio/src/GitHub.VisualStudio/Resources/icons at {branch} · github/VisualStudio - Google Chrome"; |
| 121 | + var target = new GitHubContextService(); |
| 122 | + |
| 123 | + var context = target.FindContextFromWindowTitle(windowTitle); |
| 124 | + |
| 125 | + Assert.That(context?.Branch, Is.EqualTo(expectBranch)); |
| 126 | + } |
| 127 | + |
| 128 | + [TestCase("github/VisualStudio: GitHub Extension for Visual Studio - Google Chrome", "github", "VisualStudio", null)] |
| 129 | + [TestCase("Branches · github/VisualStudio - Google Chrome", "github", "VisualStudio", null)] |
| 130 | + [TestCase("github/VisualStudio at build/appveyor-fixes - Google Chrome", "github", "VisualStudio", "build/appveyor-fixes")] |
| 131 | + [TestCase("[spike] Open from GitHub URL by jcansdale · Pull Request #1763 · github/VisualStudio - Google Chrome", "github", "VisualStudio", null)] |
| 132 | + [TestCase("Consider adding C# code style preferences to editorconfig · Issue #1750 · github/VisualStudio - Google Chrome", "github", "VisualStudio", null)] |
| 133 | + [TestCase("VisualStudio/mark_github.xaml at master · github/VisualStudio - Google Chrome", "github", "VisualStudio", "master")] |
| 134 | + [TestCase("VisualStudio/src/GitHub.VisualStudio/Resources/icons at master · github/VisualStudio - Google Chrome", "github", "VisualStudio", "master")] |
| 135 | + [TestCase("VisualStudio/GitHub.Exports.csproj at 89484dc25a3a475d3253afdc3bd3ddd6c6999c3b · github/VisualStudio - Google Chrome", "github", "VisualStudio", "89484dc25a3a475d3253afdc3bd3ddd6c6999c3b")] |
| 136 | + public void OwnerRepositoryBranch(string windowTitle, string expectOwner, string expectRepositoryName, string expectBranch) |
| 137 | + { |
| 138 | + var target = new GitHubContextService(); |
| 139 | + |
| 140 | + var context = target.FindContextFromWindowTitle(windowTitle); |
| 141 | + |
| 142 | + Assert.That(context.Owner, Is.EqualTo(expectOwner)); |
| 143 | + Assert.That(context.RepositoryName, Is.EqualTo(expectRepositoryName)); |
| 144 | + Assert.That(context.Branch, Is.EqualTo(expectBranch)); |
| 145 | + } |
| 146 | + |
| 147 | + [TestCase("[spike] Open from GitHub URL by jcansdale · Pull Request #1763 · github/VisualStudio - Google Chrome", 1763)] |
| 148 | + public void PullRequest(string windowTitle, int expectPullRequest) |
| 149 | + { |
| 150 | + var target = new GitHubContextService(); |
| 151 | + |
| 152 | + var context = target.FindContextFromWindowTitle(windowTitle); |
| 153 | + |
| 154 | + Assert.That(context.PullRequest, Is.EqualTo(expectPullRequest)); |
| 155 | + } |
| 156 | + |
| 157 | + [TestCase("Consider adding C# code style preferences to editorconfig · Issue #1750 · github/VisualStudio - Google Chrome", 1750)] |
| 158 | + public void Issue(string windowTitle, int expectIssue) |
| 159 | + { |
| 160 | + var target = new GitHubContextService(); |
| 161 | + |
| 162 | + var context = target.FindContextFromWindowTitle(windowTitle); |
| 163 | + |
| 164 | + Assert.That(context.Issue, Is.EqualTo(expectIssue)); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments