Skip to content

Commit 7c3195c

Browse files
committed
completed unit tests
1 parent 9d5d0a7 commit 7c3195c

6 files changed

Lines changed: 242 additions & 18 deletions

File tree

ShiftSharp.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ VisualStudioVersion = 15.0.27004.2005
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShiftSharp", "ShiftSharp\ShiftSharp.csproj", "{0D3FF948-F5BE-4338-94CB-CDC1A4475159}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestShiftSharp", "TestShiftSharp\TestShiftSharp.csproj", "{4EA986B7-7046-4501-B3EE-511FDC84D10E}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
1113
Release|Any CPU = Release|Any CPU
14+
Description = C# version of java Shift library
1215
EndGlobalSection
1316
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1417
{0D3FF948-F5BE-4338-94CB-CDC1A4475159}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1518
{0D3FF948-F5BE-4338-94CB-CDC1A4475159}.Debug|Any CPU.Build.0 = Debug|Any CPU
1619
{0D3FF948-F5BE-4338-94CB-CDC1A4475159}.Release|Any CPU.ActiveCfg = Release|Any CPU
1720
{0D3FF948-F5BE-4338-94CB-CDC1A4475159}.Release|Any CPU.Build.0 = Release|Any CPU
21+
{4EA986B7-7046-4501-B3EE-511FDC84D10E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{4EA986B7-7046-4501-B3EE-511FDC84D10E}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{4EA986B7-7046-4501-B3EE-511FDC84D10E}.Release|Any CPU.ActiveCfg = Release|Any CPU
24+
{4EA986B7-7046-4501-B3EE-511FDC84D10E}.Release|Any CPU.Build.0 = Release|Any CPU
1825
EndGlobalSection
1926
GlobalSection(SolutionProperties) = preSolution
2027
HideSolutionNode = FALSE

ShiftSharp/Shift.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private int ToRoundedSecond(LocalTime time)
114114
{
115115
int second = SecondOfDay(time);
116116

117-
if (time.NanosecondOfDay > 500E+06)
117+
if (time.NanosecondOfSecond > 500E+06)
118118
{
119119
second++;
120120
}
@@ -136,7 +136,6 @@ private int ToRoundedSecond(LocalTime time)
136136
*/
137137
public Duration CalculateWorkingTime(LocalTime from, LocalTime to)
138138
{
139-
140139
if (SpansMidnight())
141140
{
142141
String msg = String.Format(WorkSchedule.GetMessage("shift.spans.midnight"), GetName(), from, to);
@@ -186,7 +185,7 @@ public Duration CalculateWorkingTime(LocalTime from, LocalTime to, bool beforeMi
186185
int delta = toSecond - fromSecond;
187186

188187
// check for 24 hour shift
189-
if (delta == 0 && fromSecond == startSecond && GetDuration().Hours == 24)
188+
if (delta == 0 && fromSecond == startSecond && GetDuration().TotalHours == 24)
190189
{
191190
delta = 86400;
192191
}

TestShiftSharp/BaseTest.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ private void TestShifts(WorkSchedule ws)
9797
// 24 hour shift on midnight is a special case
9898
if (total.Equals(Duration.FromHours(24)))
9999
{
100-
Assert.IsTrue(worked.Hours == 24);
100+
Assert.IsTrue(worked.TotalHours == 24);
101101
}
102102
else
103103
{
104-
Assert.IsTrue(worked.Hours == 0);
104+
Assert.IsTrue(worked.TotalHours == 0);
105105
}
106106

107107
if (spansMidnight)
@@ -115,11 +115,11 @@ private void TestShifts(WorkSchedule ws)
115115

116116
if (total.Equals(Duration.FromHours(24)))
117117
{
118-
Assert.IsTrue(worked.Hours == 24);
118+
Assert.IsTrue(worked.TotalHours == 24);
119119
}
120120
else
121121
{
122-
Assert.IsTrue(worked.Hours == 0);
122+
Assert.IsTrue(worked.TotalHours == 0);
123123
}
124124

125125
try
@@ -312,6 +312,7 @@ private void TestDeletions()
312312

313313
foreach (NonWorkingPeriod period in periods)
314314
{
315+
schedule.DeleteNonWorkingPeriod(period);
315316
}
316317
Assert.IsTrue(schedule.GetNonWorkingPeriods().Count == 0);
317318
}

TestShiftSharp/TestShiftSharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@
5252
</ItemGroup>
5353
<ItemGroup>
5454
<Compile Include="BaseTest.cs" />
55+
<Compile Include="TestWorkSchedule.cs" />
5556
<Compile Include="Properties\AssemblyInfo.cs" />
5657
<Compile Include="TestSnapSchedule.cs" />
58+
<Compile Include="TestSnippet.cs" />
5759
</ItemGroup>
5860
<ItemGroup>
5961
<None Include="packages.config" />

TestShiftSharp/TestSnippet.cs

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,193 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626
using Microsoft.VisualStudio.TestTools.UnitTesting;
2727
using NodaTime;
2828
using Point85.ShiftSharp.Schedule;
29+
using System;
2930

3031
namespace TestShiftSharp
3132
{
3233
[TestClass]
33-
public class TestSnippet
34+
public class TestSnippet : BaseTest
3435
{
36+
[TestMethod]
37+
public void TestPartial()
38+
{
39+
schedule = new WorkSchedule("Working Time1", "Test working time");
40+
/*
41+
// shift does not cross midnight
42+
Duration shiftDuration = Duration.FromHours(8);
43+
LocalTime shiftStart = new LocalTime(7, 0, 0);
3544
45+
Shift shift = schedule.CreateShift("Work Shift1", "Working time shift", shiftStart, shiftDuration);
46+
LocalTime shiftEnd = shift.GetEnd();
47+
48+
// case #1
49+
Duration time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(2)));
50+
Assert.IsTrue(time.TotalSeconds == 0);
51+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(3)));
52+
Assert.IsTrue(time.TotalSeconds == 0);
53+
54+
// case #2
55+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftStart.PlusHours(1));
56+
Assert.IsTrue(time.TotalSeconds == 3600);
57+
58+
// case #3
59+
time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(2));
60+
Assert.IsTrue(time.TotalSeconds == 3600);
61+
62+
// case #4
63+
time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1));
64+
Assert.IsTrue(time.TotalSeconds == 3600);
65+
66+
// case #5
67+
time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(2));
68+
Assert.IsTrue(time.TotalSeconds == 0);
69+
time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(1));
70+
Assert.IsTrue(time.TotalSeconds == 0);
71+
72+
// case #6
73+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1));
74+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
75+
76+
// case #7
77+
time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(1));
78+
Assert.IsTrue(time.TotalSeconds == 0);
79+
80+
// case #8
81+
time = shift.CalculateWorkingTime(shiftStart, shiftEnd);
82+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
83+
84+
// case #9
85+
time = shift.CalculateWorkingTime(shiftStart, shiftStart);
86+
Assert.IsTrue(time.TotalSeconds == 0);
87+
88+
// case #10
89+
time = shift.CalculateWorkingTime(shiftEnd, shiftEnd);
90+
Assert.IsTrue(time.TotalSeconds == 0);
91+
92+
// case #11
93+
time = shift.CalculateWorkingTime(shiftStart, shiftStart.PlusSeconds(1));
94+
Assert.IsTrue(time.TotalSeconds == 1);
95+
96+
// case #12
97+
time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromSeconds(1)), shiftEnd);
98+
Assert.IsTrue(time.TotalSeconds == 1);
99+
100+
// 8 hr shift crossing midnight
101+
shiftStart = new LocalTime(22, 0, 0);
102+
103+
shift = schedule.CreateShift("Work Shift2", "Working time shift", shiftStart, shiftDuration);
104+
shiftEnd = shift.GetEnd();
105+
106+
// case #1
107+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(2)), true);
108+
Assert.IsTrue(time.TotalSeconds == 0);
109+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(3)), true);
110+
Assert.IsTrue(time.TotalSeconds == 0);
111+
112+
// case #2
113+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftStart.PlusHours(1), true);
114+
Assert.IsTrue(time.TotalSeconds == 3600);
115+
116+
// case #3
117+
time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(2), true);
118+
Assert.IsTrue(time.TotalSeconds == 3600);
119+
120+
// case #4
121+
time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), false);
122+
Assert.IsTrue(time.TotalSeconds == 3600);
123+
124+
// case #5
125+
time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(2), true);
126+
Assert.IsTrue(time.TotalSeconds == 0);
127+
time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(1), true);
128+
Assert.IsTrue(time.TotalSeconds == 0);
129+
130+
// case #6
131+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), true);
132+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
133+
134+
// case #7
135+
time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(1), true);
136+
Assert.IsTrue(time.TotalSeconds == 0);
137+
138+
// case #8
139+
time = shift.CalculateWorkingTime(shiftStart, shiftEnd, true);
140+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
141+
142+
// case #9
143+
time = shift.CalculateWorkingTime(shiftStart, shiftStart, true);
144+
Assert.IsTrue(time.TotalSeconds == 0);
145+
146+
// case #10
147+
time = shift.CalculateWorkingTime(shiftEnd, shiftEnd, true);
148+
Assert.IsTrue(time.TotalSeconds == 0);
149+
150+
// case #11
151+
time = shift.CalculateWorkingTime(shiftStart, shiftStart.PlusSeconds(1), true);
152+
Assert.IsTrue(time.TotalSeconds == 1);
153+
154+
// case #12
155+
time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromSeconds(1)), shiftEnd, false);
156+
Assert.IsTrue(time.TotalSeconds == 1);
157+
*/
158+
// 24 hr shift crossing midnight
159+
Duration shiftDuration = Duration.FromHours(24);
160+
LocalTime shiftStart = new LocalTime(7, 0, 0);
161+
162+
Shift shift = schedule.CreateShift("Work Shift3", "Working time shift", shiftStart, shiftDuration);
163+
LocalTime shiftEnd = shift.GetEnd();
164+
/*
165+
// case #1
166+
Duration time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(2)), false);
167+
Assert.IsTrue(time.TotalSeconds == 3600);
168+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(3)), shiftStart.Minus(Period.FromHours(3)), true);
169+
Assert.IsTrue(time.TotalSeconds == 0);
170+
171+
// case #2
172+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftStart.PlusHours(1), true);
173+
Assert.IsTrue(time.TotalSeconds == 3600);
174+
175+
// case #3
176+
time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(2), true);
177+
Assert.IsTrue(time.TotalSeconds == 3600);
178+
179+
// case #4
180+
time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), true);
181+
Assert.IsTrue(time.TotalSeconds == 3600);
182+
183+
// case #5
184+
time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(2), true);
185+
Assert.IsTrue(time.TotalSeconds == 3600);
186+
time = shift.CalculateWorkingTime(shiftEnd.PlusHours(1), shiftEnd.PlusHours(1), true);
187+
Assert.IsTrue(time.TotalSeconds == 0);
188+
189+
// case #6
190+
time = shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shiftEnd.PlusHours(1), true);
191+
Assert.IsTrue(time.TotalSeconds == 3600);
192+
193+
// case #7
194+
time = shift.CalculateWorkingTime(shiftStart.PlusHours(1), shiftStart.PlusHours(1), true);
195+
Assert.IsTrue(time.TotalSeconds == 0);
196+
*/
197+
// case #8
198+
Duration time = shift.CalculateWorkingTime(shiftStart, shiftEnd, true);
199+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
200+
201+
// case #9
202+
time = shift.CalculateWorkingTime(shiftStart, shiftStart, true);
203+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
204+
205+
// case #10
206+
time = shift.CalculateWorkingTime(shiftEnd, shiftEnd, true);
207+
Assert.IsTrue(time.TotalSeconds == shiftDuration.TotalSeconds);
208+
209+
// case #11
210+
time = shift.CalculateWorkingTime(shiftStart, shiftStart.PlusSeconds(1), true);
211+
Assert.IsTrue(time.TotalSeconds == 1);
212+
213+
// case #12
214+
time = shift.CalculateWorkingTime(shiftEnd.Minus(Period.FromSeconds(1)), shiftEnd, false);
215+
Assert.IsTrue(time.TotalSeconds == 1);
216+
}
36217
}
37218
}

0 commit comments

Comments
 (0)