Skip to content

Commit 9854835

Browse files
committed
Fixed to add catch block ; and Unit tests added
1 parent d294b38 commit 9854835

File tree

2 files changed

+81
-33
lines changed

2 files changed

+81
-33
lines changed

Cql/CoreTests/PrimitiveTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Hl7.Cql.Abstractions;
1010
using Hl7.Cql.CodeGeneration.NET.Toolkit;
1111
using Hl7.Cql.Compiler;
12+
using Hl7.Cql.Elm;
1213
using Hl7.Cql.Fhir;
1314
using Hl7.Cql.Iso8601;
1415
using Hl7.Cql.Operators;
@@ -3539,5 +3540,44 @@ public void QuantityToString()
35393540
var s = ops.ConvertQuantityToString(new CqlQuantity(125, "cm"));
35403541
s.Should().Be("125 'cm'");
35413542
}
3543+
3544+
[TestMethod]
3545+
public void Add_Date_Quantity()
3546+
{
3547+
var rc = GetNewContext();
3548+
var fcq = rc.Operators;
3549+
3550+
var inputDate = new CqlDate(9999, 12, 30);
3551+
var quantity = new CqlQuantity(1, "day");
3552+
CqlDate expectedDate = new CqlDate(9999, 12, 31);
3553+
3554+
var newDate = fcq.Add(inputDate, quantity);
3555+
Assert.IsNotNull(newDate);
3556+
Assert.AreEqual(expectedDate, newDate);
3557+
3558+
var inputDateMaxValue = CqlDate.MaxValue;
3559+
3560+
var newDateAddMax = fcq.Add(inputDateMaxValue, quantity);
3561+
Assert.IsNull(newDateAddMax);
3562+
}
3563+
3564+
[TestMethod]
3565+
public void Subtract_Date_Quantity()
3566+
{
3567+
var rc = GetNewContext();
3568+
var fcq = rc.Operators;
3569+
3570+
var inputDate = new CqlDate(1, 1, 2);
3571+
var quantity = new CqlQuantity(1, "day");
3572+
CqlDate expectedDate = new CqlDate(1, 1, 1);
3573+
3574+
var newDate = fcq.Subtract(inputDate, quantity);
3575+
Assert.IsNotNull(newDate);
3576+
Assert.AreEqual(expectedDate, newDate);
3577+
3578+
var inputDateMinValue = CqlDate.MinValue;
3579+
var newDateSubtractedMin = fcq.Subtract(inputDateMinValue, quantity);
3580+
Assert.IsNull(newDateSubtractedMin);
3581+
}
35423582
}
35433583
}

Cql/Cql.Abstractions/Primitives/CqlDate.cs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,42 +97,50 @@ public static bool TryParse(string s, out CqlDate? cqlDate)
9797
quantity = quantity.NormalizeTo(Precision);
9898
var value = quantity.value!.Value;
9999
var dto = Value.DateTimeOffset;
100-
switch (quantity.unit![0])
100+
try
101101
{
102-
case 'a':
103-
dto = dto.AddYears((int)value);
104-
break;
105-
case 'm':
106-
if (quantity.unit.Length > 1)
107-
{
108-
switch (quantity.unit[1])
102+
switch (quantity.unit![0])
103+
{
104+
case 'a':
105+
dto = dto.AddYears((int)value);
106+
break;
107+
case 'm':
108+
if (quantity.unit.Length > 1)
109109
{
110-
case 'o':
111-
dto = dto.AddMonths((int)value);
112-
break;
113-
case 'i':
114-
dto = dto.AddMinutes(Math.Truncate((double)value));
115-
break;
116-
case 's':
117-
dto = dto.AddMilliseconds(Math.Truncate((double)value));
118-
break;
119-
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
110+
switch (quantity.unit[1])
111+
{
112+
case 'o':
113+
dto = dto.AddMonths((int)value);
114+
break;
115+
case 'i':
116+
dto = dto.AddMinutes(Math.Truncate((double)value));
117+
break;
118+
case 's':
119+
dto = dto.AddMilliseconds(Math.Truncate((double)value));
120+
break;
121+
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
122+
}
120123
}
121-
}
122-
break;
123-
case 'd':
124-
dto = dto.AddDays((int)value!);
125-
break;
126-
case 'w':
127-
dto = dto.AddDays((int)(value! * CqlDateTimeMath.DaysPerWeek));
128-
break;
129-
case 'h':
130-
dto = dto.AddHours(Math.Truncate((double)value));
131-
break;
132-
case 's':
133-
dto = dto.AddSeconds(Math.Truncate((double)value));
134-
break;
135-
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
124+
break;
125+
case 'd':
126+
dto = dto.AddDays((int)value!);
127+
break;
128+
case 'w':
129+
dto = dto.AddDays((int)(value! * CqlDateTimeMath.DaysPerWeek));
130+
break;
131+
case 'h':
132+
dto = dto.AddHours(Math.Truncate((double)value));
133+
break;
134+
case 's':
135+
dto = dto.AddSeconds(Math.Truncate((double)value));
136+
break;
137+
default: throw new ArgumentException($"Unknown date unit {quantity.unit} supplied");
138+
}
139+
}
140+
catch (ArgumentOutOfRangeException)
141+
{
142+
// In cases where e.g. Predecessor is called on minimum Date.
143+
return null;
136144
}
137145

138146
var newIsoDate = new DateIso8601(dto, Value.Precision);

0 commit comments

Comments
 (0)