Skip to content

Commit

Permalink
feat: Improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 2, 2024
1 parent 775b289 commit 191fa25
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 61 deletions.
6 changes: 2 additions & 4 deletions AzureLiquid.Preview/PreviewProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,15 @@ public string Render()
}

var parser = new LiquidParser();
return !SetParserContent(parser, content) ?
string.Empty :
RenderTemplate(parser, template);
return !SetParserContent(parser, content) ? string.Empty : RenderTemplate(parser, template);
}

/// <summary>
/// Reads the file content.
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns>The file content.</returns>
private string ReadFileContent(string filePath)
internal string ReadFileContent(string filePath)

Check notice on line 232 in AzureLiquid.Preview/PreviewProcess.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Member can be made private (non-private accessibility)

Method 'ReadFileContent' can be made private
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion AzureLiquid.Preview/PreviewProcessArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static bool IsArgMatch(string arg, string key)
/// </summary>
/// <param name="args">The passed arguments.</param>
/// <param name="key">The key.</param>
/// <returns><c>true</c> if the argument was found, otherwise <c>false</c>,</returns>
/// <returns><c>true</c> if the argument was found, otherwise <c>false</c>.</returns>
public static bool HasArgument(string[] args, string key) => args.Any(arg => IsArgMatch(arg, key));

/// <summary>
Expand Down
142 changes: 86 additions & 56 deletions AzureLiquid.Tests/PreviewProcessArgumentsTests.cs
Original file line number Diff line number Diff line change
@@ -1,70 +1,100 @@
using System.IO;
using Xunit;
// <copyright file="PreviewProcessArgumentsTests.cs">
// Licensed under the open source Apache License, Version 2.0.
// </copyright>

using AzureLiquid.Preview;
using FluentAssertions;
using Xunit;

namespace AzureLiquid.Preview.Tests
namespace AzureLiquid.Tests;

