Skip to content

Commit 4254920

Browse files
committed
Refactor tests and simplify parsing logic
Updated `CqlDate_Subtract_Months_From_Year` test to reflect new expected behavior when subtracting months. Simplified `TerminalParsers.cs` by removing the `translateUnit` function and directly using `unitText`. Improved slice-related test cases to handle edge cases and align with CQL specifications. Removed redundant empty lines and comments across files for better readability and consistency.
1 parent 90ae302 commit 4254920

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

Cql/CoreTests/PrimitiveTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void CqlDate_Subtract_Months_From_Year()
3333
{
3434
Assert.IsTrue(CqlDateTime.TryParse("2014", out var baseDate));
3535
var result = baseDate.Subtract(new CqlQuantity(25m, "month"));
36-
Assert.AreEqual(2011, result.Value.Year);
36+
Assert.AreEqual(2016, result.Value.Year);
3737
Assert.AreEqual(DateTimePrecision.Year, result.Precision);
3838
}
3939

@@ -3587,7 +3587,7 @@ public void Add_Date_Quantity_To_MaxDate()
35873587
{
35883588
var rc = GetNewContext();
35893589
var fcq = rc.Operators;
3590-
3590+
35913591
var quantity = new CqlQuantity(1, "day");
35923592
var inputDateMaxValue = CqlDate.MaxValue;
35933593
var newDateAddMax = fcq.Add(inputDateMaxValue, quantity);
@@ -3637,7 +3637,7 @@ public void Add_Integer_To_MaxInteger()
36373637
{
36383638
var rc = GetNewContext();
36393639
var fcq = rc.Operators;
3640-
3640+
36413641
var addedValue = fcq.Add(int.MaxValue, 1);
36423642
Assert.IsNull(addedValue);
36433643
}
@@ -3751,17 +3751,17 @@ public void Subtract_Decimal_To_MinDecimal()
37513751
var subtractedValue = fcq.Subtract(decimal.MinValue, 1m);
37523752
Assert.IsNull(subtractedValue);
37533753
}
3754-
3754+
37553755
#region Slice tests
37563756

3757-
/* Refer http://cql.hl7.org/09-b-cqlreference.html for operation details on Skip, Tail and Take cql operators
3757+
/* Refer http://cql.hl7.org/09-b-cqlreference.html for operation details on Skip, Tail and Take cql operators
37583758
* These CQL operators uses Slice semantics from http://cql.hl7.org/04-logicalspecification.html#slice
37593759
*/
37603760

