Skip to content

Commit 7b1e336

Browse files
committed
Test command: --only is more versatile. Minor test runner refactorings.
1 parent 6e84575 commit 7b1e336

File tree

7 files changed

+93
-51
lines changed

7 files changed

+93
-51
lines changed

UnitTests/TestCommandLine.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,32 @@ public void CreateWebHostCorrectly()
1515
var webhost = Program.CreateWebHost("examples\\example1", ".", null);
1616
Assert.NotNull(webhost);
1717
}
18+
19+
string[] NAMES = new[] { "Test one", "Test two", "Heisann" };
20+
21+
[Fact]
22+
public void OnlySingleNumber()
23+
{
24+
Assert.Equal(new[] { 5 }, Program.ParseOnlyArgument("5", NAMES));
25+
}
26+
27+
[Fact]
28+
public void OnlyListOfNumbers()
29+
{
30+
Assert.Equal(new[] { 5, 7, 11 }, Program.ParseOnlyArgument("5,7,11", NAMES));
31+
}
32+
33+
[Fact]
34+
public void OnlyMatchString()
35+
{
36+
Assert.Equal(new[] { 0, 1 }, Program.ParseOnlyArgument("test", NAMES));
37+
Assert.Equal(new[] { 2 }, Program.ParseOnlyArgument("sann", NAMES));
38+
}
39+
40+
[Fact]
41+
public void OnlyNoMatch()
42+
{
43+
Assert.Equal(0, Program.ParseOnlyArgument("foobar", NAMES).Length);
44+
}
1845
}
1946
}

UnitTests/TestCommandLineParser.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,6 @@ public void TestWithStopOption()
121121
Assert.True(result.Stop);
122122
}
123123

124-
[Fact]
125-
public void OnlyOptionMustBeNumeric()
126-
{
127-
AssertGivesException("Argument --only: integer required", new[] { "c:\\dir\\foo", "test", "--only", "a" });
128-
}
129-
130124
[Fact]
131125
public void DumpCommand()
132126
{

UnitTests/TestTestCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,12 @@ async public void CanCheckExpectedRequestMatcherSuccess()
293293
}
294294

295295
[Fact]
296-
async public void CanGetResultBody()
296+
public void CanGetResultBody()
297297
{
298298
var testcase =
299299
(new JSONTest { name = "checksomething", requestpath = "/foo/", requestbody = "this is a test", expectedrequestmatcher = "Regex 'test'" })
300300
.Validated().CreateTestCase(".");
301-
var result = await testcase.GetResponseAsync(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName));
301+
var result = testcase.GetResponse(EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName), null);
302302
Assert.Equal("FOOBARBOOBAR", result.Item1);
303303
Assert.Null(result.Item2);
304304
}

netmockery/CommandLineParser.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,6 @@ static public ParsedCommandLine ParseArguments(string[] args)
5757
{
5858
var value = args[++i];
5959
switchValues[arg] = value;
60-
61-
if (arg == VALUE_SWITCH_ONLY)
62-
{
63-
int dummy;
64-
if (! int.TryParse(value, out dummy))
65-
{
66-
throw new CommandLineParsingException($"Argument --only: integer required");
67-
}
68-
}
6960
seenSwitches.Add(arg);
7061
}
7162
else if (BOOL_SWITCHES.Contains(arg))

netmockery/NetmockeryTestCase.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public string GetWrittenResponseAsString()
6868
}
6969
}
7070

71+
72+
//TODO: Refactoring needed. Smell: RequestInfo is created in two different places (search for new RequestInfo)
7173
public class NetmockeryTestCase
7274
{
7375
public string Name;
@@ -241,14 +243,14 @@ async public Task<NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endp
241243
}
242244
}
243245

244-
async public Task<Tuple<string, string>> GetResponseAsync(EndpointCollection endpointCollection)
246+
public Tuple<string, string> GetResponse(EndpointCollection endpointCollection, DateTime? now)
245247
{
246248
var endpoint = endpointCollection.Resolve(RequestPath);
247249
if (endpoint == null)
248250
{
249251
return Tuple.Create((string)null, ERROR_NOMATCHING_ENDPOINT);
250252
}
251-
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody, null);
253+
var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody ?? "", null);
252254
if (matcher_and_creator != null)
253255
{
254256
var responseCreator = matcher_and_creator.ResponseCreator as SimpleResponseCreator;
@@ -257,14 +259,19 @@ async public Task<Tuple<string, string>> GetResponseAsync(EndpointCollection end
257259
return Tuple.Create((string)null, $"This response creator is not supported by test framework: {matcher_and_creator.ResponseCreator.ToString()}");
258260
}
259261

260-
var responseBody = responseCreator.GetBodyAndExecuteReplacements(new RequestInfo
262+
var requestInfo = new RequestInfo
261263
{
262264
EndpointDirectory = endpoint.Directory,
263265
Headers = null,
264266
QueryString = QueryString,
265267
RequestBody = RequestBody,
266268
RequestPath = RequestPath
267-
});
269+
};
270+
if (now != null)
271+
{
272+
requestInfo.SetStaticNow(now.Value);
273+
}
274+
var responseBody = responseCreator.GetBodyAndExecuteReplacements(requestInfo);
268275
return Tuple.Create(responseBody, (string)null);
269276
}
270277
else

