Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit f711340

Browse files
committed
Initial commit
1 parent b3dcc92 commit f711340

File tree

12 files changed

+350
-0
lines changed

12 files changed

+350
-0
lines changed

IClock.Tests/CustomClockTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
4+
namespace IClock.Tests
5+
{
6+
[TestClass]
7+
public class CustomClockTests
8+
{
9+
[TestMethod]
10+
[ExpectedException(typeof(ArgumentNullException))]
11+
public void CustomClock_Throws_OnNullArgument() => new CustomClock(null);
12+
13+
[TestMethod]
14+
public void CustomClock_GetTime_Returns_CorrectValue1()
15+
{
16+
var tc = new TestClock();
17+
var target = new CustomClock(tc.GetTime);
18+
Assert.AreEqual(TestClock.DefaultTime, target.GetTime());
19+
}
20+
21+
[TestMethod]
22+
public void CustomClock_GetTime_Returns_CorrectValue2()
23+
{
24+
var time = new DateTime(2020, 1, 2, 3, 4, 5, 6, DateTimeKind.Local);
25+
var target = new CustomClock(() => time);
26+
Assert.AreEqual(time, target.GetTime());
27+
}
28+
29+
}
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
4+
namespace IClock.Tests
5+
{
6+
[TestClass]
7+
public class ForwardOnlyClockTests
8+
{
9+
[TestMethod]
10+
[ExpectedException(typeof(ArgumentNullException))]
11+
public void ForwardOnlyClock_Throws_OnNullArgument() => new ForwardOnlyClock(null);
12+
13+
[TestMethod]
14+
public void ForwardOnlyClock_GetTime_Returns_ForwardTime()
15+
{
16+
var tc = new TestClock();
17+
var target = new ForwardOnlyClock(tc);
18+
19+
var t1 = target.GetTime();
20+
tc.Add(TimeSpan.FromSeconds(-1)); // Set clock back 1 second
21+
var t2 = target.GetTime();
22+
Assert.AreEqual(t1, t2); // Time should be same
23+
24+
tc.Add(TimeSpan.FromSeconds(3)); // Add 2 seconds
25+
var t3 = target.GetTime();
26+
Assert.AreEqual(t1.AddSeconds(2), t3); // Time should be at +2 seconds now.
27+
}
28+
29+
}
30+
}

IClock.Tests/IClock.Tests.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
12+
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
13+
<PackageReference Include="coverlet.collector" Version="1.2.0" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\IClock\IClock.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

IClock.Tests/TestClockTests.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
4+
namespace IClock.Tests
5+
{
6+
[TestClass]
7+
public class TestClockTests
8+
{
9+
[TestMethod]
10+
public void TestClock_GetTime_Returns_DefaultTime()
11+
{
12+
var target = new TestClock();
13+
Assert.AreEqual(TestClock.DefaultTime, target.GetTime());
14+
}
15+
16+
[TestMethod]
17+
public void TestClock_GetTime_Returns_GivenTime()
18+
{
19+
var time = new DateTime(2020, 1, 2, 3, 4, 5, 6, DateTimeKind.Local);
20+
var target = new TestClock(time);
21+
Assert.AreEqual(time, target.GetTime());
22+
}
23+
24+
[TestMethod]
25+
public void TestClock_Tick_DefaultIncrement_IsOneSecond()
26+
{
27+
var target = new TestClock();
28+
var t1 = target.GetTime();
29+
target.Tick();
30+
var t2 = target.GetTime();
31+
32+
Assert.AreEqual(TimeSpan.FromSeconds(1), t2 - t1);
33+
}
34+
35+
[TestMethod]
36+
public void TestClock_Tick_Uses_GivenIncrement()
37+
{
38+
var ts = TimeSpan.FromDays(Math.PI);
39+
var target = new TestClock(TestClock.DefaultTime, ts);
40+
var t1 = target.GetTime();
41+
target.Tick();
42+
var t2 = target.GetTime();
43+
44+
Assert.AreEqual(ts, t2 - t1);
45+
}
46+
47+
[TestMethod]
48+
public void TestClock_Set_ChangesTime()
49+
{
50+
var time = new DateTime(2020, 1, 2, 3, 4, 5, 6, DateTimeKind.Local);
51+
var target = new TestClock();
52+
target.Set(time);
53+
54+
Assert.AreEqual(time, target.GetTime());
55+
}
56+
57+
[TestMethod]
58+
public void TestClock_Add_AddsTime()
59+
{
60+
var target = new TestClock();
61+
var timespan = TimeSpan.FromSeconds(Math.PI);
62+
var t1 = target.GetTime();
63+
target.Add(timespan);
64+
var t2 = target.GetTime();
65+
66+
Assert.AreEqual(timespan, t2 - t1);
67+
}
68+
69+
private DateTimeOffset GetTime1() => TestClock.GetDeterministicRandomTime();
70+
private DateTimeOffset GetTime2() => TestClock.GetDeterministicRandomTime();
71+
72+
[TestMethod]
73+
public void TestClock_GetDeterministicRandomTime_IsDeterministic()
74+
{
75+
// Each invocation within the same method should return the same value
76+
Assert.AreEqual(TestClock.GetDeterministicRandomTime(), TestClock.GetDeterministicRandomTime());
77+
78+
// Each invocation from different methods should (likely) return the different values
79+
Assert.AreNotEqual(GetTime1(), GetTime2());
80+
Assert.AreNotEqual(TestClock.GetDeterministicRandomTime(TimeSpan.Zero, "A"), TestClock.GetDeterministicRandomTime(TimeSpan.Zero, "B"));
81+
}
82+
83+
[TestMethod]
84+
public void TestClock_TestVector() // Do not change method name or fix expected value when you do
85+
{
86+
// Pre-calculated test vector
87+
var expected = new DateTimeOffset(635894622187178864L, TimeSpan.Zero);
88+
89+
// Make sure GetRandomTime() returns a deterministic "random" time
90+
Assert.AreEqual(expected, TestClock.GetDeterministicRandomTime());
91+
// Make sure a second invocation doesn't change the returned time
92+
Assert.AreEqual(expected, TestClock.GetDeterministicRandomTime());
93+
}
94+
95+
[TestMethod]
96+
public void TestClock_GetDeterministicRandomTime_UsesGivenOffset()
97+
{
98+
var offset = TimeSpan.FromMinutes(123);
99+
Assert.AreEqual(offset, TestClock.GetDeterministicRandomTime(offset).Offset);
100+
}
101+
102+
}
103+
}

IClock.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30413.136
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IClock", "IClock\IClock.csproj", "{76E771C0-D150-4A4F-9734-33F9E06D17D9}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IClock.Tests", "IClock.Tests\IClock.Tests.csproj", "{4D016A5A-EF63-4FBF-A661-87E7BF7E0098}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{76E771C0-D150-4A4F-9734-33F9E06D17D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{76E771C0-D150-4A4F-9734-33F9E06D17D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{76E771C0-D150-4A4F-9734-33F9E06D17D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{76E771C0-D150-4A4F-9734-33F9E06D17D9}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{4D016A5A-EF63-4FBF-A661-87E7BF7E0098}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{4D016A5A-EF63-4FBF-A661-87E7BF7E0098}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{4D016A5A-EF63-4FBF-A661-87E7BF7E0098}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{4D016A5A-EF63-4FBF-A661-87E7BF7E0098}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {C0707B68-2C97-4742-8B11-A4DBC094A721}
30+
EndGlobalSection
31+
EndGlobal

IClock/CustomClock.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace IClock
4+
{
5+
public class CustomClock : IClock
6+
{
7+
private readonly Func<DateTimeOffset> _gettimefunc;
8+
public CustomClock(Func<DateTimeOffset> getTimeFunction)
9+
{
10+
_gettimefunc = getTimeFunction ?? throw new ArgumentNullException(nameof(getTimeFunction));
11+
}
12+
13+
public DateTimeOffset GetTime() => _gettimefunc();
14+
}
15+
}

IClock/ForwardOnlyClock.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
3+
namespace IClock
4+
{
5+
public class ForwardOnlyClock : IClock
6+
{
7+
private IClock _internalclock;
8+
private DateTimeOffset _lasttime;
9+
private object _lock = new object();
10+
11+
public ForwardOnlyClock(IClock clock)
12+
{
13+
_internalclock = clock ?? throw new ArgumentNullException(nameof(clock));
14+
_lasttime = clock.GetTime();
15+
}
16+
17+
public DateTimeOffset GetTime()
18+
{
19+
var current = _internalclock.GetTime();
20+
lock (_lock)
21+
{
22+
if (current.CompareTo(_lasttime) > 0)
23+
_lasttime = current;
24+
return _lasttime;
25+
}
26+
}
27+
}
28+
}

IClock/IClock.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace IClock
4+
{
5+
public interface IClock
6+
{
7+
DateTimeOffset GetTime();
8+
}
9+
}

IClock/IClock.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard1.0</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<Authors>RobIII</Authors>
7+
<Company>Devcorner.nl</Company>
8+
<Copyright>Copyright © 2020 Devcorner.nl</Copyright>
9+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
10+
</PropertyGroup>
11+
12+
</Project>

IClock/LocalClock.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace IClock
4+
{
5+
public class LocalClock : IClock
6+
{
7+
public DateTimeOffset GetTime() => DateTimeOffset.Now;
8+
}
9+
}

0 commit comments

Comments
 (0)