From dc0bc11d3d9876e1ecf80736cc02fef9c192ab0f Mon Sep 17 00:00:00 2001 From: Darren Fuller Date: Thu, 24 Oct 2024 18:02:29 +0100 Subject: [PATCH] Refactor test and update regex to clean name strings 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. --- DotPrompt.Tests/PromptFileTests.cs | 8 ++++++-- DotPrompt/PromptFile.cs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/DotPrompt.Tests/PromptFileTests.cs b/DotPrompt.Tests/PromptFileTests.cs index f0e1d92..1c09273 100644 --- a/DotPrompt.Tests/PromptFileTests.cs +++ b/DotPrompt.Tests/PromptFileTests.cs @@ -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 un*cl()ean", "this-is-pretty-unclean")] + public void FromStream_WithNamePart_CleansTheName(string inputName, string expectedName) { const string content = "prompts:\n system: System prompt\n user: User prompt"; using var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); diff --git a/DotPrompt/PromptFile.cs b/DotPrompt/PromptFile.cs index d297dfa..76c4f02 100644 --- a/DotPrompt/PromptFile.cs +++ b/DotPrompt/PromptFile.cs @@ -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)]