Skip to content

Commit 1f071de

Browse files
Burgynclaude
andcommitted
Make .tp section markers whitespace-tolerant between --- and keyword
Normalize whitespace in marker matching so that ---INIT, --- INIT, and --- INIT (and all other markers) are treated identically. Uses a compiled regex to normalize `---\s*` to `--- ` before comparison. Adds two tests covering no-space and extra-spaces variants. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 38a23a6 commit 1f071de

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

src/TeaPie/TestCases/TpFileParser.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.RegularExpressions;
2+
13
namespace TeaPie.TestCases;
24

35
internal class TpFileParser
@@ -139,8 +141,13 @@ private static (string Content, int NextIndex) ExtractSectionContent(string[] li
139141
return (string.Join("\n", sectionLines).Trim(), i);
140142
}
141143

144+
private static readonly Regex MarkerNormalizer = new(@"^---\s*", RegexOptions.Compiled);
145+
146+
private static string NormalizeLine(string line)
147+
=> MarkerNormalizer.Replace(line.TrimStart(), "--- ", 1);
148+
142149
private static bool IsMarker(string line, string marker)
143-
=> line.TrimStart().Equals(marker, StringComparison.OrdinalIgnoreCase);
150+
=> NormalizeLine(line).Equals(marker, StringComparison.OrdinalIgnoreCase);
144151

145152
private static bool IsAnyMarker(string line)
146153
=> IsTestCaseMarker(line)
@@ -150,13 +157,16 @@ private static bool IsAnyMarker(string line)
150157
|| IsMarker(line, TpConstants.EndMarker);
151158

152159
private static bool IsTestCaseMarker(string line)
153-
=> line.TrimStart().StartsWith(TpConstants.TestCaseMarker + " ", StringComparison.OrdinalIgnoreCase)
154-
|| line.TrimStart().Equals(TpConstants.TestCaseMarker, StringComparison.OrdinalIgnoreCase);
160+
{
161+
var normalized = NormalizeLine(line);
162+
return normalized.StartsWith(TpConstants.TestCaseMarker + " ", StringComparison.OrdinalIgnoreCase)
163+
|| normalized.Equals(TpConstants.TestCaseMarker, StringComparison.OrdinalIgnoreCase);
164+
}
155165

156166
private static string ExtractNameOrDefault(string line, string marker, string fallbackName)
157167
{
158-
var trimmed = line.TrimStart();
159-
var name = trimmed.Length > marker.Length ? trimmed[marker.Length..].Trim() : string.Empty;
168+
var normalized = NormalizeLine(line);
169+
var name = normalized.Length > marker.Length ? normalized[marker.Length..].Trim() : string.Empty;
160170
return string.IsNullOrWhiteSpace(name) ? fallbackName : name;
161171
}
162172
}

tests/TeaPie.Tests/TestCases/TpFileParserShould.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,60 @@ public void ParseThreeTestCasesWithVariousConfigurations()
310310
Null(result[2].TestContent);
311311
}
312312

313+
[Fact]
314+
public void ParseMarkersWithNoSpaceAfterDashes()
315+
{
316+
var content = string.Join("\n",
317+
"---TESTCASE No Space Test",
318+
"",
319+
"---INIT",
320+
"tp.SetVariable(\"X\", 1);",
321+
"",
322+
"---HTTP",
323+
"GET {{ApiBaseUrl}}/health",
324+
"",
325+
"---TEST",
326+
"tp.Test(\"ok\", () => NotNull(tp.Response));",
327+
"",
328+
"---END");
329+
330+
var result = _parser.Parse(content, "fallback");
331+
332+
Single(result);
333+
Equal("No Space Test", result[0].Name);
334+
NotNull(result[0].InitContent);
335+
Contains("tp.SetVariable", result[0].InitContent);
336+
Contains("GET {{ApiBaseUrl}}/health", result[0].HttpContent);
337+
NotNull(result[0].TestContent);
338+
Contains("tp.Test", result[0].TestContent);
339+
}
340+
341+
[Fact]
342+
public void ParseMarkersWithExtraSpacesAfterDashes()
343+
{
344+
var content = string.Join("\n",
345+
"--- TESTCASE Extra Spaces",
346+
"",
347+
"--- INIT",
348+
"tp.SetVariable(\"X\", 1);",
349+
"",
350+
"--- HTTP",
351+
"GET {{ApiBaseUrl}}/health",
352+
"",
353+
"--- TEST",
354+
"tp.Test(\"ok\", () => NotNull(tp.Response));",
355+
"",
356+
"--- END");
357+
358+
var result = _parser.Parse(content, "fallback");
359+
360+
Single(result);
361+
Equal("Extra Spaces", result[0].Name);
362+
NotNull(result[0].InitContent);
363+
Contains("GET {{ApiBaseUrl}}/health", result[0].HttpContent);
364+
NotNull(result[0].TestContent);
365+
}
366+
313367
[Fact]
314368
public void ParseMultipleRequestsInHttpSection()
315369
{

0 commit comments

Comments
 (0)