From 7604397c26eaf828e3cf8242df35ddfec9f0719e Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Mon, 19 Apr 2021 17:26:32 -0500 Subject: [PATCH] refactor: Make TestData object usable outside tests TestData split out into ResourceDataReader in a new Common project, so that we have a place to share common code between tests and non-tests. --- src/Common.Tests/Common.Tests.csproj | 15 ++++++ src/Common.Tests/DefaultDataFile.txt | 1 + src/Common.Tests/ResourceDataReaderTest.cs | 38 +++++++++++++ .../TestData}/DataFile.txt | 0 src/Common/Common.csproj | 2 + src/Common/ResourceDataReader.cs | 43 +++++++++++++++ src/TestLibrary.Tests/DataFileWontBeFound.txt | 1 - .../OtherData/AnotherDataFile.txt | 1 - src/TestLibrary.Tests/TestDataTest.cs | 53 ------------------- .../TestLibrary.Tests.csproj | 5 -- src/TestLibrary/TestData.cs | 41 -------------- src/TestLibrary/TestLibrary.csproj | 3 ++ .../Config/ConfigurationLoaderTest.cs | 5 +- .../ReleaseProfileParserTest.cs | 9 ++-- src/Trash/Trash.csproj | 4 ++ src/TrashUpdater.sln | 12 +++++ 16 files changed, 126 insertions(+), 107 deletions(-) create mode 100644 src/Common.Tests/Common.Tests.csproj create mode 100644 src/Common.Tests/DefaultDataFile.txt create mode 100644 src/Common.Tests/ResourceDataReaderTest.cs rename src/{TestLibrary.Tests/Data => Common.Tests/TestData}/DataFile.txt (100%) create mode 100644 src/Common/Common.csproj create mode 100644 src/Common/ResourceDataReader.cs delete mode 100644 src/TestLibrary.Tests/DataFileWontBeFound.txt delete mode 100644 src/TestLibrary.Tests/OtherData/AnotherDataFile.txt delete mode 100644 src/TestLibrary.Tests/TestDataTest.cs delete mode 100644 src/TestLibrary/TestData.cs diff --git a/src/Common.Tests/Common.Tests.csproj b/src/Common.Tests/Common.Tests.csproj new file mode 100644 index 00000000..472adcd3 --- /dev/null +++ b/src/Common.Tests/Common.Tests.csproj @@ -0,0 +1,15 @@ + + + false + + + + + + + + + + + + diff --git a/src/Common.Tests/DefaultDataFile.txt b/src/Common.Tests/DefaultDataFile.txt new file mode 100644 index 00000000..60d69f0b --- /dev/null +++ b/src/Common.Tests/DefaultDataFile.txt @@ -0,0 +1 @@ +DefaultDataFile diff --git a/src/Common.Tests/ResourceDataReaderTest.cs b/src/Common.Tests/ResourceDataReaderTest.cs new file mode 100644 index 00000000..8757762b --- /dev/null +++ b/src/Common.Tests/ResourceDataReaderTest.cs @@ -0,0 +1,38 @@ +using System; +using FluentAssertions; +using NUnit.Framework; + +namespace Common.Tests +{ + [TestFixture] + [Parallelizable(ParallelScope.All)] + public class ResourceDataReaderTest + { + [Test] + public void GetResourceData_DefaultDir_ReturnResourceData() + { + var testData = new ResourceDataReader(typeof(ResourceDataReaderTest)); + var data = testData.ReadData("DefaultDataFile.txt"); + data.Trim().Should().Be("DefaultDataFile"); + } + + [Test] + public void GetResourceData_NonexistentFile_Throw() + { + var testData = new ResourceDataReader(typeof(ResourceDataReaderTest)); + Action act = () => testData.ReadData("DataFileWontBeFound.txt"); + + act.Should() + .Throw() + .WithMessage("Embedded resource not found*"); + } + + [Test] + public void ReadData_ExplicitSubDir_ReturnResourceData() + { + var testData = new ResourceDataReader(typeof(ResourceDataReaderTest), "TestData"); + var data = testData.ReadData("DataFile.txt"); + data.Trim().Should().Be("DataFile"); + } + } +} diff --git a/src/TestLibrary.Tests/Data/DataFile.txt b/src/Common.Tests/TestData/DataFile.txt similarity index 100% rename from src/TestLibrary.Tests/Data/DataFile.txt rename to src/Common.Tests/TestData/DataFile.txt diff --git a/src/Common/Common.csproj b/src/Common/Common.csproj new file mode 100644 index 00000000..35e3d842 --- /dev/null +++ b/src/Common/Common.csproj @@ -0,0 +1,2 @@ + + diff --git a/src/Common/ResourceDataReader.cs b/src/Common/ResourceDataReader.cs new file mode 100644 index 00000000..e03d4d22 --- /dev/null +++ b/src/Common/ResourceDataReader.cs @@ -0,0 +1,43 @@ +using System; +using System.IO; +using System.Reflection; +using System.Text; + +namespace Common +{ + public class ResourceDataReader + { + private readonly Assembly? _assembly; + private readonly string? _namespace; + private readonly string _subdirectory; + + public ResourceDataReader(Type typeWithNamespaceToUse, string subdirectory = "") + { + _subdirectory = subdirectory; + _namespace = typeWithNamespaceToUse.Namespace; + _assembly = Assembly.GetAssembly(typeWithNamespaceToUse); + } + + public string ReadData(string filename) + { + var nameBuilder = new StringBuilder(); + nameBuilder.Append(_namespace); + if (!string.IsNullOrEmpty(_subdirectory)) + { + nameBuilder.Append($".{_subdirectory}"); + } + + nameBuilder.Append($".{filename}"); + + var resourceName = nameBuilder.ToString(); + using var stream = _assembly?.GetManifestResourceStream(resourceName); + if (stream == null) + { + throw new ArgumentException($"Embedded resource not found: {resourceName}"); + } + + using var reader = new StreamReader(stream); + return reader.ReadToEnd(); + } + } +} diff --git a/src/TestLibrary.Tests/DataFileWontBeFound.txt b/src/TestLibrary.Tests/DataFileWontBeFound.txt deleted file mode 100644 index 9a67e972..00000000 --- a/src/TestLibrary.Tests/DataFileWontBeFound.txt +++ /dev/null @@ -1 +0,0 @@ -DataFileWontBeFound diff --git a/src/TestLibrary.Tests/OtherData/AnotherDataFile.txt b/src/TestLibrary.Tests/OtherData/AnotherDataFile.txt deleted file mode 100644 index a523bba2..00000000 --- a/src/TestLibrary.Tests/OtherData/AnotherDataFile.txt +++ /dev/null @@ -1 +0,0 @@ -AnotherDataFile diff --git a/src/TestLibrary.Tests/TestDataTest.cs b/src/TestLibrary.Tests/TestDataTest.cs deleted file mode 100644 index 353abb3b..00000000 --- a/src/TestLibrary.Tests/TestDataTest.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using FluentAssertions; -using NUnit.Framework; - -namespace TestLibrary.Tests -{ - internal class TestFixtureMissingAttribute - { - } - - [TestFixture] - public class TestDataTest - { - [Test] - public void Construction_ClassMissingAttribute_Throw() - { - // ReSharper disable once ObjectCreationAsStatement - Action act = () => new TestData(); - - act.Should() - .Throw() - .WithMessage("*does not have the [TestFixture] attribute"); - } - - [Test] - public void GetResourceData_CustomDir_ReturnResourceData() - { - TestData testData = new(); - testData.DataSubdirectoryName = "OtherData"; - var data = testData.GetResourceData("AnotherDataFile.txt"); - data.Trim().Should().Be("AnotherDataFile"); - } - - [Test] - public void GetResourceData_DefaultDir_ReturnResourceData() - { - TestData testData = new(); - var data = testData.GetResourceData("DataFile.txt"); - data.Trim().Should().Be("DataFile"); - } - - [Test] - public void GetResourceData_NonexistentFile_Throw() - { - TestData testData = new(); - Action act = () => testData.GetResourceData("DataFileWontBeFound.txt"); - - act.Should() - .Throw() - .WithMessage("Embedded resource not found*"); - } - } -} diff --git a/src/TestLibrary.Tests/TestLibrary.Tests.csproj b/src/TestLibrary.Tests/TestLibrary.Tests.csproj index 474578da..56f817ad 100644 --- a/src/TestLibrary.Tests/TestLibrary.Tests.csproj +++ b/src/TestLibrary.Tests/TestLibrary.Tests.csproj @@ -6,9 +6,4 @@ - - - - - diff --git a/src/TestLibrary/TestData.cs b/src/TestLibrary/TestData.cs deleted file mode 100644 index 91f86fbd..00000000 --- a/src/TestLibrary/TestData.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.IO; -using System.Reflection; -using NUnit.Framework; - -namespace TestLibrary -{ - public class TestData - { - private readonly Assembly? _assembly; - private readonly string? _namespace; - - public TestData() - { - var attributes = typeof(TTestFixtureClass).GetCustomAttributes(typeof(TestFixtureAttribute), true); - if (attributes.Length == 0) - { - throw new ArgumentException( - $"{typeof(TTestFixtureClass).Name} does not have the [TestFixture] attribute"); - } - - _namespace = typeof(TTestFixtureClass).Namespace; - _assembly = Assembly.GetAssembly(typeof(TTestFixtureClass)); - } - - public string DataSubdirectoryName { get; set; } = "Data"; - - public string GetResourceData(string name) - { - var resourceName = $"{_namespace}.{DataSubdirectoryName}.{name}"; - using var stream = _assembly?.GetManifestResourceStream(resourceName); - if (stream == null) - { - throw new ArgumentException($"Embedded resource not found: {resourceName}"); - } - - using var reader = new StreamReader(stream); - return reader.ReadToEnd(); - } - } -} diff --git a/src/TestLibrary/TestLibrary.csproj b/src/TestLibrary/TestLibrary.csproj index d7877722..5bf0c226 100644 --- a/src/TestLibrary/TestLibrary.csproj +++ b/src/TestLibrary/TestLibrary.csproj @@ -4,4 +4,7 @@ + + + diff --git a/src/Trash.Tests/Config/ConfigurationLoaderTest.cs b/src/Trash.Tests/Config/ConfigurationLoaderTest.cs index 4ff9f2ff..3d13deba 100644 --- a/src/Trash.Tests/Config/ConfigurationLoaderTest.cs +++ b/src/Trash.Tests/Config/ConfigurationLoaderTest.cs @@ -4,6 +4,7 @@ using System.IO; using System.IO.Abstractions; using System.Linq; using System.Text; +using Common; using FluentAssertions; using NSubstitute; using NUnit.Framework; @@ -22,13 +23,13 @@ namespace Trash.Tests.Config { private TextReader GetResourceData(string file) { - var testData = new TestData(); + var testData = new ResourceDataReader(typeof(ConfigurationLoaderTest), "Data"); if (testData == null) { throw new InvalidOperationException("TestData object has not been created yet"); } - return new StringReader(testData.GetResourceData(file)); + return new StringReader(testData.ReadData(file)); } [Test] diff --git a/src/Trash.Tests/Sonarr/ReleaseProfile/ReleaseProfileParserTest.cs b/src/Trash.Tests/Sonarr/ReleaseProfile/ReleaseProfileParserTest.cs index 37ebdad6..d0d09373 100644 --- a/src/Trash.Tests/Sonarr/ReleaseProfile/ReleaseProfileParserTest.cs +++ b/src/Trash.Tests/Sonarr/ReleaseProfile/ReleaseProfileParserTest.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using Common; using FluentAssertions; using NSubstitute; using NUnit.Framework; @@ -38,7 +39,7 @@ namespace Trash.Tests.Sonarr.ReleaseProfile public SonarrConfiguration Config { get; } public ReleaseProfileGuideParser GuideParser { get; } - public TestData TestData { get; } = new(); + public ResourceDataReader TestData { get; } = new(typeof(ReleaseProfileParserTest), "Data"); public IDictionary ParseWithDefaults(string markdown) { @@ -116,7 +117,7 @@ One more public void Parse_IgnoredRequiredPreferredScores() { var context = new Context(); - var markdown = context.TestData.GetResourceData("test_parse_markdown_complete_doc.md"); + var markdown = context.TestData.ReadData("test_parse_markdown_complete_doc.md"); var results = context.GuideParser.ParseMarkdown(context.Config.ReleaseProfiles.First(), markdown); results.Count.Should().Be(1); @@ -132,7 +133,7 @@ One more public void Parse_IncludePreferredWhenRenaming() { var context = new Context(); - var markdown = context.TestData.GetResourceData("include_preferred_when_renaming.md"); + var markdown = context.TestData.ReadData("include_preferred_when_renaming.md"); var results = context.ParseWithDefaults(markdown); results.Should() @@ -350,7 +351,7 @@ abc new() {StrictNegativeScores = true} }; - var markdown = context.TestData.GetResourceData("strict_negative_scores.md"); + var markdown = context.TestData.ReadData("strict_negative_scores.md"); var results = context.ParseWithDefaults(markdown); results.Should() diff --git a/src/Trash/Trash.csproj b/src/Trash/Trash.csproj index 3c49be89..2e3c6805 100644 --- a/src/Trash/Trash.csproj +++ b/src/Trash/Trash.csproj @@ -17,4 +17,8 @@ + + + + diff --git a/src/TrashUpdater.sln b/src/TrashUpdater.sln index 5dafa2e1..43fab4ff 100644 --- a/src/TrashUpdater.sln +++ b/src/TrashUpdater.sln @@ -14,6 +14,10 @@ ProjectSection(SolutionItems) = preProject Directory.Build.targets = Directory.Build.targets EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Common\Common.csproj", "{18CF1FCA-7983-4423-8B7E-4A830108C624}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.Tests", "Common.Tests\Common.Tests.csproj", "{0720939D-1CA6-43D7-BBED-F8F894C4F562}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -36,6 +40,14 @@ Global {BF105B2F-8E13-48AD-BF72-DF7EFEB018B6}.Debug|Any CPU.Build.0 = Debug|Any CPU {BF105B2F-8E13-48AD-BF72-DF7EFEB018B6}.Release|Any CPU.ActiveCfg = Release|Any CPU {BF105B2F-8E13-48AD-BF72-DF7EFEB018B6}.Release|Any CPU.Build.0 = Release|Any CPU + {18CF1FCA-7983-4423-8B7E-4A830108C624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {18CF1FCA-7983-4423-8B7E-4A830108C624}.Debug|Any CPU.Build.0 = Debug|Any CPU + {18CF1FCA-7983-4423-8B7E-4A830108C624}.Release|Any CPU.ActiveCfg = Release|Any CPU + {18CF1FCA-7983-4423-8B7E-4A830108C624}.Release|Any CPU.Build.0 = Release|Any CPU + {0720939D-1CA6-43D7-BBED-F8F894C4F562}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0720939D-1CA6-43D7-BBED-F8F894C4F562}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0720939D-1CA6-43D7-BBED-F8F894C4F562}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0720939D-1CA6-43D7-BBED-F8F894C4F562}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution EndGlobalSection