Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jwallet committed Oct 3, 2018
1 parent 30bfbe1 commit 95d836e
Show file tree
Hide file tree
Showing 11 changed files with 59,065 additions and 9 deletions.
15 changes: 15 additions & 0 deletions EspionSpotify.Tests/EspionSpotify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
<Reference Include="Moq, Version=4.9.0.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.9.0\lib\net45\Moq.dll</HintPath>
</Reference>
<Reference Include="NAudio, Version=1.8.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.1.8.1\lib\net35\NAudio.dll</HintPath>
</Reference>
<Reference Include="NAudio.Lame, Version=1.0.5.22475, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NAudio.Lame.1.0.7\lib\net20\NAudio.Lame.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -73,6 +79,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="FileManagerTests.cs" />
<Compile Include="LastFMAPITests.cs" />
<Compile Include="SpotifyHandlerTests.cs" />
<Compile Include="SpotifyStatusTests.cs" />
Expand All @@ -92,6 +99,14 @@
<ItemGroup>
<Analyzer Include="..\packages\xunit.analyzers.0.10.0\analyzers\dotnet\cs\xunit.analyzers.dll" />
</ItemGroup>
<ItemGroup>
<Content Include="libmp3lame.32.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="libmp3lame.64.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
148 changes: 148 additions & 0 deletions EspionSpotify.Tests/FileManagerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using EspionSpotify.Models;
using NAudio.Lame;
using System.IO;
using System.Text.RegularExpressions;
using Xunit;

namespace EspionSpotify.Tests
{
public class FileManagerTests
{
private UserSettings _userSettings;
private Track _track;
private FileManager _fileManager;

private readonly string _windowsExlcudedChars = $"[{Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()))}]";

public FileManagerTests()
{
_userSettings = new UserSettings
{
OutputPath = "C:\\path",
Bitrate = LAMEPreset.ABR_128,
MediaFormat = Enums.MediaFormat.Mp3,
MinimumRecordedLengthSeconds = 30,
GroupByFoldersEnabled = false,
TrackTitleSeparator = " ",
OrderNumberInMediaTagEnabled = false,
OrderNumberInfrontOfFileEnabled = false,
EndingTrackDelayEnabled = true,
MuteAdsEnabled = true,
RecordUnknownTrackTypeEnabled = false,
InternalOrderNumber = 1,
DuplicateAlreadyRecordedTrack = true
};

_track = new Track
{
Title = "Title",
Artist = "Artist",
TitleExtended = "Live",
Ad = false
};

_fileManager = new FileManager(_userSettings, _track);
}

[Theory]
[InlineData("Artist - Title", 1, null, "Artist - Title.mp3")]
[InlineData("002 Artist - Title", 1, null, "002 Artist - Title.mp3")]
[InlineData("001_Artist_-_Title", 1, null, "001_Artist_-_Title.mp3")]
[InlineData("Artist - Title", 2, null, "Artist - Title 2.mp3")]
[InlineData("Artist_-_Title", 4, "C:\\path", "C:\\path\\Artist_-_Title 4.mp3")]
[InlineData("001 Title", 1, "C:\\path\\Artist", "C:\\path\\Artist\\001 Title.mp3")]
[InlineData("Artist - Title", 1, "C:\\path", "C:\\path\\Artist - Title.mp3")]
private void GetFileName_ReturnsTrackNameWithMediaFormat(string songName, int count, string path, string expectedResult)
{
var fileName = _fileManager.GetFileName(songName, count, path);

Assert.Equal(expectedResult, fileName);
}

[Theory]
[InlineData("C:\\path\\Artist", false, "Artist - Title - Live.mp3")]
[InlineData("C:\\path", false, "Artist - Title - Live.mp3")]
[InlineData("C:\\path\\Artist", true, "C:\\path\\Artist\\Artist - Title - Live.mp3")]
[InlineData("C:\\path", true, "C:\\path\\Artist - Title - Live.mp3")]
private void BuildFileName_ReturnsFileName(string path, bool includePath, string expectedResult)
{
var fileName = _fileManager.BuildFileName(path, includePath);

Assert.Equal(expectedResult, fileName);
}

[Theory]
[InlineData("C:\\path\\Artist", false, "100_Artist_-_Title_-_Live.mp3")]
[InlineData("C:\\path", false, "100_Artist_-_Title_-_Live.mp3")]
[InlineData("C:\\path\\Artist", true, "C:\\path\\Artist\\100_Artist_-_Title_-_Live.mp3")]
[InlineData("C:\\path", true, "C:\\path\\100_Artist_-_Title_-_Live.mp3")]
private void BuildFileName_ReturnsFileNameOrderNumbered(string path, bool includePath, string expectedResult)
{
_userSettings.OrderNumberInfrontOfFileEnabled = true;
_userSettings.TrackTitleSeparator = "_";
_userSettings.InternalOrderNumber = 100;

var fileName = _fileManager.BuildFileName(path, includePath);

Assert.Equal(expectedResult, fileName);
}

[Theory]
[InlineData("C:\\path\\Artist", false, "Title.mp3")]
[InlineData("C:\\path", false, "Title.mp3")]
[InlineData("C:\\path\\Artist", true, "C:\\path\\Artist\\Title.mp3")]
[InlineData("C:\\path", true, "C:\\path\\Title.mp3")]
private void BuildFileName_ReturnsFileNameGroupByFolders(string path, bool includePath, string expectedResult)
{
_userSettings.GroupByFoldersEnabled = true;

var fileName = _fileManager.BuildFileName(path, includePath);

Assert.Equal(expectedResult, fileName);
}

[Fact]
private void BuildFileName_ReturnsFileNameWav()
{
_userSettings.MediaFormat = Enums.MediaFormat.Wav;

var fileName = _fileManager.BuildFileName("C:\\path", true);

Assert.Equal("C:\\path\\Artist - Title - Live.wav", fileName);
}

[Fact]
private void CreateDirectory_ReturnsNoArtistFolderPath()
{
var artistFolder = _fileManager.CreateDirectory();

Assert.Null(artistFolder);
}

[Fact]
private void CreateDirectory_ReturnsArtistFolderPath()
{
_userSettings.GroupByFoldersEnabled = true;

var artistDir = Regex.Replace(_track.Artist, _windowsExlcudedChars, string.Empty);

var artistFolder = _fileManager.CreateDirectory();

Assert.Equal($"//{artistDir}", artistFolder);
}

[Fact]
private void DeleteFile_DeletesFolderWhenGroupByFoldersEnabled()
{
var artistDir = Regex.Replace(_track.Artist, _windowsExlcudedChars, string.Empty);
_userSettings.GroupByFoldersEnabled = true;

var currentFile = $"{_userSettings.OutputPath}//{artistDir}//{_track.ToString()}";

_fileManager.DeleteFile(currentFile);

Assert.False(Directory.Exists($"{_userSettings.OutputPath}//{artistDir}"));
Assert.False(File.Exists(currentFile));
}
}
}
2 changes: 2 additions & 0 deletions EspionSpotify.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<packages>
<package id="Castle.Core" version="4.3.1" targetFramework="net461" />
<package id="Moq" version="4.9.0" targetFramework="net461" />
<package id="NAudio" version="1.8.1" targetFramework="net461" />
<package id="NAudio.Lame" version="1.0.7" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net461" />
Expand Down
19 changes: 11 additions & 8 deletions EspionSpotify/FileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@

