Apply R# clean-up for reliability (#877)
* Apply R# clean-up for reliability - Migrate to NUnit4 Assert.That - Update dependencise versions - Fix LangVersion to 9.0 (until net core migration) - Reformat code - Apply 'var' style (easier for refactoring) - Remove redundant parentheses - Add braces on every condition blocks (1 line) - Uses new null checking patterns - Uses AutoProperties when possible - Uses string scalar type instead of class - Clean usings - Add .orig files in git ignore - Rename some properties to respect defined conventions and US format definition - ID -> Id - BPM -> Bpm - Finish -> End * Update R# settings according to the clean-up made
This commit is contained in:
1 parent
b9b0f7c8df
commit
6ffa63b923
236 files changed
+14194
-7278
No files matched your search
@@ -20,9 +20,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Vocaluxe.Base;
|
||||
using Vocaluxe.Reporting;
|
||||
using VocaluxeLib;
|
||||
using VocaluxeLib.Log;
|
||||
using VocaluxeLib.PartyModes.Challenge;
|
||||
|
||||
namespace Tests.PartyModeChallenge
|
||||
@@ -31,20 +29,19 @@ namespace Tests.PartyModeChallenge
|
||||
public class CPartyModeChallangeTest
|
||||
{
|
||||
#region Tests
|
||||
|
||||
[Test]
|
||||
public void TestRoundBoundaries()
|
||||
{
|
||||
// ReSharper disable ObjectCreationAsStatement
|
||||
new CChallengeRounds(1, 1, 1); // Should succeed
|
||||
// ReSharper restore ObjectCreationAsStatement
|
||||
for (int inv = -1; inv <= 0; inv++)
|
||||
for (var inv = -1; inv <= 0; inv++)
|
||||
{
|
||||
int invL = inv;
|
||||
var invL = inv;
|
||||
// ReSharper disable ObjectCreationAsStatement
|
||||
Assert.Throws<ArgumentException>(() => new CChallengeRounds(invL, 1, 1));
|
||||
Assert.Throws<ArgumentException>(() => new CChallengeRounds(1, invL, 1));
|
||||
Assert.Throws<ArgumentException>(() => new CChallengeRounds(1, 1, invL));
|
||||
Assert.Throws<ArgumentException>(new Action(() => new CChallengeRounds(invL, 1, 1)));
|
||||
Assert.Throws<ArgumentException>(new Action(() => new CChallengeRounds(1, invL, 1)));
|
||||
Assert.Throws<ArgumentException>(new Action(() => new CChallengeRounds(1, 1, invL)));
|
||||
// ReSharper restore ObjectCreationAsStatement
|
||||
}
|
||||
}
|
||||
@@ -54,36 +51,38 @@ namespace Tests.PartyModeChallenge
|
||||
{
|
||||
CBase.Game = new CBGame();
|
||||
|
||||
int roundFactor = (numPlayer % numMic == 0) ? numPlayer / numMic : numPlayer;
|
||||
for (int numRounds = roundFactor; numRounds <= 100 && numRounds <= roundFactor * 5; numRounds += roundFactor)
|
||||
var roundFactor = numPlayer % numMic == 0 ? numPlayer / numMic : numPlayer;
|
||||
for (var numRounds = roundFactor; numRounds <= 100 && numRounds <= roundFactor * 5; numRounds += roundFactor)
|
||||
{
|
||||
CChallengeRounds rounds = new CChallengeRounds(numRounds, numPlayer, numMic);
|
||||
Assert.IsTrue(rounds.Count >= numRounds);
|
||||
var rounds = new CChallengeRounds(numRounds, numPlayer, numMic);
|
||||
Assert.That(rounds.Count >= numRounds, Is.True);
|
||||
_CheckRounds(rounds, numPlayer);
|
||||
Assert.That(rounds.Count, Is.GreaterThanOrEqualTo(numRounds),
|
||||
Assert.That(rounds.Count, Is.GreaterThanOrEqualTo(numRounds),
|
||||
$"Number of rounds should be >= {numRounds}, is {rounds.Count} for {numPlayer}/{numMic}");
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper methods
|
||||
private static void _CheckRounds(CChallengeRounds rounds, int numPlayer)
|
||||
{
|
||||
List<int> numSongs = new List<int>(numPlayer);
|
||||
for (int i = 0; i < numPlayer; i++)
|
||||
numSongs.Add(0);
|
||||
for (int i = 0; i < rounds.Count; i++)
|
||||
var numSongs = new List<int>(numPlayer);
|
||||
for (var i = 0; i < numPlayer; i++)
|
||||
{
|
||||
foreach (int player in rounds[i].Players)
|
||||
numSongs.Add(0);
|
||||
}
|
||||
|
||||
for (var i = 0; i < rounds.Count; i++)
|
||||
{
|
||||
foreach (var player in rounds[i].Players)
|
||||
{
|
||||
Assert.IsTrue(player >= 0 && player < numPlayer);
|
||||
Assert.That(player >= 0 && player < numPlayer, Is.True);
|
||||
numSongs[player]++;
|
||||
}
|
||||
}
|
||||
Assert.IsTrue(numSongs.Min() == numSongs.Max(), "Some players have more songs than others");
|
||||
}
|
||||
|
||||
Assert.That(numSongs.Min() == numSongs.Max(), Is.True, "Some players have more songs than others");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
@@ -30,4 +29,4 @@ using System.Runtime.InteropServices;
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
+7
-3
@@ -28,6 +28,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@@ -37,6 +38,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
@@ -47,6 +49,7 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
@@ -56,14 +59,15 @@
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<LangVersion>9.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NUnit" Version="3.10.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.10.0" PrivateAssets="all" />
|
||||
<PackageReference Include="NUnit" Version="4.6.1" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="6.2.0" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="PartyModeChallenge\CPartyModeChallangeTest.cs" />
|
||||
@@ -117,4 +121,4 @@
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
</Project>
|
||||
@@ -15,7 +15,6 @@
|
||||
// along with Vocaluxe. If not, see <http://www.gnu.org/licenses/>.
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using NUnit.Framework;
|
||||
using VocaluxeLib.Log;
|
||||
@@ -28,7 +27,6 @@ namespace Tests.VocaluxeLib.Log
|
||||
private string _TestFolder;
|
||||
|
||||
#region Setup methods
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
@@ -40,18 +38,16 @@ namespace Tests.VocaluxeLib.Log
|
||||
{
|
||||
Directory.Delete(_TestFolder, true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Tests
|
||||
|
||||
[Test]
|
||||
public void BenchmarkTimeTest()
|
||||
{
|
||||
const string testMessage = "BenchmarkTime Test";
|
||||
string testFileName = Path.GetRandomFileName();
|
||||
string testFileSongName = Path.GetRandomFileName();
|
||||
string testFileMarkerName = Path.GetRandomFileName();
|
||||
var testFileName = Path.GetRandomFileName();
|
||||
var testFileSongName = Path.GetRandomFileName();
|
||||
var testFileMarkerName = Path.GetRandomFileName();
|
||||
const string versionTag = "Test Version (1.2.4)";
|
||||
|
||||
// Init Log
|
||||
@@ -68,59 +64,59 @@ namespace Tests.VocaluxeLib.Log
|
||||
CLog.Close();
|
||||
|
||||
// Check log
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileName)), "Mainlog file is missing.");
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileSongName)), "Songlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileName)), Is.True, "Mainlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileSongName)), Is.True, "Songlog file is missing.");
|
||||
|
||||
string mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
string songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
var mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
var songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
|
||||
StringAssert.Contains($"[Information] Started \"{ testMessage }\"", mainLogContent, "Start entry wrong");
|
||||
StringAssert.Contains($"[Information] Finished \"{ testMessage }\" successfully in ", mainLogContent, "Finish entry wrong");
|
||||
StringAssert.AreEqualIgnoringCase("", songLogContent, "Benchmark should not create song log entries");
|
||||
Assert.That(mainLogContent, Contains.Substring($"[Information] Started \"{testMessage}\""), "Start entry wrong");
|
||||
Assert.That(mainLogContent, Contains.Substring($"[Information] Finished \"{testMessage}\" successfully in "), "Finish entry wrong");
|
||||
Assert.That(songLogContent, Is.Empty, "Benchmark should not create song log entries");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BenchmarkBeginTest()
|
||||
{
|
||||
const string testMessage = "BenchmarkBegin Test";
|
||||
string testFileName = Path.GetRandomFileName();
|
||||
string testFileSongName = Path.GetRandomFileName();
|
||||
string testFileMarkerName = Path.GetRandomFileName();
|
||||
var testFileName = Path.GetRandomFileName();
|
||||
var testFileSongName = Path.GetRandomFileName();
|
||||
var testFileMarkerName = Path.GetRandomFileName();
|
||||
const string versionTag = "Test Version (1.2.4)";
|
||||
|
||||
// Init Log
|
||||
CLog.Init(_TestFolder, testFileName, testFileSongName, testFileMarkerName, versionTag,
|
||||
(crash, cont, tag, log, error) => { Assert.Fail("Benchmarks should not show the reporter."); },
|
||||
ELogLevel.Verbose);
|
||||
|
||||
|
||||
using (var token = CBenchmark.Begin(testMessage))
|
||||
{
|
||||
System.Threading.Thread.Sleep(1);
|
||||
token.End();
|
||||
}
|
||||
|
||||
|
||||
// Close logfile
|
||||
CLog.Close();
|
||||
|
||||
// Check log
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileName)), "Mainlog file is missing.");
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileSongName)), "Songlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileName)), Is.True, "Mainlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileSongName)), Is.True, "Songlog file is missing.");
|
||||
|
||||
string mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
string songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
var mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
var songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
|
||||
StringAssert.Contains($"[Information] Started \"{ testMessage }\"", mainLogContent, "Start entry wrong");
|
||||
StringAssert.Contains($"[Information] Finished \"{ testMessage }\" successfully in ", mainLogContent, "Finish entry wrong");
|
||||
StringAssert.AreEqualIgnoringCase("", songLogContent, "Benchmark should not create song log entries");
|
||||
Assert.That(mainLogContent, Contains.Substring($"[Information] Started \"{testMessage}\""), "Start entry wrong");
|
||||
Assert.That(mainLogContent, Contains.Substring($"[Information] Finished \"{testMessage}\" successfully in "), "Finish entry wrong");
|
||||
Assert.That(songLogContent, Is.Empty, "Benchmark should not create song log entries");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BenchmarkBeginWithoutEndTest()
|
||||
{
|
||||
const string testMessage = "BenchmarkBegin Test";
|
||||
string testFileName = Path.GetRandomFileName();
|
||||
string testFileSongName = Path.GetRandomFileName();
|
||||
string testFileMarkerName = Path.GetRandomFileName();
|
||||
var testFileName = Path.GetRandomFileName();
|
||||
var testFileSongName = Path.GetRandomFileName();
|
||||
var testFileMarkerName = Path.GetRandomFileName();
|
||||
const string versionTag = "Test Version (1.2.4)";
|
||||
|
||||
// Init Log
|
||||
@@ -137,28 +133,25 @@ namespace Tests.VocaluxeLib.Log
|
||||
CLog.Close();
|
||||
|
||||
// Check log
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileName)), "Mainlog file is missing.");
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileSongName)), "Songlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileName)), Is.True, "Mainlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileSongName)), Is.True, "Songlog file is missing.");
|
||||
|
||||
string mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
string songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
var mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
var songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
|
||||
StringAssert.Contains($"[Information] Started \"{ testMessage }\"", mainLogContent, "Start entry wrong");
|
||||
StringAssert.Contains($"[Information] Failed \"{ testMessage }\" in ", mainLogContent, "Finish entry wrong");
|
||||
StringAssert.AreEqualIgnoringCase("", songLogContent, "Benchmark should not create song log entries");
|
||||
Assert.That(mainLogContent, Contains.Substring($"[Information] Started \"{testMessage}\""), "Start entry wrong");
|
||||
Assert.That(mainLogContent, Contains.Substring($"[Information] Failed \"{testMessage}\" in "), "Finish entry wrong");
|
||||
Assert.That(songLogContent, Is.Empty, "Benchmark should not create song log entries");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper methods
|
||||
|
||||
private string _GetTemporaryDirectory()
|
||||
{
|
||||
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
||||
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
||||
Directory.CreateDirectory(tempDirectory);
|
||||
return tempDirectory;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
+566
-579
File diff suppressed because it is too large.
Load diff
@@ -92,16 +92,16 @@ namespace Tests.VocaluxeLib.Log
|
||||
(crash, cont, tag, log, error) =>
|
||||
{
|
||||
messageShown = true;
|
||||
Assert.IsTrue(show);
|
||||
Assert.IsFalse(crash);
|
||||
Assert.IsTrue(cont);
|
||||
Assert.AreEqual(versionTag, tag);
|
||||
Assert.That(show, Is.True);
|
||||
Assert.That(crash, Is.False);
|
||||
Assert.That(cont, Is.True);
|
||||
Assert.That(tag, Is.EqualTo(versionTag));
|
||||
|
||||
StringAssert.Contains("[Information] Starting to log", log, "Main log start entry wrong");
|
||||
StringAssert.Contains("Version = " + versionTag, log, "Main log version tag entry wrong");
|
||||
StringAssert.Contains("[<#= LogLevel #>] " + <#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>, log, "Main log entry wrong");
|
||||
Assert.That(log, Contains.Substring("[Information] Starting to log"), "Main log start entry wrong");
|
||||
Assert.That(log, Contains.Substring("Version = " + versionTag), "Main log version tag entry wrong");
|
||||
Assert.That(log, Contains.Substring("[<#= LogLevel #>] " + <#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>), "Main log entry wrong");
|
||||
|
||||
StringAssert.Contains(<#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>, error, "Error message wrong");
|
||||
Assert.That(error, Contains.Substring(<#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>), "Error message wrong");
|
||||
},
|
||||
ELogLevel.Verbose);
|
||||
|
||||
@@ -115,27 +115,27 @@ namespace Tests.VocaluxeLib.Log
|
||||
CLog.Close();
|
||||
|
||||
// Check log
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileName)), "Mainlog file is missing.");
|
||||
Assert.IsTrue(File.Exists(Path.Combine(_TestFolder, testFileSongName)), "Songlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileName)), Is.True, "Mainlog file is missing.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, testFileSongName)), Is.True, "Songlog file is missing.");
|
||||
|
||||
string mainLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileName));
|
||||
string songLogContent = File.ReadAllText(Path.Combine(_TestFolder, testFileSongName));
|
||||
|
||||
StringAssert.Contains("[Information] Starting to log", mainLogContent, "Main log start entry wrong");
|
||||
StringAssert.Contains("Version = " + versionTag, mainLogContent, "Main log version tag entry wrong");
|
||||
StringAssert.Contains("[<#= LogLevel #>] " +<#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>, mainLogContent, "Main log entry wrong");
|
||||
Assert.That(mainLogContent, Contains.Substring("[Information] Starting to log"), "Main log start entry wrong");
|
||||
Assert.That(mainLogContent, Contains.Substring("Version = " + versionTag), "Main log version tag entry wrong");
|
||||
Assert.That(mainLogContent, Contains.Substring("[<#= LogLevel #>] " +<#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>), "Main log entry wrong");
|
||||
<# if(WithException) { #>
|
||||
StringAssert.Contains(_TestExceptionMessage, mainLogContent, "Exception is missing");
|
||||
Assert.That(mainLogContent, Contains.Substring(_TestExceptionMessage), "Exception is missing");
|
||||
<# } #>
|
||||
<# if(WithData) { #>
|
||||
StringAssert.Contains(_SecondParam, mainLogContent, "Second data field is missing");
|
||||
Assert.That(mainLogContent, Contains.Substring(_SecondParam), "Second data field is missing");
|
||||
<# } #>
|
||||
<# if(LogLevel!="Fatal") { #>
|
||||
StringAssert.Contains(<#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>, songLogContent, "Song log entry wrong");
|
||||
Assert.That(songLogContent, Contains.Substring(<#= WithData?"_TestMessageWithResolvedData":"_TestMessage" #>), "Song log entry wrong");
|
||||
<# } #>
|
||||
if(!show)
|
||||
{
|
||||
Assert.IsFalse(messageShown);
|
||||
Assert.That(messageShown, Is.False);
|
||||
}
|
||||
}
|
||||
<# } #>
|
||||
|
||||
@@ -29,7 +29,6 @@ namespace Tests.VocaluxeLib.Log.Rolling
|
||||
private string _TestFolder;
|
||||
|
||||
#region Setup methods
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
@@ -41,20 +40,18 @@ namespace Tests.VocaluxeLib.Log.Rolling
|
||||
{
|
||||
Directory.Delete(_TestFolder, true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Tests
|
||||
|
||||
[Test]
|
||||
public void RollFilesTest([Range(0,4)] int numFilesExisting, [Range(0, 3)] int numOldFilesToKeep)
|
||||
public void RollFilesTest([Range(0, 4)] int numFilesExisting, [Range(0, 3)] int numOldFilesToKeep)
|
||||
{
|
||||
// Create existing files
|
||||
if (numFilesExisting > 0)
|
||||
{
|
||||
CreateFile(_TestFolder, _TestFileName, 0);
|
||||
}
|
||||
|
||||
|
||||
for (var i = 1; i < numFilesExisting; i++)
|
||||
{
|
||||
CreateFile(_TestFolder, GetFileName(i), i);
|
||||
@@ -63,33 +60,31 @@ namespace Tests.VocaluxeLib.Log.Rolling
|
||||
|
||||
// Roll the files
|
||||
CLogFileRoller.RollLogs(Path.Combine(_TestFolder, _TestFileName), numOldFilesToKeep);
|
||||
|
||||
|
||||
|
||||
// Check main file
|
||||
Assert.IsFalse(File.Exists(Path.Combine(_TestFolder, _TestFileName)), "Main file was not deleted.");
|
||||
Assert.That(File.Exists(Path.Combine(_TestFolder, _TestFileName)), Is.False, "Main file was not deleted.");
|
||||
//Check other files
|
||||
for (int i = 1; i <= Math.Max(numOldFilesToKeep, numFilesExisting); i++)
|
||||
for (var i = 1; i <= Math.Max(numOldFilesToKeep, numFilesExisting); i++)
|
||||
{
|
||||
var fileToCheck = Path.Combine(_TestFolder, GetFileName(i));
|
||||
if (i <= Math.Min(numFilesExisting,numOldFilesToKeep))
|
||||
if (i <= Math.Min(numFilesExisting, numOldFilesToKeep))
|
||||
{
|
||||
Assert.IsTrue(File.Exists(fileToCheck), $"File {GetFileName(i)} is missing.");
|
||||
Assert.AreEqual((i - 1).ToString(), File.ReadAllText(fileToCheck), "Rotation is wrong");
|
||||
Assert.That(File.Exists(fileToCheck), Is.True, $"File {GetFileName(i)} is missing.");
|
||||
Assert.That(File.ReadAllText(fileToCheck), Is.EqualTo((i - 1).ToString()), "Rotation is wrong");
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.IsFalse(File.Exists(fileToCheck));
|
||||
Assert.That(File.Exists(fileToCheck), Is.False);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper methods
|
||||
|
||||
private string _GetTemporaryDirectory()
|
||||
{
|
||||
string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
||||
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
||||
Directory.CreateDirectory(tempDirectory);
|
||||
return tempDirectory;
|
||||
}
|
||||
@@ -103,8 +98,6 @@ namespace Tests.VocaluxeLib.Log.Rolling
|
||||
{
|
||||
return _TestFileName.Replace(".", $"_{i}.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,72 +27,82 @@ namespace Tests.VocaluxeLib.Utils.Combinatorics
|
||||
public class CCombinationsTest
|
||||
{
|
||||
#region Tests
|
||||
|
||||
[Test]
|
||||
public void TestCount()
|
||||
{
|
||||
Assert.AreEqual(4, CCombinations<int>.Count(4, 3));
|
||||
Assert.AreEqual(6, CCombinations<int>.Count(4, 2));
|
||||
Assert.AreEqual(66, CCombinations<int>.Count(12, 2));
|
||||
Assert.That(CCombinations<int>.Count(4, 3), Is.EqualTo(4));
|
||||
Assert.That(CCombinations<int>.Count(4, 2), Is.EqualTo(6));
|
||||
Assert.That(CCombinations<int>.Count(12, 2), Is.EqualTo(66));
|
||||
// Corner cases
|
||||
for (int i = 1; i < 12; i++)
|
||||
for (var i = 1; i < 12; i++)
|
||||
{
|
||||
Assert.AreEqual(1, CCombinations<int>.Count(i, 0));
|
||||
Assert.AreEqual(1, CCombinations<int>.Count(i, i));
|
||||
Assert.AreEqual(0, CCombinations<int>.Count(i, i + 1));
|
||||
Assert.That(CCombinations<int>.Count(i, 0), Is.EqualTo(1));
|
||||
Assert.That(CCombinations<int>.Count(i, i), Is.EqualTo(1));
|
||||
Assert.That(CCombinations<int>.Count(i, i + 1), Is.EqualTo(0));
|
||||
}
|
||||
|
||||
// Check identity (n k) = (n n-k)
|
||||
for (int i = 0; i <= 12; i++)
|
||||
Assert.AreEqual(CCombinations<int>.Count(12, i), CCombinations<int>.Count(12, 12 - i));
|
||||
Assert.That(() => CCombinations<int>.Count(-1, -1), Throws.TypeOf<ArgumentException>());
|
||||
Assert.That(() => CCombinations<int>.Count(-1, 1), Throws.TypeOf<ArgumentException>());
|
||||
Assert.That(() => CCombinations<int>.Count(1, -1), Throws.TypeOf<ArgumentException>());
|
||||
for (var i = 0; i <= 12; i++)
|
||||
{
|
||||
Assert.That(CCombinations<int>.Count(12, 12 - i), Is.EqualTo(CCombinations<int>.Count(12, i)));
|
||||
}
|
||||
|
||||
Assert.Throws<ArgumentException>(new Action(() => CCombinations<int>.Count(-1, -1)));
|
||||
Assert.Throws<ArgumentException>(new Action(() => CCombinations<int>.Count(-1, 1)));
|
||||
Assert.Throws<ArgumentException>(new Action(() => CCombinations<int>.Count(1, -1)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestResults([Range(0, 8, 1)] int n)
|
||||
{
|
||||
Random rand = new Random();
|
||||
|
||||
for (int k = 0; k <= n; k++)
|
||||
var rand = new Random();
|
||||
|
||||
for (var k = 0; k <= n; k++)
|
||||
{
|
||||
List<int> collection = new List<int>(n);
|
||||
int r = rand.Next(1000);
|
||||
for (int i = 0; i < n; i++)
|
||||
var collection = new List<int>(n);
|
||||
var r = rand.Next(1000);
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
collection.Add(i + r);
|
||||
CCombinations<int> combs = new CCombinations<int>(collection, k);
|
||||
Assert.AreEqual(CCombinations<int>.Count(n, k), combs.Count());
|
||||
}
|
||||
|
||||
var combs = new CCombinations<int>(collection, k);
|
||||
Assert.That(combs.Count(), Is.EqualTo(CCombinations<int>.Count(n, k)));
|
||||
_CheckResults(combs, collection);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper methods
|
||||
|
||||
// ReSharper disable UnusedParameter.Local
|
||||
private static void _CheckResults<T>(CCombinations<T> combs, List<T> input)
|
||||
// ReSharper restore UnusedParameter.Local
|
||||
// ReSharper restore UnusedParameter.Local
|
||||
{
|
||||
List<List<T>> results = new List<List<T>>();
|
||||
foreach (List<T> comb in combs)
|
||||
var results = new List<List<T>>();
|
||||
foreach (var comb in combs)
|
||||
{
|
||||
foreach (List<T> other in results)
|
||||
Assert.IsFalse(other.SequenceEqual(comb), "Result must be unique");
|
||||
for (int i = 0; i < comb.Count - 1; i++)
|
||||
foreach (var other in results)
|
||||
{
|
||||
Assert.IsTrue(comb.IndexOf(comb[i], i + 1) < 0, "Repetition not allowed");
|
||||
Assert.IsTrue(input.Contains(comb[i]), "Output must be in input sequence");
|
||||
Assert.That(other.SequenceEqual(comb), Is.False, "Result must be unique");
|
||||
}
|
||||
|
||||
for (var i = 0; i < comb.Count - 1; i++)
|
||||
{
|
||||
Assert.That(comb.IndexOf(comb[i], i + 1), Is.LessThan(0),"Repetition not allowed");
|
||||
Assert.That(input.Contains(comb[i]), Is.True, "Output must be in input sequence");
|
||||
}
|
||||
|
||||
results.Add(comb);
|
||||
}
|
||||
Assert.AreEqual(combs.Count(), results.Count);
|
||||
List<List<T>> results2 = combs.GetAll();
|
||||
Assert.AreEqual(results.Count, results2.Count);
|
||||
for (int i = 0; i < results2.Count; i++)
|
||||
Assert.IsTrue(results[i].SequenceEqual(results2[i]));
|
||||
}
|
||||
|
||||
Assert.That(results, Has.Count.EqualTo(combs.Count()));
|
||||
var results2 = combs.GetAll();
|
||||
Assert.That(results, Has.Count.EqualTo(results2.Count));
|
||||
for (var i = 0; i < results2.Count; i++)
|
||||
{
|
||||
Assert.That(results[i], Is.EquivalentTo(results2[i]));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,9 @@ using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Xml.Serialization;
|
||||
using NUnit.Framework;
|
||||
using Vocaluxe.Base;
|
||||
using VocaluxeLib;
|
||||
using VocaluxeLib.Xml;
|
||||
using Vocaluxe.Base;
|
||||
|
||||
namespace Tests.VocaluxeLib.XML
|
||||
{
|
||||
@@ -34,7 +34,6 @@ namespace Tests.VocaluxeLib.XML
|
||||
private const string _Empty = _Head + @"<root />";
|
||||
|
||||
#region Tests
|
||||
|
||||
[Test]
|
||||
public void TestBasic()
|
||||
{
|
||||
@@ -51,67 +50,76 @@ namespace Tests.VocaluxeLib.XML
|
||||
<S>2</S>
|
||||
</root>";
|
||||
var xml = new CXmlDeserializer();
|
||||
SBasic foo = xml.DeserializeString<SBasic>(s);
|
||||
Assert.AreEqual(1, foo.I);
|
||||
Assert.AreEqual("2", foo.S);
|
||||
Assert.AreEqual(3, foo.F, 0.0001);
|
||||
Assert.AreEqual(4, foo.D, 0.0001);
|
||||
var foo = xml.DeserializeString<SBasic>(s);
|
||||
Assert.That(foo.I, Is.EqualTo(1));
|
||||
Assert.That(foo.S, Is.EqualTo("2"));
|
||||
Assert.That(foo.F, Is.EqualTo(3).Within(0.0001));
|
||||
Assert.That(foo.D, Is.EqualTo(4).Within(0.0001));
|
||||
foo = xml.DeserializeString<SBasic>(sUnordered);
|
||||
Assert.AreEqual(1, foo.I);
|
||||
Assert.AreEqual("2", foo.S);
|
||||
Assert.AreEqual(3, foo.F, 0.0001);
|
||||
Assert.AreEqual(4, foo.D, 0.0001);
|
||||
Assert.That(foo.I, Is.EqualTo(1));
|
||||
Assert.That(foo.S, Is.EqualTo("2"));
|
||||
Assert.That(foo.F, Is.EqualTo(3).Within(0.0001));
|
||||
Assert.That(foo.D, Is.EqualTo(4).Within(0.0001));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMissingXmlElement()
|
||||
{
|
||||
string[] s = new string[] { @"<root>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>", @"<root>
|
||||
<I>1</I>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>", @"<root>
|
||||
<I>1</I>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
</root>" };
|
||||
foreach (string s1 in s)
|
||||
var s = new string[]
|
||||
{
|
||||
string sTmp = s1;
|
||||
_AssertFail<SBasic, CXmlException>(sTmp);
|
||||
@"<root>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>",
|
||||
@"<root>
|
||||
<I>1</I>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>",
|
||||
@"<root>
|
||||
<I>1</I>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
</root>"
|
||||
};
|
||||
foreach (var s1 in s)
|
||||
{
|
||||
_AssertXmlSerializeFailWithMissingElement<SBasic, CXmlMissingElementException>(s1);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMissingStructElement()
|
||||
{
|
||||
string[] s = new string[] { @"<root>
|
||||
<Foo>1</Foo>
|
||||
<I>1</I>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>", @"<root>
|
||||
<I>1</I>
|
||||
<Foo>1</Foo>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>", @"<root>
|
||||
<I>1</I>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
<Foo>1</Foo>
|
||||
</root>" };
|
||||
foreach (string s1 in s)
|
||||
var s = new string[]
|
||||
{
|
||||
string sTmp = s1;
|
||||
_AssertFail<SBasic, CXmlException>(sTmp);
|
||||
@"<root>
|
||||
<Foo>1</Foo>
|
||||
<I>1</I>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>",
|
||||
@"<root>
|
||||
<I>1</I>
|
||||
<Foo>1</Foo>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
</root>",
|
||||
@"<root>
|
||||
<I>1</I>
|
||||
<S>2</S>
|
||||
<F>3</F>
|
||||
<D>4</D>
|
||||
<Foo>1</Foo>
|
||||
</root>"
|
||||
};
|
||||
foreach (var s1 in s)
|
||||
{
|
||||
var sTmp = s1;
|
||||
_AssertXmlSerializeFailWithMissingElement<SBasic, CXmlException>(sTmp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +135,7 @@ namespace Tests.VocaluxeLib.XML
|
||||
_AssertSerDeserMatch<SBasic>(s);
|
||||
}
|
||||
|
||||
private readonly string[] _XmlList = new string[]
|
||||
private readonly string[] _XmlList =
|
||||
{
|
||||
_Head + @"<root>
|
||||
<Ints>
|
||||
@@ -153,22 +161,22 @@ namespace Tests.VocaluxeLib.XML
|
||||
{
|
||||
var xml = new CXmlDeserializer();
|
||||
var ser = new CXmlSerializer();
|
||||
SList foo = xml.DeserializeString<SList>(_XmlList[0]);
|
||||
Assert.AreEqual(foo.Ints.Count, 2, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[1].I, 1, "Deserialization failed");
|
||||
string res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlList[0], res, "Serialization failed");
|
||||
var foo = xml.DeserializeString<SList>(_XmlList[0]);
|
||||
Assert.That(foo.Ints, Has.Count.EqualTo(2), "Deserialization failed");
|
||||
Assert.That(foo.Ints[0].I, Is.EqualTo(1), "Deserialization failed");
|
||||
Assert.That(foo.Ints[1].I, Is.EqualTo(1), "Deserialization failed");
|
||||
var res = ser.Serialize(foo);
|
||||
Assert.That(res, Is.EqualTo(_XmlList[0]), "Serialization failed");
|
||||
foo = xml.DeserializeString<SList>(_XmlList[1]);
|
||||
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed");
|
||||
Assert.That(foo.Ints, Is.Empty, "Deserialization2 failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlList[2], res, "Serialization2 failed");
|
||||
Assert.That(_XmlList[2], Is.EqualTo(res), "Serialization2 failed");
|
||||
foo = xml.DeserializeString<SList>(_XmlList[2]);
|
||||
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization2 failed");
|
||||
Assert.That(foo.Ints, Is.Empty, "Deserialization2 failed");
|
||||
_AssertSerDeserMatch<SList>(_XmlList[2]);
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlList[2], res, "Serialization2 failed");
|
||||
_AssertFail<SList, CXmlException>(_Empty);
|
||||
Assert.That(_XmlList[2], Is.EqualTo(res), "Serialization2 failed");
|
||||
_AssertXmlSerializeFailWithMissingElement<SList, CXmlMissingElementException>(_Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -176,21 +184,21 @@ namespace Tests.VocaluxeLib.XML
|
||||
{
|
||||
var xml = new CXmlDeserializer();
|
||||
var ser = new CXmlSerializer();
|
||||
SArray foo = xml.DeserializeString<SArray>(_XmlList[0]);
|
||||
Assert.AreEqual(foo.Ints.Length, 2, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[1].I, 1, "Deserialization failed");
|
||||
string res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlList[0], res, "Serialization failed");
|
||||
var foo = xml.DeserializeString<SArray>(_XmlList[0]);
|
||||
Assert.That(foo.Ints, Has.Length.EqualTo(2), "Deserialization failed");
|
||||
Assert.That(foo.Ints[0].I, Is.EqualTo(1), "Deserialization failed");
|
||||
Assert.That(foo.Ints[1].I, Is.EqualTo(1), "Deserialization failed");
|
||||
var res = ser.Serialize(foo);
|
||||
Assert.That(res, Is.EqualTo(_XmlList[0]), "Serialization failed");
|
||||
foo = xml.DeserializeString<SArray>(_XmlList[1]);
|
||||
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed");
|
||||
Assert.That(foo.Ints, Is.Empty, "Deserialization2 failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlList[2], res, "Serialization2 failed");
|
||||
Assert.That(_XmlList[2], Is.EqualTo(res), "Serialization2 failed");
|
||||
foo = xml.DeserializeString<SArray>(_XmlList[2]);
|
||||
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization2 failed");
|
||||
Assert.That(foo.Ints, Is.Empty, "Deserialization2 failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlList[2], res, "Serialization2 failed");
|
||||
_AssertFail<SArray, CXmlException>(_Empty);
|
||||
Assert.That(_XmlList[2], Is.EqualTo(res), "Serialization2 failed");
|
||||
_AssertXmlSerializeFailWithMissingElement<SArray, CXmlMissingElementException>(_Empty);
|
||||
}
|
||||
|
||||
private const string _XmlListEmb = _Head + @"<root>
|
||||
@@ -225,25 +233,25 @@ namespace Tests.VocaluxeLib.XML
|
||||
{
|
||||
var xml = new CXmlDeserializer();
|
||||
var ser = new CXmlSerializer();
|
||||
SListEmb foo = xml.DeserializeString<SListEmb>(_XmlListEmb);
|
||||
Assert.AreEqual(foo.Ints.Count, 1, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed");
|
||||
string res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb, res, "Serialization failed");
|
||||
var foo = xml.DeserializeString<SListEmb>(_XmlListEmb);
|
||||
Assert.That(foo.Ints, Has.Count.EqualTo(1), "Deserialization failed");
|
||||
Assert.That(foo.Ints[0].I, Is.EqualTo(1), "Deserialization failed");
|
||||
var res = ser.Serialize(foo);
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb), "Serialization failed");
|
||||
foo = xml.DeserializeString<SListEmb>(_XmlListEmb2);
|
||||
Assert.AreEqual(foo.Ints.Count, 2, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[1].I, 2, "Deserialization failed");
|
||||
Assert.That(foo.Ints, Has.Count.EqualTo(2), "Deserialization failed");
|
||||
Assert.That(foo.Ints[1].I, Is.EqualTo(2) , "Deserialization failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb2, res, "Serialization failed");
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb2), "Serialization failed");
|
||||
foo = xml.DeserializeString<SListEmb>(_XmlListEmb3);
|
||||
Assert.AreEqual(foo.Ints.Count, 0, "Deserialization failed");
|
||||
Assert.That(foo.Ints, Is.Empty, "Deserialization failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb3, res, "Serialization failed");
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb3), "Serialization failed");
|
||||
ser = new CXmlSerializer(true);
|
||||
foo.Ints.Clear();
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb4, res, "Serialization failed");
|
||||
_AssertFail<SListEmb, CXmlException>(_Empty);
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb4), "Serialization failed");
|
||||
_AssertXmlSerializeFailWithMissingElement<SListEmb, CXmlMissingElementException>(_Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -251,25 +259,25 @@ namespace Tests.VocaluxeLib.XML
|
||||
{
|
||||
var xml = new CXmlDeserializer();
|
||||
var ser = new CXmlSerializer();
|
||||
SArrayEmb foo = xml.DeserializeString<SArrayEmb>(_XmlListEmb);
|
||||
Assert.AreEqual(foo.Ints.Length, 1, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[0].I, 1, "Deserialization failed");
|
||||
string res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb, res, "Serialization failed");
|
||||
var foo = xml.DeserializeString<SArrayEmb>(_XmlListEmb);
|
||||
Assert.That(foo.Ints, Has.Length.EqualTo(1), "Deserialization failed");
|
||||
Assert.That(foo.Ints[0].I, Is.EqualTo(1), "Deserialization failed");
|
||||
var res = ser.Serialize(foo);
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb), "Serialization failed");
|
||||
foo = xml.DeserializeString<SArrayEmb>(_XmlListEmb2);
|
||||
Assert.AreEqual(foo.Ints.Length, 2, "Deserialization failed");
|
||||
Assert.AreEqual(foo.Ints[1].I, 2, "Deserialization failed");
|
||||
Assert.That(foo.Ints, Has.Length.EqualTo(2), "Deserialization failed");
|
||||
Assert.That(foo.Ints[1].I, Is.EqualTo(2), "Deserialization failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb2, res, "Serialization failed");
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb2), "Serialization failed");
|
||||
foo = xml.DeserializeString<SArrayEmb>(_XmlListEmb3);
|
||||
Assert.AreEqual(foo.Ints.Length, 0, "Deserialization failed");
|
||||
Assert.That(foo.Ints, Is.Empty, "Deserialization failed");
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb3, res, "Serialization failed");
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb3), "Serialization failed");
|
||||
ser = new CXmlSerializer(true);
|
||||
foo.Ints = new SEntry[0];
|
||||
foo.Ints = Array.Empty<SEntry>();
|
||||
res = ser.Serialize(foo);
|
||||
Assert.AreEqual(_XmlListEmb4, res, "Serialization failed");
|
||||
_AssertFail<SArrayEmb, CXmlException>(_Empty);
|
||||
Assert.That(res, Is.EqualTo(_XmlListEmb4), "Serialization failed");
|
||||
_AssertXmlSerializeFailWithMissingElement<SArrayEmb, CXmlMissingElementException>(_Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -279,9 +287,9 @@ namespace Tests.VocaluxeLib.XML
|
||||
<Public>2</Public>
|
||||
<Auto>3</Auto>
|
||||
</root>";
|
||||
SProperty foo = _AssertSerDeserMatch<SProperty>(xmlString);
|
||||
Assert.AreEqual(3, foo.Private);
|
||||
Assert.AreEqual(3, foo.Auto);
|
||||
var foo = _AssertSerDeserMatch<SProperty>(xmlString);
|
||||
Assert.That(foo.Private, Is.EqualTo(3));
|
||||
Assert.That(foo.Auto, Is.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -305,7 +313,7 @@ namespace Tests.VocaluxeLib.XML
|
||||
public void TestIgnore()
|
||||
{
|
||||
_AssertSerDeserMatch<SIgnore>(_XmlIgnore);
|
||||
_AssertFail<SIgnore, CXmlException>(_XmlIgnore2);
|
||||
_AssertXmlSerializeFailWithMissingElement<SIgnore, CXmlException>(_XmlIgnore2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -314,15 +322,15 @@ namespace Tests.VocaluxeLib.XML
|
||||
var foo = new SIgnore { I = 1 };
|
||||
var xml = new CXmlDeserializer();
|
||||
var ser = new CXmlSerializer();
|
||||
SIgnore bar = xml.DeserializeString(_XmlIgnore, foo);
|
||||
Assert.AreEqual(1, bar.I);
|
||||
Assert.AreEqual(_XmlIgnore, ser.Serialize(bar));
|
||||
var bar = xml.DeserializeString(_XmlIgnore, foo);
|
||||
Assert.That(bar.I, Is.EqualTo(1));
|
||||
Assert.That(ser.Serialize(bar), Is.EqualTo(_XmlIgnore));
|
||||
|
||||
var foo2 = new CIgnore { I = 1 };
|
||||
CIgnore bar2 = xml.DeserializeString(_XmlIgnore, foo2);
|
||||
Assert.AreEqual(1, bar2.I);
|
||||
Assert.AreEqual(foo2.J, bar2.J, "Original classes should be modified by the deserialization");
|
||||
Assert.AreEqual(_XmlIgnore, ser.Serialize(bar2));
|
||||
var bar2 = xml.DeserializeString(_XmlIgnore, foo2);
|
||||
Assert.That(bar2.I, Is.EqualTo(1));
|
||||
Assert.That(bar2.J, Is.EqualTo(foo2.J), "Original classes should be modified by the deserialization");
|
||||
Assert.That(ser.Serialize(bar2), Is.EqualTo(_XmlIgnore));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -359,16 +367,16 @@ namespace Tests.VocaluxeLib.XML
|
||||
</root>";
|
||||
_AssertSerDeserMatch<SDefault>(s);
|
||||
var xml = new CXmlDeserializer(new CXmlErrorHandler(exception => { }));
|
||||
SDefault foo = xml.DeserializeString<SDefault>(@"<root />");
|
||||
Assert.AreEqual(foo.I, 1337);
|
||||
Assert.AreEqual(foo.F, null);
|
||||
Assert.AreEqual(foo.S, "Foo");
|
||||
Assert.AreEqual(foo.D, 666);
|
||||
Assert.AreEqual(foo.Sub.I, 111);
|
||||
string newXml = new CXmlSerializer().Serialize(foo);
|
||||
Assert.AreEqual(_Head + @"<root>
|
||||
var foo = xml.DeserializeString<SDefault>(@"<root />");
|
||||
Assert.That(foo.I, Is.EqualTo(1337));
|
||||
Assert.That(foo.F, Is.Null);
|
||||
Assert.That(foo.S, Is.EqualTo("Foo"));
|
||||
Assert.That(foo.D, Is.EqualTo(666));
|
||||
Assert.That(foo.Sub.I, Is.EqualTo(111));
|
||||
var newXml = new CXmlSerializer().Serialize(foo);
|
||||
Assert.That(newXml, Is.EqualTo(_Head + @"<root>
|
||||
<Sub />
|
||||
</root>", newXml);
|
||||
</root>"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -382,9 +390,9 @@ namespace Tests.VocaluxeLib.XML
|
||||
</root>";
|
||||
_AssertSerDeserMatch<SUnescaping>(s);
|
||||
var xml = new CXmlDeserializer(new CXmlErrorHandler(exception => { }));
|
||||
SUnescaping foo = xml.DeserializeString<SUnescaping>(@"<root />");
|
||||
Assert.AreEqual(foo.S, "Foo\'Bar");
|
||||
Assert.AreEqual(foo.Sub.S, "Foo\'Bar");
|
||||
var foo = xml.DeserializeString<SUnescaping>(@"<root />");
|
||||
Assert.That(foo.S, Is.EqualTo("Foo\'Bar"));
|
||||
Assert.That(foo.Sub.S, Is.EqualTo("Foo\'Bar"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -398,37 +406,38 @@ namespace Tests.VocaluxeLib.XML
|
||||
</root>";
|
||||
_AssertSerDeserMatch<SUnescaping>(s);
|
||||
var xml = new CXmlDeserializer(new CXmlErrorHandler(exception => { }), true);
|
||||
SUnescaping foo = xml.DeserializeString<SUnescaping>(@"<root />");
|
||||
Assert.AreEqual(foo.S, "Foo'Bar");
|
||||
Assert.AreEqual(foo.Sub.S, "Foo'Bar");
|
||||
var foo = xml.DeserializeString<SUnescaping>(@"<root />");
|
||||
Assert.That(foo.S, Is.EqualTo("Foo'Bar"));
|
||||
Assert.That(foo.Sub.S, Is.EqualTo("Foo'Bar"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestRealFiles([Values(typeof(SThemeCover), typeof(CConfig.SConfig), /*typeof(SThemeScreen),*/ typeof(SDefaultFonts), typeof(SSkin), typeof(STheme), typeof(Dictionary<string, string>))] Type type)
|
||||
public void TestRealFiles(
|
||||
[Values(typeof(SThemeCover), typeof(CConfig.SConfig), /*typeof(SThemeScreen),*/ typeof(SDefaultFonts), typeof(SSkin), typeof(STheme),
|
||||
typeof(Dictionary<string, string>))]
|
||||
Type type)
|
||||
{
|
||||
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "VocaluxeLib", "XML", "TestFiles");
|
||||
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "VocaluxeLib", "XML", "TestFiles");
|
||||
|
||||
string xmlPath = Path.Combine(filePath, type.Name + ".xml");
|
||||
var xmlPath = Path.Combine(filePath, type.Name + ".xml");
|
||||
var deser = new CXmlDeserializer();
|
||||
|
||||
object foo = deser.Deserialize(xmlPath, Activator.CreateInstance(type));
|
||||
var foo = deser.Deserialize(xmlPath, Activator.CreateInstance(type));
|
||||
|
||||
Assert.IsInstanceOf(type, foo, "Wrong type with " + type.Name);
|
||||
Assert.That(foo, Is.InstanceOf(type), "Wrong type with " + type.Name);
|
||||
var ser = new CXmlSerializer(type == typeof(CConfig.SConfig));
|
||||
string newXml = ser.Serialize(foo, type == typeof(Dictionary<string, string>) ? "resources" : null);
|
||||
var newXml = ser.Serialize(foo, type == typeof(Dictionary<string, string>) ? "resources" : null);
|
||||
// Typename will be uppercase but input is lowercase
|
||||
newXml = newXml.Replace("<String", "<string").Replace("</String", "</string");
|
||||
string oldXml = File.ReadAllText(xmlPath);
|
||||
var oldXml = File.ReadAllText(xmlPath);
|
||||
// Trim trailing whitespace/newlines
|
||||
oldXml = oldXml.TrimEnd('\r', '\n', ' ', '\t');
|
||||
newXml = newXml.TrimEnd('\r', '\n', ' ', '\t');
|
||||
Assert.AreEqual(oldXml, newXml, "Reconstructed XML has differences.");
|
||||
Assert.That(newXml, Is.EqualTo(oldXml), "Reconstructed XML has differences.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper
|
||||
|
||||
#pragma warning disable 649
|
||||
#pragma warning disable 169
|
||||
private struct SBasic
|
||||
@@ -539,29 +548,26 @@ namespace Tests.VocaluxeLib.XML
|
||||
public string S;
|
||||
}
|
||||
|
||||
|
||||
#pragma warning restore 169
|
||||
#pragma warning restore 649
|
||||
|
||||
private static void _AssertFail<T, T2>(String xmlString) where T2 : Exception where T : new()
|
||||
private static void _AssertXmlSerializeFailWithMissingElement<TSerializeType, TXmlException>(string xmlString)
|
||||
where TSerializeType : new()
|
||||
where TXmlException : CXmlException
|
||||
{
|
||||
var deserializer = new CXmlDeserializer();
|
||||
|
||||
Exception exception =
|
||||
Assert.Catch(() => deserializer.DeserializeString<T>(xmlString));
|
||||
Assert.IsInstanceOf(typeof(T2), exception);
|
||||
Assert.Throws<TXmlException>(new Action(() => deserializer.DeserializeString<TSerializeType>(xmlString)));
|
||||
}
|
||||
|
||||
private static T _AssertSerDeserMatch<T>(string xmlString) where T : new()
|
||||
{
|
||||
var deserializer = new CXmlDeserializer();
|
||||
var serializer = new CXmlSerializer();
|
||||
T foo = deserializer.DeserializeString<T>(xmlString);
|
||||
string xmlNew = serializer.Serialize(foo);
|
||||
Assert.AreEqual(xmlString, xmlNew);
|
||||
var foo = deserializer.DeserializeString<T>(xmlString);
|
||||
var xmlNew = serializer.Serialize(foo);
|
||||
Assert.That(xmlString, Is.EqualTo(xmlNew));
|
||||
return foo;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user