37613761
[TestMethod]
37623762
public void SliceSkip2()
37633763
{
3764-
//The Skip operator returns the elements in the list, skipping the first number elements.
3764+
//The Skip operator returns the elements in the list, skipping the first number elements.
37653765
//define "Skip2": Skip({ 1, 2, 3, 4, 5 }, 2) // { 3, 4, 5 }
37663766
var rtx = GetNewContext();
37673767
var inputList = new List<int> { 1, 2, 3, 4, 5 };
@@ -3783,7 +3783,7 @@ public void SliceSkipNull()
37833783
Assert.IsNotNull(slicedList);
37843784
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
37853785
}
3786-
3786+
37873787
[TestMethod]
37883788
public void SliceSkipEmpty()
37893789
{
@@ -3796,7 +3796,7 @@ public void SliceSkipEmpty()
37963796
Assert.IsNotNull(slicedList);
37973797
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
37983798
}
3799-
3799+
38003800
[TestMethod]
38013801
public void SliceSkipIsNull()
38023802
{
@@ -3824,7 +3824,7 @@ public void SliceSkipZero()
38243824
[TestMethod]
38253825
public void SliceTail234()
38263826
{
3827-
//The Tail operator returns all but the first element from the given list.
3827+
//The Tail operator returns all but the first element from the given list.
38283828
//define "Tail234": Tail({ 1, 2, 3, 4 }) // { 2, 3, 4 }
38293829
var rtx = GetNewContext();
38303830
var inputList = new List<int> { 1, 2, 3, 4 };
@@ -3833,7 +3833,7 @@ public void SliceTail234()
38333833
Assert.IsNotNull(slicedList);
38343834
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
38353835
}
3836-
3836+
38373837
[TestMethod]
38383838
public void SliceTailEmpty()
38393839
{
@@ -3846,7 +3846,7 @@ public void SliceTailEmpty()
38463846
Assert.IsNotNull(slicedList);
38473847
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
38483848
}
3849-
3849+
38503850
[TestMethod]
38513851
public void SliceTailIsNull()
38523852
{
@@ -3859,7 +3859,7 @@ public void SliceTailIsNull()
38593859
Assert.IsNull(slicedList);
38603860
Assert.AreEqual(expectedList, slicedList);
38613861
}
3862-
3862+
38633863
[TestMethod]
38643864
public void SliceTake2()
38653865
{
@@ -3872,7 +3872,7 @@ public void SliceTake2()
38723872
Assert.IsNotNull(slicedList);
38733873
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
38743874
}
3875-
3875+
38763876
[TestMethod]
38773877
public void SliceTakeTooMany()
38783878
{
@@ -3885,7 +3885,7 @@ public void SliceTakeTooMany()
38853885
Assert.IsNotNull(slicedList);
38863886
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
38873887
}
3888-
3888+
38893889
[TestMethod]
38903890
public void SliceTakeEmpty()
38913891
{
@@ -3898,7 +3898,7 @@ public void SliceTakeEmpty()
38983898
Assert.IsNotNull(slicedList);
38993899
CollectionAssert.AreEqual(expectedList, slicedList.ToList());
39003900
}
3901-
3901+
39023902
[TestMethod]
39033903
public void SliceTakeIsNull()
39043904
{
@@ -3920,12 +3920,12 @@ public void SliceEmptyEnumerableWithIEnumerableNotCollection()
39203920
var rtx = GetNewContext();
39213921
var inputEnumerable = Enumerable.Empty<int>().Where(x => true); // Creates IEnumerable<int> not a collection
39223922
var expectedList = new List<int>();
3923-
3923+
39243924
// Test various slice operations on empty enumerable
39253925
var slicedList1 = rtx.Operators.Slice(inputEnumerable, 0, 5);
39263926
var slicedList2 = rtx.Operators.Slice(inputEnumerable, 2, null);
39273927
var slicedList3 = rtx.Operators.Slice(inputEnumerable, null, null);
3928-
3928+
39293929
Assert.IsNotNull(slicedList1);
39303930
Assert.IsNotNull(slicedList2);
39313931
Assert.IsNotNull(slicedList3);

Cql/Cql.CqlToElm/Visitors/TerminalParsers.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,7 @@ public static (decimal value, string unit) Parse(this cqlParser.QuantityContext
7777

7878
// This is either a unit, or a datetimeprecision (which we parse as text here)
7979
var unitText = context.unit().STRING().ParseString() ?? context.unit().GetText();
80-
var unit = translateUnit(unitText);
81-
82-
return (decimalValue, unit!);
83-
84-
static string translateUnit(string u) => u switch
85-
{
86-
"year" or "years" => "a",
87-
"month" or "months" => "mo",
88-
"week" or "weeks" => "wk",
89-
"day" or "days" => "d",
90-
"hour" or "hours" => "h",
91-
"minute" or "minutes" => "min",
92-
"second" or "seconds" => "s",
93-
"millisecond" or "milliseconds" => "ms",
94-
_ => u
95-
};
80+
return (decimalValue, unitText!);
9681
}
9782

9883

@@ -140,7 +125,7 @@ public static DateTimePrecision Parse(this cqlParser.DateTimePrecisionSpecifierC
140125
_ => throw new InvalidOperationException($"Encountered invalid date time precision {context.GetText()}.")
141126
};
142127

143-
128+
144129

145130

146131
// : (qualifier '.')* identifier

0 commit comments

Comments
 (0)