netmockery/Program.cs

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Reflection;
1010
using System.Text;
11+
using System.Text.RegularExpressions;
1112
using System.Threading.Tasks;
1213
using static System.Console;
1314

@@ -120,40 +121,46 @@ public static void Test(ParsedCommandLine commandArgs, EndpointCollection endpoi
120121

121122
if (commandArgs.Only != null)
122123
{
123-
var index = int.Parse(commandArgs.Only);
124-
if (commandArgs.Diff)
124+
var indexes = ParseOnlyArgument(commandArgs.Only, (from testCase in testRunner.Tests select testCase.Name).ToArray());
125+
if (indexes.Length == 0)
125126
{
126-
var testCase = testRunner.Tests.ElementAt(index);
127-
if (testCase.ExpectedResponseBody == null)
127+
Error.WriteLine("ERROR: No testcases matches --only");
128+
}
129+
130+
foreach (var index in indexes)
131+
{
132+
if (commandArgs.Diff)
128133
{
129-
Error.WriteLine($"ERROR: Test case has no expected response body");
130-
return;
134+
var testCase = testRunner.Tests.ElementAt(index);
135+
if (testCase.ExpectedResponseBody == null)
136+
{
137+
Error.WriteLine($"ERROR: Test case has no expected response body");
138+
return;
139+
}
140+
141+
var responseTuple = testCase.GetResponse(endpointCollection, testRunner.Now);
142+
if (responseTuple.Item2 != null)
143+
{
144+
Error.WriteLine($"ERROR: {responseTuple.Item2}");
145+
return;
146+
}
147+
148+
var expectedFilename = Path.GetTempFileName();
149+
var actualFilename = Path.GetTempFileName();
150+
151+
File.WriteAllText(expectedFilename, testCase.ExpectedResponseBody);
152+
File.WriteAllText(actualFilename, responseTuple.Item1);
153+
154+
StartExternalDiffTool(expectedFilename, actualFilename);
131155
}
132-
133-
var responseTuple = testCase.GetResponseAsync(endpointCollection).Result;
134-
if (responseTuple.Item2 != null)
156+
if (commandArgs.ShowResponse)
135157
{
136-
Error.WriteLine($"ERROR: {responseTuple.Item2}");
137-
return;
158+
testRunner.ShowResponse(index);
159+
}
160+
else
161+
{
162+
testRunner.ExecuteTestAndOutputResult(index);
138163
}
139-
140-
var expectedFilename = Path.GetTempFileName();
141-
var actualFilename = Path.GetTempFileName();
142-
143-
File.WriteAllText(expectedFilename, testCase.ExpectedResponseBody);
144-
File.WriteAllText(actualFilename, responseTuple.Item1);
145-
146-
StartExternalDiffTool(expectedFilename, actualFilename);
147-
148-
return;
149-
}
150-
if (commandArgs.ShowResponse)
151-
{
152-
testRunner.ShowResponse(index);
153-
}
154-
else
155-
{
156-
testRunner.ExecuteTestAndOutputResult(index);
157164
}
158165
}
159166
else
@@ -162,6 +169,22 @@ public static void Test(ParsedCommandLine commandArgs, EndpointCollection endpoi
162169
}
163170
}
164171

172+
public static int[] ParseOnlyArgument(string only, string[] names)
173+
{
174+
if (Regex.IsMatch(only, @"^\d+$"))
175+
{
176+
return new[] { int.Parse(only) };
177+
}
178+
else if (Regex.IsMatch(only, @"^(\d+)(,\d+)+$"))
179+
{
180+
return (from strval in only.Split(',') select int.Parse(strval)).ToArray();
181+
}
182+
else
183+
{
184+
return (from i in Enumerable.Range(0, names.Length) where names[i].ToLower().Contains(only.ToLower()) select i).ToArray();
185+
}
186+
}
187+
165188
public static void StartExternalDiffTool(string expectedFilename, string actualFilename)
166189
{
167190
var difftool = Environment.GetEnvironmentVariable("DIFFTOOL");

netmockery/TestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public NetmockeryTestCaseResult ExecuteTestAndOutputResult(int index, Netmockery
115115
public void ShowResponse(int index)
116116
{
117117
var testCase = testcases.ElementAt(index);
118-
var response = testCase.GetResponseAsync(endpointCollection).Result;
118+
var response = testCase.GetResponse(endpointCollection, Now);
119119
if (response.Item2 != null)
120120
{
121121
WriteError(response.Item2);

0 commit comments

Comments
 (0)