Skip to content

Commit

Permalink
Simplify test code by using new C# features
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 25, 2023
1 parent 3402390 commit c70bc36
Show file tree
Hide file tree
Showing 33 changed files with 121 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ protected override void OnInitialize()

Context = UnitTestSynchronizationContext.Create();

catalog = new AggregateCatalog();
catalog = new();
OnCatalogInitialize(catalog);

Container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
Container = new(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(Container);
Container.Compose(batch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void FileSystemWatcherService()
{
var path = @"C:\Music";
var fileService = Container.GetExportedValue<MockFileService>();
fileService.GetFilesStub = (dir, deep, f1, f2, c) => Task.FromResult((IReadOnlyList<string>)new[] { Path.Combine(path, "TestMP3.mp3"), Path.Combine(path, "Test42.mp3") });
fileService.GetFilesStub = (dir, deep, f1, f2, c) => Task.FromResult<IReadOnlyList<string>>([ Path.Combine(path, "TestMP3.mp3"), Path.Combine(path, "Test42.mp3") ]);

var fileSystemWatcherService = Container.GetExportedValue<MockFileSystemWatcherService>();
viewModel.FolderBrowser.CurrentPath = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ protected override void OnInitialize()
base.OnInitialize();

musicFileContext = Container.GetExportedValue<MockMusicFileContext>();
musicFiles = new ObservableCollection<MusicFile>()
{
musicFiles = [
musicFileContext.Create(@"C:\Users\Public\Music\Dancefloor\Culture Beat - Serenity.wav"),
musicFileContext.Create(@"C:\Culture Beat - Serenity - Epilog.wma"),
};
];
selectionService = Container.GetExportedValue<SelectionService>();
selectionService.Initialize(musicFiles);

playlistManager = new PlaylistManager();
playlistManager = new();
controller = Container.GetExportedValue<MusicPropertiesController>();
controller.PlaylistManager = playlistManager;
controller.Initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ protected override void OnInitialize()
base.OnInitialize();

var musicFileContext = Container.GetExportedValue<MockMusicFileContext>();
musicFiles = new ObservableCollection<MusicFile>()
{
musicFiles = [
musicFileContext.Create(@"C:\Users\Public\Music\Dancefloor\Culture Beat - Serenity.wav"),
musicFileContext.Create(@"C:\Culture Beat - Serenity - Epilog.wma"),
};
];
selectionService = Container.GetExportedValue<SelectionService>();
selectionService.Initialize(musicFiles);
playlistManager = new PlaylistManager();
playlistSettings = new PlaylistSettings();
playlistManager = new();
playlistSettings = new();
controller = Container.GetExportedValue<PlayerController>();
controller.PlaylistSettings = playlistSettings;
controller.PlaylistManager = playlistManager;
Expand Down Expand Up @@ -106,7 +105,7 @@ public void PlayAllAndSelected()
Assert.IsTrue(viewModel.PlayerService.PlaySelectedCommand.CanExecute(null));

// Insert a dummy playlist item which will be replaced by Play
playlistManager.AddAndReplaceItems(new[] { new PlaylistItem(MockMusicFile.CreateEmpty("empty.mp3")) });
playlistManager.AddAndReplaceItems([new PlaylistItem(MockMusicFile.CreateEmpty("empty.mp3")) ]);
Assert.AreEqual(1, playlistManager.Items.Count);

// Play all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ protected override void OnInitialize()
base.OnInitialize();

musicFileContext = Container.GetExportedValue<MockMusicFileContext>();
musicFiles = new ObservableCollection<MusicFile>()
{
musicFiles = [
musicFileContext.Create(@"C:\Users\Public\Music\Dancefloor\Culture Beat - Serenity.wav"),
musicFileContext.Create(@"C:\Culture Beat - Serenity - Epilog.wma"),
};
];
selectionService = Container.GetExportedValue<SelectionService>();
selectionService.Initialize(musicFiles);

playlistManager = new PlaylistManager();
playlistManager = new();
controller = Container.GetExportedValue<PlaylistController>();
controller.PlaylistSettings = new PlaylistSettings();
controller.PlaylistSettings = new();
controller.PlaylistManager = playlistManager;
controller.Initialize();
controller.Run();
Expand Down Expand Up @@ -133,7 +132,7 @@ public void ShowMusicPropertiesTest()

private static void SetSelection(PlaylistViewModel viewModel, params PlaylistItem[] items)
{
viewModel.SelectedPlaylistItem = items.Last();
viewModel.SelectedPlaylistItem = items[^1];
viewModel.SelectedPlaylistItems.Clear();
foreach (var x in items) viewModel.SelectedPlaylistItems.Add(x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ protected override void OnInitialize()
base.OnInitialize();

musicFileContext = Container.GetExportedValue<MockMusicFileContext>();
musicFiles = new ObservableCollection<MusicFile>()
{
musicFiles = [
musicFileContext.Create(@"C:\Users\Public\Music\Dancefloor\Culture Beat - Serenity.wav"),
musicFileContext.Create(@"C:\Culture Beat - Serenity - Epilog.wma"),
};
];
selectionService = Container.GetExportedValue<SelectionService>();
selectionService.Initialize(musicFiles);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Waf.MusicManager.Applications.DataModels;
using Waf.MusicManager.Applications.Services;

namespace Test.MusicManager.Applications.DataModels;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public void RaisePropertyChangedTest()

var musicFile = new MockMusicFile(new MusicMetadata(new TimeSpan(0, 3, 33), 320)
{
Artists = Array.Empty<string>(),
Artists = [],
Title = ""
}, @"C:\Users\Public\Music\Dancefloor\Culture Beat - Serenity.mp3");
var dataModel = new MusicFileDataModel(musicFile);

Assert.AreEqual("", dataModel.ArtistsString);

AssertHelper.PropertyChangedEvent(dataModel, x => x.ArtistsString, () => musicFile.Metadata!.Artists = new[] { "Culture Beat" });
AssertHelper.PropertyChangedEvent(dataModel, x => x.ArtistsString, () => musicFile.Metadata!.Artists = [ "Culture Beat" ]);
Assert.AreEqual("Culture Beat", dataModel.ArtistsString);

AssertHelper.PropertyChangedEvent(dataModel, x => x.ArtistsString, () => musicFile.Metadata!.Artists = new[] { "Culture Beat", "Second artist" });
AssertHelper.PropertyChangedEvent(dataModel, x => x.ArtistsString, () => musicFile.Metadata!.Artists = [ "Culture Beat", "Second artist" ]);
Assert.AreEqual("Culture Beat" + ls + " Second artist", dataModel.ArtistsString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ namespace Test.MusicManager.Applications.Services;
[Export, Export(typeof(IEnvironmentService))]
public class MockEnvironmentService : IEnvironmentService
{
public MockEnvironmentService()
{
MusicFilesToLoad = Array.Empty<string>();
}

public IReadOnlyList<string> MusicFilesToLoad { get; set; }
public IReadOnlyList<string> MusicFilesToLoad { get; set; } = [];

public string MusicPath { get; set; } = "";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.ComponentModel.Composition;
using Waf.MusicManager.Applications.DataModels;
using Waf.MusicManager.Applications.Services;

namespace Test.MusicManager.Applications.Services
Expand All @@ -15,13 +14,13 @@ public class MockFileService : IFileService

public Func<string, bool, string, string, CancellationToken, Task<IReadOnlyList<string>>>? GetFilesStub { get; set; }
public Task<IReadOnlyList<string>> GetFiles(string directory, bool deep, string userSearchFilter, string applicationSearchFilter, CancellationToken cancellation)
=> GetFilesStub?.Invoke(directory, deep, userSearchFilter, applicationSearchFilter, cancellation) ?? Task.FromResult((IReadOnlyList<string>)Array.Empty<string>());
=> GetFilesStub?.Invoke(directory, deep, userSearchFilter, applicationSearchFilter, cancellation) ?? Task.FromResult<IReadOnlyList<string>>([]);

public Task<FolderItem> GetFolderFromPath(string path) => Task.FromResult(new FolderItem(path, path));

public Task<string?> GetParentPath(string path) => throw new NotImplementedException();

public Task<IReadOnlyList<FolderItem>> GetSubFoldersFromPath(string path) => Task.FromResult((IReadOnlyList<FolderItem>)Array.Empty<FolderItem>());
public Task<IReadOnlyList<FolderItem>> GetSubFoldersFromPath(string path) => Task.FromResult<IReadOnlyList<FolderItem>>([]);

public bool IsFileSupported(string fileName) => true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ public void GetTitleTextTest()
{
var fileName = @"C:\Users\Public\Music\Dancefloor\Culture Beat - Serenity.mp3";

Assert.AreEqual("Culture Beat - Serenity", MusicTitleHelper.GetTitleText(fileName, Array.Empty<string>(), null));
Assert.AreEqual("Culture Beat - Serenity", MusicTitleHelper.GetTitleText(fileName, Array.Empty<string>(), ""));
Assert.AreEqual("Culture Beat - Serenity", MusicTitleHelper.GetTitleText(fileName, [], null));
Assert.AreEqual("Culture Beat - Serenity", MusicTitleHelper.GetTitleText(fileName, [], ""));

Assert.AreEqual("", MusicTitleHelper.GetTitleText(fileName, new[] { "Culture Beat" }, null));
Assert.AreEqual("", MusicTitleHelper.GetTitleText(fileName, new[] { "Culture Beat" }, ""));
Assert.AreEqual("", MusicTitleHelper.GetTitleText(fileName, [ "Culture Beat" ], null));
Assert.AreEqual("", MusicTitleHelper.GetTitleText(fileName, [ "Culture Beat" ], ""));

Assert.AreEqual("Serenity (Epilog)", MusicTitleHelper.GetTitleText(fileName, new[] { "Culture Beat" }, "Serenity (Epilog)"));
Assert.AreEqual("Serenity (Epilog)", MusicTitleHelper.GetTitleText(fileName, [ "Culture Beat" ], "Serenity (Epilog)"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void UpdateAutoFillFromFileNameTestCommand()
AssertHelper.CanExecuteChangedEvent(viewModel.AutoFillFromFileNameCommand, () => musicFile.Metadata!.Title = "");
Assert.IsTrue(viewModel.AutoFillFromFileNameCommand.CanExecute(null));

AssertHelper.CanExecuteChangedEvent(viewModel.AutoFillFromFileNameCommand, () => musicFile.Metadata!.Artists = new[] { "Culture Beat" });
AssertHelper.CanExecuteChangedEvent(viewModel.AutoFillFromFileNameCommand, () => musicFile.Metadata!.Artists = [ "Culture Beat" ]);
Assert.IsFalse(viewModel.AutoFillFromFileNameCommand.CanExecute(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ namespace Test.MusicManager.Applications.Views;
[Export(typeof(IPlayerView))]
public class MockPlayerView : MockView, IPlayerView
{
public MockPlayerView()
{
Position = TimeSpan.FromSeconds(33);
}

public TimeSpan Position { get; set; }
public TimeSpan Position { get; set; } = TimeSpan.FromSeconds(33);

public TimeSpan GetPosition() => Position;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void AutoFillFromFileName()
private void MetadataLoaded()
{
if (MusicFile?.IsMetadataLoaded != true) return;
metadataPropertyChangedProxy = WeakEvent.PropertyChanged.Add(MusicFile.Metadata, MetadataPropertyChanged);
metadataPropertyChangedProxy = WeakEvent.PropertyChanged.Add(MusicFile.Metadata!, MetadataPropertyChanged);
autoFillFromFileNameCommand.RaiseCanExecuteChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public MockMusicFile(MusicMetadata? metadata, string? fileName) : base(x => Task

public static MusicFile CreateEmpty(string fileName) => new MockMusicFile(new MusicMetadata(new TimeSpan(0, 3, 33), 320000)
{
Artists = Array.Empty<string>(),
Artists = [],
Title = ""
}, fileName);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Waf.UnitTesting;
using Waf.MusicManager.Domain;
using Waf.MusicManager.Domain.MusicFiles;
using Waf.MusicManager.Domain.Playlists;

Expand All @@ -14,12 +13,11 @@ public class PlaylistManagerTest : DomainTest
protected override void OnInitialize()
{
base.OnInitialize();
threeItems = new[]
{
new PlaylistItem(new MockMusicFile(new MusicMetadata(TimeSpan.FromMinutes(2), 256) { Album = "1" }, "")),
new PlaylistItem(new MockMusicFile(new MusicMetadata(TimeSpan.FromMinutes(2), 256) { Album = "2" }, "")),
new PlaylistItem(new MockMusicFile(new MusicMetadata(TimeSpan.FromMinutes(2), 256) { Album = "3" }, "")),
};
threeItems = [
new(new MockMusicFile(new(TimeSpan.FromMinutes(2), 256) { Album = "1" }, "")),
new(new MockMusicFile(new(TimeSpan.FromMinutes(2), 256) { Album = "2" }, "")),
new(new MockMusicFile(new(TimeSpan.FromMinutes(2), 256) { Album = "3" }, "")),
];
}

[TestMethod]
Expand Down Expand Up @@ -193,14 +191,14 @@ public void AddRemoveAndMoveItems()
manager.AddItems(threeItems);
Assert.AreEqual(3, manager.Items.Count);

manager.RemoveItems(new[] { threeItems[2] });
manager.RemoveItems([ threeItems[2] ]);
Assert.AreEqual(2, manager.Items.Count);

manager.AddAndReplaceItems(threeItems);
Assert.AreEqual(3, manager.Items.Count);

manager.MoveItems(2, new[] { manager.Items[0] });
AssertHelper.SequenceEqual(new[] { threeItems[1], threeItems[2], threeItems[0] }, manager.Items);
manager.MoveItems(2, [ manager.Items[0] ]);
AssertHelper.SequenceEqual([ threeItems[1], threeItems[2], threeItems[0] ], manager.Items);
}

[TestMethod]
Expand All @@ -213,9 +211,9 @@ public void PreviousAndNextDuringAddRemoveItems()
manager.NextItem();

Assert.IsTrue(manager.CanNextItem);
AssertHelper.PropertyChangedEvent(manager, x => x.CanNextItem, () => manager.RemoveItems(new[] { threeItems[2] }));
AssertHelper.PropertyChangedEvent(manager, x => x.CanNextItem, () => manager.RemoveItems([ threeItems[2] ]));
Assert.IsFalse(manager.CanNextItem);
AssertHelper.PropertyChangedEvent(manager, x => x.CanNextItem, () => manager.AddItems(new[] { threeItems[2] }));
AssertHelper.PropertyChangedEvent(manager, x => x.CanNextItem, () => manager.AddItems([ threeItems[2] ]));
Assert.IsTrue(manager.CanNextItem);

manager.NextItem();
Expand All @@ -224,7 +222,7 @@ public void PreviousAndNextDuringAddRemoveItems()
Assert.AreEqual(threeItems[1], manager.CurrentItem);

Assert.IsTrue(manager.CanPreviousItem);
AssertHelper.PropertyChangedEvent(manager, x => x.CanPreviousItem, () => manager.RemoveItems(new[] { threeItems[0] }));
AssertHelper.PropertyChangedEvent(manager, x => x.CanPreviousItem, () => manager.RemoveItems([ threeItems[0] ]));
Assert.IsFalse(manager.CanPreviousItem);
}

Expand All @@ -233,8 +231,8 @@ public void TotalDurationWithMetadataLoading()
{
var manager = new PlaylistManager();
Assert.AreEqual(TimeSpan.Zero, manager.TotalDuration);
var firstFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(10), 0), "");
manager.AddAndReplaceItems(new[] { new PlaylistItem(firstFile) });
var firstFile = new MockMusicFile(new(TimeSpan.FromSeconds(10), 0), "");
manager.AddAndReplaceItems([ new PlaylistItem(firstFile) ]);

var secondMetadata = new MusicMetadata(TimeSpan.FromSeconds(20), 0);
var secondFile = new MusicFile(async x =>
Expand All @@ -243,15 +241,15 @@ public void TotalDurationWithMetadataLoading()
return secondMetadata;
}, "");

manager.AddItems(new[] { new PlaylistItem(secondFile) });
manager.AddItems([ new PlaylistItem(secondFile) ]);
Assert.IsTrue(manager.IsTotalDurationEstimated);
Assert.AreEqual(TimeSpan.FromSeconds(20), manager.TotalDuration);
AssertHelper.PropertyChangedEvent(manager, x => x.TotalDuration, () => _ = secondFile.GetMetadataAsync().Result);
Assert.IsFalse(manager.IsTotalDurationEstimated);
Assert.AreEqual(TimeSpan.FromSeconds(30), manager.TotalDuration);

var thirdFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(30), 0), "");
AssertHelper.PropertyChangedEvent(manager, x => x.TotalDuration, () => manager.AddItems(new[] { new PlaylistItem(thirdFile) }));
AssertHelper.PropertyChangedEvent(manager, x => x.TotalDuration, () => manager.AddItems([ new PlaylistItem(thirdFile) ]));
Assert.AreEqual(TimeSpan.FromSeconds(60), manager.TotalDuration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class StatisticsHelperTest
[TestMethod]
public void TruncatedMeanTest()
{
Assert.AreEqual(0, StatisticsHelper.TruncatedMean(Array.Empty<double>(), 0.25));
Assert.AreEqual(0, StatisticsHelper.TruncatedMean([], 0.25));

Assert.AreEqual(6.5, StatisticsHelper.TruncatedMean(new double[] { 5, 8, 4, 38, 8, 6, 9, 7, 7, 3, 1, 6 }, 0.25));
Assert.AreEqual(6.5, StatisticsHelper.TruncatedMean([ 5, 8, 4, 38, 8, 6, 9, 7, 7, 3, 1, 6 ], 0.25));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TranscodeItemTest : DomainTest
[TestMethod]
public void PropertiesTest()
{
var musicFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile.wma");
var musicFile = new MockMusicFile(new(TimeSpan.FromSeconds(33), 320), "TestFile.wma");
var item = new TranscodeItem(musicFile, "TestFile.mp3");

Assert.AreEqual(musicFile, item.Source);
Expand All @@ -31,7 +31,7 @@ public void PropertiesTest()
[TestMethod]
public void TranscodeStatusTest()
{
var musicFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile.wma");
var musicFile = new MockMusicFile(new(TimeSpan.FromSeconds(33), 320), "TestFile.wma");
var item = new TranscodeItem(musicFile, "TestFile.mp3");

Assert.AreEqual(TranscodeStatus.Pending, item.TranscodeStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class TranscodingManagerTest : DomainTest
[TestMethod]
public void AddRemoveTranscodeItemsTest()
{
var musicFile1 = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile1.wma");
var musicFile2 = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile2.wma");
var musicFile1 = new MockMusicFile(new(TimeSpan.FromSeconds(33), 320), "TestFile1.wma");
var musicFile2 = new MockMusicFile(new(TimeSpan.FromSeconds(33), 320), "TestFile2.wma");
var item1 = new TranscodeItem(musicFile1, "TestFile1.mp3");
var item2 = new TranscodeItem(musicFile2, "TestFile2.mp3");

Expand All @@ -23,7 +23,7 @@ public void AddRemoveTranscodeItemsTest()
manager.AddTranscodeItem(item1);
manager.AddTranscodeItem(item2);

AssertHelper.SequenceEqual(new[] { item1, item2 }, manager.TranscodeItems);
AssertHelper.SequenceEqual([ item1, item2 ], manager.TranscodeItems);

manager.RemoveTranscodeItem(item1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void SaveAndLoadPlaylist()
Assert.AreEqual(2, viewModel.PlaylistManager.Items.Count);

var fileDialogService = Container.GetExportedValue<MockFileDialogService>();
fileDialogService.Result = new FileDialogResult(playlistFileName, new FileType("test", ".m3u"));
fileDialogService.Result = new(playlistFileName, new FileType("test", ".m3u"));
viewModel.SaveListCommand.Execute(null);

Assert.IsTrue(File.Exists(playlistFileName));
Expand Down
Loading

0 comments on commit c70bc36

Please sign in to comment.