namespace EspionSpotify
{
internal class FileManager
public class FileManager
{
private readonly UserSettings _userSettings;
private readonly Track _track;

private const int FirstSongNameCount = 1;
private readonly string _windowsExlcudedChars = $"[{Regex.Escape(new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars()))}]";

internal FileManager(UserSettings userSettings, Track track)
public FileManager(UserSettings userSettings, Track track)
{
_userSettings = userSettings;
_track = track;
}

internal string GetFileName(string songName, int count, string path = null)
public string GetFileName(string songName, int count, string path = null)
{
var ending = _userSettings.MediaFormat.ToString().ToLower();
songName += count > FirstSongNameCount ? $"{_userSettings.TrackTitleSeparator}{count}" : string.Empty;
return path != null ? $"{path}\\{songName}.{ending}" : $"{songName}.{ending}";
}

internal string BuildFileName(string path, bool includePath = true)
public string BuildFileName(string path, bool includePath = true)
{
string songName;
var track = _userSettings.OrderNumber != -1 && _userSettings.OrderNumberInfrontOfFileEnabled ? $"{_userSettings.OrderNumber:000} " : null;
var track = _userSettings.OrderNumber?.ToString("000 ") ?? null;

if (_userSettings.GroupByFoldersEnabled)
{
Expand All @@ -56,7 +56,7 @@ internal string BuildFileName(string path, bool includePath = true)
return filename;
}

internal string CreateDirectory()
public string CreateDirectory()
{
string insertArtistDir = null;
var artistDir = Normalize.RemoveDiacritics(_track.Artist);
Expand All @@ -71,9 +71,12 @@ internal string CreateDirectory()
return insertArtistDir;
}

internal void DeleteFile(string currentFile)
public void DeleteFile(string currentFile)
{
File.Delete(currentFile);
if (File.Exists(currentFile))
{
File.Delete(currentFile);
}

if (_userSettings.GroupByFoldersEnabled)
{
Expand Down
2 changes: 1 addition & 1 deletion EspionSpotify/Models/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace EspionSpotify.Models
{
class UserSettings
public class UserSettings
{
public string OutputPath { get; set; }
public LAMEPreset Bitrate { get; set; }
Expand Down
Binary file added packages/NAudio.1.8.1/NAudio.1.8.1.nupkg
Binary file not shown.
Loading

0 comments on commit 95d836e

Please sign in to comment.