Skip to content

Commit 73b2d9b

Browse files
Use generator
1 parent ba606d1 commit 73b2d9b

File tree

3 files changed

+324
-149
lines changed

3 files changed

+324
-149
lines changed
Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,49 @@
1-
using Xunit;
1+
{{ func to_timespan
2+
if $0 == "00:00:00"
3+
ret "TimeSpan.Zero"
4+
end
5+
6+
parts = string.split $0 ":"
7+
hours = parts[0] | string.to_int
8+
minutes = parts[1] | string.to_int
9+
seconds = parts[2] | string.to_int
10+
$"new TimeSpan({hours},{minutes},{seconds})"
11+
end }}
12+
13+
using Microsoft.Extensions.Time.Testing;
214

315
public class {{ testClass }}
416
{
517
{{- for test in tests }}
618
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
719
public void {{ test.testMethod }}()
820
{
9-
Assert.Equal({{ test.expected }}, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.commands }}));
21+
{{- for command in test.input.commands }}
22+
{{- if command.command == "new" }}
23+
var timeProvider = new FakeTimeProvider();
24+
var stopwatch = new {{ testedClass }}(timeProvider);
25+
{{- else if command.command == "state" }}
26+
Assert.Equal({{ command.expected | enum "StopwatchState" }}, stopwatch.State);
27+
{{- else if command.command == "currentLap" }}
28+
Assert.Equal({{ command.expected | to_timespan }}, stopwatch.CurrentLap);
29+
{{- else if command.command == "total" }}
30+
Assert.Equal({{ command.expected | to_timespan }}, stopwatch.Total);
31+
{{- else if command.command == "previousLaps" }}
32+
{{- if command.expected.empty? }}
33+
Assert.Empty(stopwatch.PreviousLaps);
34+
{{ else }}
35+
Assert.Equal([{{- for previousLap in command.expected }}{{ previousLap | to_timespan }}{{ if !for.last }}, {{ end }}{{ end -}}], stopwatch.PreviousLaps);
36+
{{ end -}}
37+
{{- else if command.command == "advanceTime" }}
38+
timeProvider.Advance({{ command.by | to_timespan }});
39+
{{- else }}
40+
{{- if command.expected && command.expected.error }}
41+
Assert.Throws<InvalidOperationException>(() => stopwatch.{{ command.command | pascalize }}());
42+
{{ else }}
43+
stopwatch.{{ command.command | pascalize }}();
44+
{{ end -}}
45+
{{ end -}}
46+
{{ end -}}
1047
}
1148
{{ end -}}
1249
}

exercises/practice/split-second-stopwatch/SplitSecondStopwatch.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
public enum StopwatchState
22
{
33
Ready,
4-
Running
4+
Running,
5+
Stopped
56
}
67

78
public class SplitSecondStopwatch(TimeProvider time)

0 commit comments

Comments
 (0)