Skip to content

Commit 4b86c13

Browse files
committed
feat: add validation command for test cases with variables and enhance test case handling
1 parent d12a552 commit 4b86c13

File tree

7 files changed

+95
-11
lines changed

7 files changed

+95
-11
lines changed

.vscode/launch.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@
120120
],
121121
"cwd": "${workspaceFolder}/src/testr.Cli",
122122
"stopAtEntry": false
123-
}
123+
},
124+
{
125+
"name": "testR:validate-TC-Login-001",
126+
"type": "coreclr",
127+
"request": "launch",
128+
"preLaunchTask": "build-testR",
129+
"program": "${workspaceFolder}/src/testr.Cli/bin/Debug/net9.0/tomware.TestR.dll",
130+
"args": [
131+
"validate",
132+
"TC-Login-001",
133+
"-i",
134+
"../../samples/Definitions/localhost"
135+
],
136+
"cwd": "${workspaceFolder}/src/testr.Cli",
137+
"stopAtEntry": false
138+
},
124139
]
125140
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Id": "51458661-5966-40f2-8fd7-c8354380eadc",
3+
"IssueId": "",
4+
"Prefix": "Fixed",
5+
"Tag": "",
6+
"Message": "Fixed validation command for test cases that contain variables.",
7+
"CreatedAt": "2025-07-02T05:09:47.4358615+00:00",
8+
"CreatedBy": "thomasduft"
9+
}

src/testr.Cli/Commands/RunCommand.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,12 @@ CancellationToken cancellationToken
158158
{
159159
_stopwatch.Reset();
160160
var domain = _domain.ParsedValue;
161-
var variables = _variables.HasValue()
162-
? _variables.Values
163-
.Where(item => item!.Contains('='))
164-
.Select(item => item!.Split('=', 2))
165-
.Where(parts => parts.Length == 2)
166-
.ToDictionary(parts => parts[0], parts => parts[1])
167-
: [];
168161

169162
// Read the Test Case definition
170163
var testCase = await TestCase.FromTestCaseFileAsync(file, cancellationToken);
171164
testCase
172165
.WithDomain(domain)
173-
.WithVariables(variables);
166+
.WithVariables(VariablesHelper.CreateVariables(_variables.Values));
174167

175168
ConsoleHelper.WriteLineYellow($"Running Test Case: {testCase.Id}");
176169

src/testr.Cli/Commands/ValidateCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ private async Task<int> ExecuteAsync(CancellationToken cancellationToken)
3636

3737
// 2. Read the Test Case definition
3838
var testCase = await TestCase.FromTestCaseFileAsync(file, cancellationToken);
39+
testCase.WithVariables(
40+
VariablesHelper.CreateDummyVariables(testCase.Steps)
41+
);
3942

4043
// 3. Validate the Test Case definition
4144
var testCaseValidator = new TestCaseValidator(testCase);
@@ -54,4 +57,4 @@ private async Task<int> ExecuteAsync(CancellationToken cancellationToken)
5457

5558
return await Task.FromResult(0);
5659
}
57-
}
60+
}

src/testr.Cli/Domain/TestCase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal class TestCase
1818
public string Domain => _domain;
1919
public bool HasDomain => !string.IsNullOrEmpty(_domain);
2020
public Dictionary<string, string> Variables { get; set; } = [];
21+
public bool HasVariables => Variables.Count > 0;
2122

2223
public TestCase WithDomain(string domain)
2324
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace tomware.TestR;
4+
5+
internal static class VariablesHelper
6+
{
7+
public static Dictionary<string, string> CreateVariables(
8+
IReadOnlyList<string?> variables)
9+
{
10+
if (variables == null || !variables.Any())
11+
{
12+
return [];
13+
}
14+
15+
return variables
16+
.Select(v => v.Split('='))
17+
.Where(parts => parts.Length == 2)
18+
.ToDictionary(parts => parts[0].Trim(), parts => parts[1].Trim());
19+
}
20+
21+
internal static Dictionary<string, string> CreateDummyVariables(IEnumerable<TestStep> steps)
22+
{
23+
// For each test step data that contains a variable starting with an
24+
// @ sign, create a dummy variable
25+
var variables = new Dictionary<string, string>();
26+
foreach (var step in steps)
27+
{
28+
if (string.IsNullOrWhiteSpace(step.TestData))
29+
{
30+
continue;
31+
}
32+
33+
var matches = Regex.Matches(step.TestData, @"@(\w+)");
34+
foreach (Match match in matches)
35+
{
36+
if (!variables.ContainsKey(match.Groups[1].Value))
37+
{
38+
variables.Add(match.Groups[1].Value, "dummy-value");
39+
}
40+
}
41+
}
42+
return variables;
43+
}
44+
}

src/testr.Tests/TestCaseValidatorTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace testr.Tests;
55
public class TestCaseValidatorTests
66
{
77
[Fact]
8-
public async Task Validate_With4TestSteps_ShouldReturnNoValidationErrors()
8+
public async Task Validate_With4TestStepsAndRealVariables_ShouldReturnNoValidationErrors()
99
{
1010
// Arrange
1111
var file = Path.Combine(Environment.CurrentDirectory, "TestData", "TC-001-Login.md");
@@ -26,6 +26,25 @@ public async Task Validate_With4TestSteps_ShouldReturnNoValidationErrors()
2626
Assert.True(result.IsValid);
2727
}
2828

29+
[Fact]
30+
public async Task Validate_With4TestStepsAndDummyVariables_ShouldReturnNoValidationErrors()
31+
{
32+
// Arrange
33+
var file = Path.Combine(Environment.CurrentDirectory, "TestData", "TC-001-Login.md");
34+
var testCase = await TestCase
35+
.FromTestCaseFileAsync(file, default);
36+
testCase.WithVariables(VariablesHelper.CreateDummyVariables(testCase.Steps));
37+
38+
var validator = new TestCaseValidator(testCase);
39+
40+
// Act
41+
var result = validator.Validate();
42+
43+
// Assert
44+
Assert.NotNull(result);
45+
Assert.True(result.IsValid);
46+
}
47+
2948
[Fact]
3049
public async Task Validate_WithNotAllowedType_ShouldReturnOneValidationError()
3150
{

0 commit comments

Comments
 (0)