Skip to content

Commit

Permalink
tests for new helper methods
Browse files Browse the repository at this point in the history
- added tests for the new public helper methods
- added null checks handling
  • Loading branch information
dnenov committed Feb 26, 2024
1 parent 98ee40b commit 9c39751
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/DynamoUtilities/PathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,16 +501,28 @@ public static string getServiceConfigValues(object o, string serviceKey)
}

/// <summary>
/// This function will load embedded resources such as HTML and JS files and return the content as a string
/// Loads embedded resources such as HTML and JS files and returns the content as a string.
/// </summary>
/// <param name="resourcePath">The resource path to rreturn</param>
/// <returns>The embeded resource as string</returns>
/// <param name="resourcePath">The resource path to return.</param>
/// <param name="assembly">The assembly containing the resource.</param>
/// <returns>The embedded resource as a string.</returns>
public static string LoadEmbeddedResourceAsString(string resourcePath, Assembly assembly)
{
if (string.IsNullOrEmpty(resourcePath))
throw new ArgumentNullException(nameof(resourcePath), "The resource path cannot be null or empty.");

if (assembly == null)
throw new ArgumentNullException(nameof(assembly), "The assembly cannot be null.");

using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
if (stream == null)
throw new FileNotFoundException("The specified resource was not found in the assembly.", resourcePath);

using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}

Expand All @@ -520,8 +532,15 @@ public static string LoadEmbeddedResourceAsString(string resourcePath, Assembly
/// <param name="resourcePath">The location of the font resource</param>
/// <param name="outputPath">The temporary path to save the resource to</param>
/// <param name="outputFileName">The name of the temporary resource file</param>
/// <param name="assembly">The assembly containing the resource</param>
public static void ExtractAndSaveEmbeddedFont(string resourcePath, string outputPath, string outputFileName, Assembly assembly)
{
if (string.IsNullOrEmpty(resourcePath) || string.IsNullOrEmpty(outputPath) || string.IsNullOrEmpty(outputFileName))
throw new ArgumentNullException($"One of the input arguments is null or empty.");

if (assembly == null)
throw new ArgumentNullException($"The assembly cannot be null.");

using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
if (stream != null)
Expand Down
6 changes: 6 additions & 0 deletions test/DynamoCoreTests/DynamoCoreTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<ItemGroup>
<Compile Remove="AstBuilderTest.cs" />
<Compile Remove="Saving.cs" />
</ItemGroup>
<ItemGroup>
<None Remove="TestResource.txt" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" ExcludeAssets="none" />
Expand Down Expand Up @@ -130,6 +133,9 @@
<Content Include="Crypto\AnAlteredFile.txt" />
<Content Include="Crypto\AnImportantFile.txt" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources\TestResource.txt" />
</ItemGroup>
<Target Name="CopyDisableADPTestDeps">
<!--ms diag runtime + deps -->
<Message Importance="High" Text="copying disable adp test deps to test dependencies folder" />
Expand Down
1 change: 1 addition & 0 deletions test/DynamoCoreTests/Resources/TestResource.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dynamo resource.
54 changes: 54 additions & 0 deletions test/DynamoCoreTests/UtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
using Dynamo.Configuration;
using Dynamo.Engine;
Expand Down Expand Up @@ -837,5 +838,58 @@ public void GenerateSnapshotNameTest()
var snapshotNameWithoutTimestamp = PathHelper.GetScreenCaptureNameFromPath(examplePath, false);
Assert.AreEqual(snapshotNameWithoutTimestamp, "Add");
}

[Test]
public void LoadEmbeddedResourceAsString_ReturnsCorrectContent()
{
// Arrange
var expectedContent = "Dynamo resource.";
var resourceName = "Dynamo.Tests.Resources.TestResource.txt";

// Act
var assembly = Assembly.GetExecutingAssembly();
var actualContent = PathHelper.LoadEmbeddedResourceAsString(resourceName, assembly);

// Assert
Assert.AreEqual(expectedContent, actualContent, "The content loaded from the embedded resource does not match the expected content.");
}

[Test]
public void ExtractAndSaveEmbeddedFont_CreatesFileWithCorrectContent()
{
// Arrange
var tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
var outputFileName = "testResourceFile.ttf";
var assembly = Assembly.GetExecutingAssembly();
var resourcePath = "Dynamo.Tests.Resources.TestResource.txt";

// Act
PathHelper.ExtractAndSaveEmbeddedFont(resourcePath, tempDirectory, outputFileName, assembly);

var outputFileFullPath = Path.Combine(tempDirectory, outputFileName);

// Assert
Assert.IsTrue(File.Exists(outputFileFullPath), "The output file was not created.");

var originalContent = GetEmbeddedResourceContent(resourcePath, assembly);
var extractedContent = File.ReadAllBytes(outputFileFullPath);
Assert.AreEqual(originalContent, extractedContent, "The contents of the extracted file do not match the original.");

// Clean up
Directory.Delete(tempDirectory, true);
}

private byte[] GetEmbeddedResourceContent(string resourcePath, Assembly assembly)
{
using (var stream = assembly.GetManifestResourceStream(resourcePath))
{
if (stream == null) throw new InvalidOperationException("Resource not found.");

var content = new byte[stream.Length];
stream.Read(content, 0, content.Length);
return content;
}
}

}
}

0 comments on commit 9c39751

Please sign in to comment.