Now, selected gitignore and license are never null

I really hate null references.
This commit is contained in:
Haacked 2015-03-21 22:18:52 -07:00
Родитель 86de22b507
Коммит 5f1b113322
5 изменённых файлов: 30 добавлений и 20 удалений

Просмотреть файл

@ -29,9 +29,9 @@ namespace GitHub.SampleData
Accounts = new ReactiveList<IAccount> { new AccountDesigner("GitHub") };
GitIgnoreTemplates = new ReactiveList<GitIgnoreItem>
{
new GitIgnoreItem("VisualStudio"),
new GitIgnoreItem("Wap"),
new GitIgnoreItem("WordPress")
GitIgnoreItem.Create("VisualStudio"),
GitIgnoreItem.Create("Wap"),
GitIgnoreItem.Create("WordPress")
};
Licenses = new ReactiveList<LicenseItem>

Просмотреть файл

@ -41,7 +41,7 @@ namespace GitHub.ViewModels
this.operatingSystem = operatingSystem;
RepositoryHost = hosts.GitHubHost;
this.repositoryCreationService = repositoryCreationService;
Accounts = RepositoryHost.Accounts ?? new ReactiveList<IAccount>();
Debug.Assert(Splat.ModeDetector.InUnitTestRunner() || Accounts.Any(), "There must be at least one account");
var selectedAccount = Accounts.FirstOrDefault();
@ -49,6 +49,8 @@ namespace GitHub.ViewModels
{
SelectedAccount = Accounts.FirstOrDefault();
}
SelectedGitIgnoreTemplate = GitIgnoreItem.None;
SelectedLicense = LicenseItem.None;
safeRepositoryName = this.WhenAny(x => x.RepositoryName, x => x.Value)
.Select(x => x != null ? GetSafeRepositoryName(x) : null)
@ -86,13 +88,12 @@ namespace GitHub.ViewModels
RepositoryHost.ApiClient
.GetGitIgnoreTemplates()
.ObserveOn(RxApp.MainThreadScheduler)
.Select(templateName => new GitIgnoreItem(templateName)))
.Select(GitIgnoreItem.Create))
.ToList()
.Subscribe(templates =>
{
GitIgnoreTemplates.AddRange(templates.OrderByDescending(template => template.Recommended));
Debug.Assert(GitIgnoreTemplates.Any(), "There should be at least one GitIgnoreTemplate");
SelectedGitIgnoreTemplate = GitIgnoreTemplates[0];
});
Licenses = new ReactiveList<LicenseItem>();
@ -107,7 +108,6 @@ namespace GitHub.ViewModels
{
Licenses.AddRange(licenses.OrderByDescending(lic => lic.Recommended));
Debug.Assert(Licenses.Any(), "There should be at least one license");
SelectedLicense = Licenses[0];
});
var canKeepPrivateObs = this.WhenAny(
@ -282,18 +282,16 @@ namespace GitHub.ViewModels
[AllowNull]
public GitIgnoreItem SelectedGitIgnoreTemplate
{
[return: AllowNull]
get { return selectedGitIgnoreTemplate; }
set { this.RaiseAndSetIfChanged(ref selectedGitIgnoreTemplate, value); }
set { this.RaiseAndSetIfChanged(ref selectedGitIgnoreTemplate, value ?? GitIgnoreItem.None); }
}
LicenseItem selectedLicense;
[AllowNull]
public LicenseItem SelectedLicense
{
[return: AllowNull]
get { return selectedLicense; }
set { this.RaiseAndSetIfChanged(ref selectedLicense, value); }
set { this.RaiseAndSetIfChanged(ref selectedLicense, value ?? LicenseItem.None); }
}
// These are the characters which are permitted when creating a repository name on GitHub The Website

Просмотреть файл

@ -1,17 +1,24 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace GitHub.Models
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class GitIgnoreItem
public sealed class GitIgnoreItem
{
public static readonly GitIgnoreItem None = new GitIgnoreItem("None");
readonly string[] recommendedIgnoreFiles = { "None", "VisualStudio", "Node", "Eclipse", "C++", "Windows" };
public GitIgnoreItem(string name)
static readonly GitIgnoreItem none = new GitIgnoreItem("None");
public static GitIgnoreItem None { get { return none; } }
public static GitIgnoreItem Create(string name)
{
return name.Equals("None", StringComparison.OrdinalIgnoreCase) ? none : new GitIgnoreItem(name);
}
GitIgnoreItem(string name)
{
Name = name;
Recommended = recommendedIgnoreFiles.Any(item => item.Equals(name, System.StringComparison.OrdinalIgnoreCase));

Просмотреть файл

@ -5,7 +5,8 @@ namespace GitHub.Models
{
public class LicenseItem
{
public static LicenseItem None = new LicenseItem();
static readonly LicenseItem none = new LicenseItem();
public static LicenseItem None { get { return none; } }
public LicenseItem(LicenseMetadata license)
{

Просмотреть файл

@ -464,9 +464,11 @@ public class RepositoryCreationViewModelTests
{
var creationService = Substitute.For<IRepositoryCreationService>();
var account = Substitute.For<IAccount>();
var hosts = Substitute.For<IRepositoryHosts>();
hosts.GitHubHost.Accounts.Returns(new ReactiveList<IAccount> { account });
var vm = new RepositoryCreationViewModel(
Substitute.For<IOperatingSystem>(),
Substitute.For<IRepositoryHosts>(),
hosts,
creationService);
vm.RepositoryName = "Krieger";
vm.BaseRepositoryPath = @"c:\dev";
@ -494,15 +496,17 @@ public class RepositoryCreationViewModelTests
{
var creationService = Substitute.For<IRepositoryCreationService>();
var account = Substitute.For<IAccount>();
var hosts = Substitute.For<IRepositoryHosts>();
hosts.GitHubHost.Accounts.Returns(new ReactiveList<IAccount> { account });
var vm = new RepositoryCreationViewModel(
Substitute.For<IOperatingSystem>(),
Substitute.For<IRepositoryHosts>(),
hosts,
creationService);
vm.RepositoryName = "Krieger";
vm.BaseRepositoryPath = @"c:\dev";
vm.SelectedAccount = account;
vm.KeepPrivate = false;
vm.SelectedGitIgnoreTemplate = new GitIgnoreItem("VisualStudio");
vm.SelectedGitIgnoreTemplate = GitIgnoreItem.Create("VisualStudio");
vm.CreateRepository.Execute(null);