FilePickerTests pass

This commit is contained in:
Timothy Baldridge 2020-03-28 15:52:43 -06:00
parent b8afcfa781
commit 3fc4464a14
5 changed files with 146 additions and 151 deletions

View File

@ -7,7 +7,7 @@ namespace Wabbajack.App.Test
{
public class BasicUITests
{
/*
[StaFact]
public async Task CanCompileASimpleModlist()
{
@ -20,5 +20,6 @@ namespace Wabbajack.App.Test
window.Close();
Assert.True(true);
}
*/
}
}

View File

@ -1,63 +1,49 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.IO;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using DynamicData;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Wabbajack.Common;
using Wabbajack;
using Xunit;
namespace Wabbajack.Test
{
[TestClass]
public class FilePickerTests
{
public static TempFile CreateSetFile(FilePickerVM vm)
{
var temp = new TempFile();
using (new FileStream(temp.File.FullName, FileMode.CreateNew)) { }
vm.TargetPath = temp.File.FullName;
vm.TargetPath = temp.Path;
return temp;
}
public static TempFolder CreateSetFolder(FilePickerVM vm)
{
await var temp = new TempFolder();
Directory.CreateDirectory(temp.Dir.FullName);
vm.TargetPath = temp.Dir.FullName;
return temp;
}
[TestMethod]
[Fact]
public async Task Stock()
{
var vm = new FilePickerVM();
Assert.AreEqual(FilePickerVM.PathTypeOptions.Off, vm.PathType);
Assert.AreEqual(FilePickerVM.CheckOptions.Off, vm.ExistCheckOption);
Assert.Equal(FilePickerVM.PathTypeOptions.Off, vm.PathType);
Assert.Equal(FilePickerVM.CheckOptions.Off, vm.ExistCheckOption);
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task FileNoExistsCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task FileNoExistsCheck_Exists()
{
var vm = new FilePickerVM();
@ -66,27 +52,27 @@ namespace Wabbajack.Test
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task ExistCheckTypeOff_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Off;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task ExistCheckTypeOff_Exists()
{
var vm = new FilePickerVM();
@ -95,41 +81,41 @@ namespace Wabbajack.Test
vm.PathType = FilePickerVM.PathTypeOptions.Off;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task FileIfNotEmptyCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task FileIfNotEmptyCheck_SetPath_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.TargetPath = "SomePath.jpg";
vm.TargetPath = (AbsolutePath)"SomePath.jpg";
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
[TestMethod]
[Fact]
public async Task FileIfNotEmptyCheck_Exists()
{
var vm = new FilePickerVM();
@ -138,27 +124,27 @@ namespace Wabbajack.Test
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
Assert.IsTrue(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task FileOnExistsCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
[TestMethod]
[Fact]
public async Task FileOnExistsCheck_Exists()
{
var vm = new FilePickerVM();
@ -167,109 +153,109 @@ namespace Wabbajack.Test
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsTrue(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task FolderIfNotEmptyCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task FolderIfNotEmptyCheck_SetPath_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.TargetPath = "SomePath.jpg";
vm.TargetPath = (AbsolutePath)"SomePath.jpg";
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
[TestMethod]
[Fact]
public async Task FolderIfNotEmptyCheck_Exists()
{
var vm = new FilePickerVM();
using (CreateSetFolder(vm))
await using (CreateSetFolder(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
await Task.Delay(250);
Assert.IsTrue(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task FolderOnExistsCheck_DoesNotExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
[TestMethod]
[Fact]
public async Task FolderOnExistsCheck_Exists()
{
var vm = new FilePickerVM();
using (CreateSetFolder(vm))
await using (CreateSetFolder(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsTrue(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.True(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task AdditionalError_Success()
{
var vm = new FilePickerVM();
vm.AdditionalError = Observable.Return<IErrorResponse>(ErrorResponse.Succeed());
await Task.Delay(250);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task AdditionalError_Fail()
{
var vm = new FilePickerVM();
string errText = "An error";
vm.AdditionalError = Observable.Return<IErrorResponse>(ErrorResponse.Fail(errText));
await Task.Delay(250);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(errText, vm.ErrorTooltip);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(errText, vm.ErrorTooltip);
}
[TestMethod]
[Fact]
public async Task FileExistsButSetToFolder()
{
var vm = new FilePickerVM();
@ -278,30 +264,30 @@ namespace Wabbajack.Test
vm.PathType = FilePickerVM.PathTypeOptions.Folder;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
}
[TestMethod]
[Fact]
public async Task FolderExistsButSetToFile()
{
var vm = new FilePickerVM();
using (CreateSetFolder(vm))
await using (CreateSetFolder(vm))
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.On;
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.PathDoesNotExistText, vm.ErrorTooltip);
}
}
[TestMethod]
[Fact]
public async Task FileWithFilters_Passes()
{
var vm = new FilePickerVM();
@ -309,16 +295,16 @@ namespace Wabbajack.Test
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{Path.GetExtension(vm.TargetPath)}"));
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{vm.TargetPath.Extension}"));
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
}
[TestMethod]
[Fact]
public async Task FileWithFilters_ExistsButFails()
{
var vm = new FilePickerVM();
@ -326,43 +312,52 @@ namespace Wabbajack.Test
{
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{Path.GetExtension(vm.TargetPath)}z"));
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{vm.TargetPath.Extension}z"));
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsFalse(vm.ErrorState.Succeeded);
Assert.IsTrue(vm.InError);
Assert.AreEqual(FilePickerVM.DoesNotPassFiltersText, vm.ErrorTooltip);
Assert.False(vm.Exists);
Assert.False(vm.ErrorState.Succeeded);
Assert.True(vm.InError);
Assert.Equal(FilePickerVM.DoesNotPassFiltersText, vm.ErrorTooltip);
}
}
[TestMethod]
[Fact]
public async Task FileWithFilters_PassesButDoesntExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
vm.TargetPath = "SomePath.png";
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{Path.GetExtension(vm.TargetPath)}"));
vm.TargetPath = (AbsolutePath)"SomePath.png";
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{vm.TargetPath.Extension}"));
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded, vm.ErrorState.Reason);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
[TestMethod]
[Fact]
public async Task FileWithFilters_IfNotEmptyCheck_DoesntExist()
{
var vm = new FilePickerVM();
vm.PathType = FilePickerVM.PathTypeOptions.File;
vm.ExistCheckOption = FilePickerVM.CheckOptions.Off;
vm.FilterCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty;
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{Path.GetExtension(vm.TargetPath)}"));
var vm = new FilePickerVM
{
PathType = FilePickerVM.PathTypeOptions.File,
ExistCheckOption = FilePickerVM.CheckOptions.Off,
FilterCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty
};
vm.Filters.Add(new Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogFilter("test", $"*.{vm.TargetPath.Extension}"));
await Task.Delay(250);
Assert.IsFalse(vm.Exists);
Assert.IsTrue(vm.ErrorState.Succeeded);
Assert.IsFalse(vm.InError);
Assert.IsTrue(string.IsNullOrEmpty(vm.ErrorTooltip));
Assert.False(vm.Exists);
Assert.True(vm.ErrorState.Succeeded);
Assert.False(vm.InError);
Assert.True(string.IsNullOrEmpty(vm.ErrorTooltip));
}
private static TempFolder CreateSetFolder(FilePickerVM vm)
{
var temp = new TempFolder();
vm.TargetPath = temp.Dir;
return temp;
}
}
}

View File

@ -657,7 +657,7 @@ namespace Wabbajack.Common
{
if (!_extension.StartsWith("."))
{
throw new InvalidDataException("Extensions must start with '.'");
throw new InvalidDataException($"Extensions must start with '.' got {_extension}");
}
}

View File

@ -21,7 +21,6 @@
<None Include="ZEditIntegrationTests.cs" />
<Compile Remove="CompilationStackTests.cs" />
<Compile Remove="FilePickerTests.cs" />
<None Include="FilePickerTests.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -98,7 +98,7 @@ namespace Wabbajack
case CheckOptions.Off:
return false;
case CheckOptions.IfPathNotEmpty:
return t.Path != null;
return t.Path != default;
case CheckOptions.On:
return true;
default:
@ -182,7 +182,7 @@ namespace Wabbajack
try
{
if (!query.Any(filter => filter.Extensions.Any(ext => new Extension(ext) == target.Extension))) return false;
if (!query.Any(filter => filter.Extensions.Any(ext => new Extension("." + ext) == target.Extension))) return false;
}
catch (ArgumentException)
{