/// <summary>
/// Unit tests for the <see cref="PreviewProcessArguments" /> class.
/// </summary>
public class PreviewProcessArgumentsTests
{
public class PreviewProcessArgumentsTests
/// <summary>
/// Tests the <see cref="PreviewProcessArguments.GetArgumentIndex" /> method.
/// </summary>
/// <param name="args">The array of arguments.</param>
/// <param name="key">The key to search for.</param>
/// <param name="expectedIndex">The expected index of the key in the arguments array.</param>
[Theory]
[InlineData(new[] { "--template", "template.liquid" }, "template", 0)]
[InlineData(new[] { "--content", "content.json" }, "content", 0)]
[InlineData(new[] { "--output", "output.txt" }, "output", 0)]
[InlineData(new[] { "--watch" }, "watch", 0)]
[InlineData(new[] { "--template", "template.liquid" }, "content", -1)]
public void GetArgumentIndex_ShouldReturnCorrectIndex(string[] args, string key, int expectedIndex)
{
[Theory]
[InlineData(new string[] { "--template", "template.liquid" }, "template", 0)]
[InlineData(new string[] { "--content", "content.json" }, "content", 0)]
[InlineData(new string[] { "--output", "output.txt" }, "output", 0)]
[InlineData(new string[] { "--watch" }, "watch", 0)]
[InlineData(new string[] { "--template", "template.liquid" }, "content", -1)]
public void GetArgumentIndex_ShouldReturnCorrectIndex(string[] args, string key, int expectedIndex)
{
// Act
var index = PreviewProcessArguments.GetArgumentIndex(args, key);
// Act
var index = PreviewProcessArguments.GetArgumentIndex(args, key);

// Assert
index.Should().Be(expectedIndex);
}
// Assert
index.Should().Be(expectedIndex);
}

[Theory]
[InlineData("--template", "template", true)]
[InlineData("--content", "content", true)]
[InlineData("--output", "output", true)]
[InlineData("--watch", "watch", true)]
[InlineData("--template", "content", false)]
public void IsArgMatch_ShouldReturnCorrectResult(string arg, string key, bool expectedResult)
{
// Act
var result = PreviewProcessArguments.IsArgMatch(arg, key);
/// <summary>
/// Tests the <see cref="PreviewProcessArguments.IsArgMatch" /> method.
/// </summary>
/// <param name="arg">The argument to check.</param>
/// <param name="key">The key to match against.</param>
/// <param name="expectedResult">The expected result of the match.</param>
[Theory]
[InlineData("--template", "template", true)]
[InlineData("--content", "content", true)]
[InlineData("--output", "output", true)]
[InlineData("--watch", "watch", true)]
[InlineData("--template", "content", false)]
public void IsArgMatch_ShouldReturnCorrectResult(string arg, string key, bool expectedResult)
{
// Act
var result = PreviewProcessArguments.IsArgMatch(arg, key);

// Assert
result.Should().Be(expectedResult);
}
// Assert
result.Should().Be(expectedResult);
}

[Theory]
[InlineData(new string[] { "--template", "template.liquid" }, "template", true)]
[InlineData(new string[] { "--content", "content.json" }, "content", true)]
[InlineData(new string[] { "--output", "output.txt" }, "output", true)]
[InlineData(new string[] { "--watch" }, "watch", true)]
[InlineData(new string[] { "--template", "template.liquid" }, "content", false)]
public void HasArgument_ShouldReturnCorrectResult(string[] args, string key, bool expectedResult)
{
// Act
var result = PreviewProcessArguments.HasArgument(args, key);
/// <summary>
/// Tests the <see cref="PreviewProcessArguments.HasArgument" /> method.
/// </summary>
/// <param name="args">The array of arguments.</param>
/// <param name="key">The key to search for.</param>
/// <param name="expectedResult">The expected result of the search.</param>
[Theory]
[InlineData(new[] { "--template", "template.liquid" }, "template", true)]
[InlineData(new[] { "--content", "content.json" }, "content", true)]
[InlineData(new[] { "--output", "output.txt" }, "output", true)]
[InlineData(new[] { "--watch" }, "watch", true)]
[InlineData(new[] { "--template", "template.liquid" }, "content", false)]
public void HasArgument_ShouldReturnCorrectResult(string[] args, string key, bool expectedResult)
{
// Act
var result = PreviewProcessArguments.HasArgument(args, key);

// Assert
result.Should().Be(expectedResult);
}
// Assert
result.Should().Be(expectedResult);
}

[Theory]
[InlineData(new string[] { "--template", "template.liquid" }, "template", "template.liquid")]
[InlineData(new string[] { "--content", "content.json" }, "content", "content.json")]
[InlineData(new string[] { "--output", "output.txt" }, "output", "output.txt")]
public void ParsePath_ShouldReturnCorrectPath(string[] args, string key, string expectedPath)
{
// Arrange
var previewArgs = new PreviewProcessArguments();
/// <summary>
/// Tests the <see cref="PreviewProcessArguments.ParsePath" /> method.
/// </summary>
/// <param name="args">The array of arguments.</param>
/// <param name="key">The key to search for.</param>
/// <param name="expectedPath">The expected path associated with the key.</param>
[Theory]
[InlineData(new[] { "--template", "template.liquid" }, "template", "template.liquid")]
[InlineData(new[] { "--content", "content.json" }, "content", "content.json")]
[InlineData(new[] { "--output", "output.txt" }, "output", "output.txt")]
public void ParsePath_ShouldReturnCorrectPath(string[] args, string key, string expectedPath)
{
// Arrange
var previewArgs = new PreviewProcessArguments();

// Act
var path = previewArgs.ParsePath(args, key);
// Act
var path = previewArgs.ParsePath(args, key);

// Assert
path.Should().Contain(expectedPath);
}
// Assert
path.Should().Contain(expectedPath);
}
}

0 comments on commit 191fa25

Please sign in to comment.