Skip to content

Commit e370426

Browse files
committed
#32 - Year parser.
1 parent ea10b42 commit e370426

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ https://github.com/Apress/beg-sql-server-reporting-services/blob/master/Beginnin
302302
| Date & Time | Today | Complete | |
303303
| Date & Time | Weekday | Complete | |
304304
| Date & Time | WeekdayName | Complete | |
305-
| Date & Time | Year | Not started | |
305+
| Date & Time | Year | Complete | |
306306

307307
### Math
308308

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using ReportViewer.NET.DataObjects;
2+
using ReportViewer.NET.Extensions;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text.RegularExpressions;
6+
7+
namespace ReportViewer.NET.Parsers.DateAndTime
8+
{
9+
public class YearParser : BaseParser
10+
{
11+
public static Regex YearRegex = RegexCommon.GenerateParserRegex("Year");
12+
13+
public YearParser(
14+
string currentString,
15+
ExpressionFieldOperator op,
16+
ReportExpression currentExpression,
17+
IEnumerable<IDictionary<string, object>> dataSetResults,
18+
IDictionary<string, object> values,
19+
int currentRowNumber,
20+
IEnumerable<DataSet> dataSets,
21+
DataSet activeDataset,
22+
ReportRDL report
23+
) : base(currentString, op, currentExpression, dataSetResults, values, currentRowNumber, dataSets, activeDataset, YearRegex, report)
24+
{
25+
}
26+
27+
public override (Type, object) ExtractExpressionValue(string fieldName, string dataSetName)
28+
{
29+
throw new NotImplementedException();
30+
}
31+
32+
public override void Parse()
33+
{
34+
var match = YearRegex.Match(CurrentString);
35+
var matchValue = match.Value;
36+
37+
// Remove the surrounding Day including open & close brace so we can inspect inner members and see if they too contain program flow expressions.
38+
matchValue = matchValue.MatchValueSubString(5);
39+
40+
var dtt = this.Report.Parser.ParseReportExpressionString(
41+
matchValue,
42+
this.DataSetResults,
43+
this.Values,
44+
this.CurrentRowNumber,
45+
this.DataSets,
46+
this.ActiveDataset,
47+
null
48+
).ExpressionAsDateTime();
49+
50+
CurrentExpression.Index = match.Index;
51+
CurrentExpression.ResolvedType = typeof(int);
52+
CurrentExpression.Value = dtt.Year;
53+
}
54+
}
55+
}

src/ReportViewer.NET/Parsers/ExpressionFieldOperator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public enum ExpressionFieldOperator
7272
Today,
7373
Weekday,
7474
WeekdayName,
75+
Year,
7576
FormatCurrency,
7677
RowNumber
7778
}

src/ReportViewer.NET/Parsers/ExpressionParser.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,15 @@ ref string proposedString
659659
weekdayNameParser.Parse();
660660
proposedString = weekdayNameParser.GetProposedString();
661661
}
662+
663+
if (YearParser.YearRegex.IsMatch(currentString) &&
664+
(currentExpression.Operator == ExpressionFieldOperator.None || YearParser.YearRegex.Match(currentString).Index < currentExpression.Index)
665+
)
666+
{
667+
var yearParser = new YearParser(currentString, ExpressionFieldOperator.Year, currentExpression, dataSetResults, values, currentRowNumber, dataSets, activeDataset, _report);
668+
yearParser.Parse();
669+
proposedString = yearParser.GetProposedString();
670+
}
662671
}
663672

664673
private void SearchMathFunctions()
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using ReportViewer.NET.DataObjects;
3+
using ReportViewer.NET.Extensions;
4+
using ReportViewer.NET.Parsers;
5+
6+
namespace ReportViewer.NET.Tests.Parsers.DateAndTime
7+
{
8+
[TestClass]
9+
public class YearParserTests
10+
{
11+
private ExpressionParser _expressionParser;
12+
private ReportRDL _report;
13+
14+
public YearParserTests()
15+
{
16+
var report = TestHelper.PrimeReport();
17+
18+
_report = report;
19+
_expressionParser = new ExpressionParser(report);
20+
}
21+
22+
[TestMethod]
23+
public void Year_Returns_Integer()
24+
{
25+
// Arrange
26+
var expr = "=Year(\"January 15, 2010 14:15:30\")";
27+
28+
// Act
29+
var result = _expressionParser.ParseReportExpressionString(
30+
expr,
31+
null,
32+
null,
33+
1,
34+
_report.DataSets,
35+
null,
36+
null
37+
).ExpressionAsInt();
38+
39+
// Assert
40+
Assert.AreEqual(2010, result);
41+
}
42+
43+
[TestMethod]
44+
public void Day_Returns_Integer_Nested_DateAdd()
45+
{
46+
// Arrange
47+
var expr = "=Year(DateAdd(\"yyyy\", 3, \"January 15, 2010 14:15:30\"))";
48+
49+
// Act
50+
var result = _expressionParser.ParseReportExpressionString(
51+
expr,
52+
null,
53+
null,
54+
1,
55+
_report.DataSets,
56+
null,
57+
null
58+
).ExpressionAsInt();
59+
60+
// Assert
61+
Assert.AreEqual(2013, result);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)