Skip to content

Commit

Permalink
Refactor test and update regex to clean name strings (#8)
Browse files Browse the repository at this point in the history
Refactored unit test to use [Theory] and included multiple inline data sets for better coverage of name cleaning logic. Also updated the regex pattern to allow hyphens in names, ensuring more accurate cleaning of input strings.
  • Loading branch information
dazfuller authored Oct 24, 2024
1 parent 17bfcde commit c8487c0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions DotPrompt.Tests/PromptFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ public void FromStream_WithUnreadableStream_ThrowsException()
Assert.Contains("Stream is not in a readable state", exception.Message);
}

[Fact]
public void FromStream_WithInvalidCharactersInNameForWindows_CleansTheName()
[Theory]
[InlineData("clean\r\n\r\nthis name", "clean-this-name")]
[InlineData("do-not-clean", "do-not-clean")]
[InlineData("My COOL nAMe", "my-cool-name")]
[InlineData("this <is .pretty> un*cl()ean", "this-is-pretty-unclean")]
public void FromStream_WithNamePart_CleansTheName(string inputName, string expectedName)

Check warning on line 175 in DotPrompt.Tests/PromptFileTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'FromStream_WithNamePart_CleansTheName' on test class 'PromptFileTests' does not use parameter 'inputName'. Use the parameter, or remove the parameter and associated data. (https://xunit.net/xunit.analyzers/rules/xUnit1026)

Check warning on line 175 in DotPrompt.Tests/PromptFileTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'FromStream_WithNamePart_CleansTheName' on test class 'PromptFileTests' does not use parameter 'expectedName'. Use the parameter, or remove the parameter and associated data. (https://xunit.net/xunit.analyzers/rules/xUnit1026)

Check warning on line 175 in DotPrompt.Tests/PromptFileTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'FromStream_WithNamePart_CleansTheName' on test class 'PromptFileTests' does not use parameter 'inputName'. Use the parameter, or remove the parameter and associated data. (https://xunit.net/xunit.analyzers/rules/xUnit1026)

Check warning on line 175 in DotPrompt.Tests/PromptFileTests.cs

View workflow job for this annotation

GitHub Actions / build

Theory method 'FromStream_WithNamePart_CleansTheName' on test class 'PromptFileTests' does not use parameter 'expectedName'. Use the parameter, or remove the parameter and associated data. (https://xunit.net/xunit.analyzers/rules/xUnit1026)
{
const string content = "prompts:\n system: System prompt\n user: User prompt";
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
Expand Down
2 changes: 1 addition & 1 deletion DotPrompt/PromptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private static string CleanName(string name)
return trimmedName;
}

[GeneratedRegex(@"([^A-Za-z0-9 \r\n]*)", RegexOptions.Multiline | RegexOptions.Compiled)]
[GeneratedRegex(@"([^A-Za-z0-9 \-\r\n]*)", RegexOptions.Multiline | RegexOptions.Compiled)]
private static partial Regex InvalidCharactersRegex();

[GeneratedRegex(@"[\s\r\n]+", RegexOptions.Multiline | RegexOptions.Compiled)]
Expand Down

0 comments on commit c8487c0

Please sign in to comment.