Browse Source

Add some Test for Formatting results

pull/37/head
Peter Forstmeier 13 years ago
parent
commit
92e6511ec9
  1. 44
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/BaseClasses/Printing/StandardFormatter.cs
  2. 6
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/BaseClasses/TypeHelpers.cs
  3. 75
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/Expressions/SimpleExpressionEvaluator/Compilation/Functions/DateSubtract.cs
  4. 45
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/Expressions/SimpleExpressionEvaluator/Utilities/TypeNormalizer.cs
  5. 4
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/Globals/GlobalLists.cs
  6. 1
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Test/ICSharpCode.Reports.Core.Test/ICSharpCode.Reports.Core.Test.csproj
  7. 138
      src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Test/ICSharpCode.Reports.Core.Test/Printing/FormattingFixture.cs

44
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/BaseClasses/Printing/StandardFormatter.cs

@ -18,29 +18,27 @@ namespace ICSharpCode.Reports.Core.BaseClasses.Printing @@ -18,29 +18,27 @@ namespace ICSharpCode.Reports.Core.BaseClasses.Printing
internal static class StandardFormatter
{
public static string FormatOutput(string valueToFormat,string format,
string dataType, string nullValue )
{
if (String.IsNullOrEmpty(format)) {
return valueToFormat;
}
if (String.IsNullOrEmpty(valueToFormat)) {
return nullValue;
}
TypeCode typeCode = TypeHelpers.TypeCodeFromString(dataType);
return FormatItem(valueToFormat,format,typeCode,nullValue);
}
public static string FormatItem (string valueToFormat,string format,
private static string FormatItem (string valueToFormat,string format,
TypeCode typeCode,string nullValue)
{
string retValue = String.Empty;
if (String.IsNullOrEmpty(format)) {
retValue = valueToFormat;
return retValue;
}
if (String.IsNullOrEmpty(valueToFormat)) {
return nullValue;
}
switch (typeCode) {
case TypeCode.Int16:
case TypeCode.Int32:
@ -123,24 +121,30 @@ namespace ICSharpCode.Reports.Core.BaseClasses.Printing @@ -123,24 +121,30 @@ namespace ICSharpCode.Reports.Core.BaseClasses.Printing
return str;
}
// http://stackoverflow.com/questions/4710455/i-need-code-to-validate-any-time-in-c-sharp-in-hhmmss-format
private static string FormatDate(string toFormat, string format)
{
try {
DateTime date = DateTime.Parse (toFormat.Trim(),
CultureInfo.CurrentCulture.DateTimeFormat);
DateTime date;
if (DateTime.TryParse(toFormat, out date))
{
string str = date.ToString(format,
DateTimeFormatInfo.CurrentInfo);
return str.Trim();
} catch (System.FormatException ) {
// string s = String.Format("< {0} > {1}",toFormat,e.Message);
// System.Console.WriteLine("\t\tDateValue {0}",s);
}
return toFormat.Trim();
TimeSpan time;
bool valid = TimeSpan.TryParseExact(toFormat,
"g",
CultureInfo.CurrentCulture,
out time);
if (valid) {
return time.ToString("g");
}
return toFormat;
}
private static bool CheckValue (string toFormat)
{
if (String.IsNullOrEmpty(toFormat)) {

6
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/BaseClasses/TypeHelpers.cs

@ -67,9 +67,9 @@ namespace ICSharpCode.Reports.Core.Project.BaseClasses @@ -67,9 +67,9 @@ namespace ICSharpCode.Reports.Core.Project.BaseClasses
if (String.IsNullOrEmpty(type)) {
throw new ArgumentNullException("type");
}
var s = Type.GetTypeCode( Type.GetType(type));
Console.WriteLine ("typeCode for {0} - {1}",type,s);
// var x = Type.GetType(type,false,true);
// var s = Type.GetTypeCode( Type.GetType(type));
// Console.WriteLine ("typeCode for {0} - {1}",type,s);
return Type.GetTypeCode( Type.GetType(type));
}
}

75
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/Expressions/SimpleExpressionEvaluator/Compilation/Functions/DateSubtract.cs

@ -7,58 +7,39 @@ @@ -7,58 +7,39 @@
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Text.RegularExpressions;
using SimpleExpressionEvaluator.Utilities;
namespace SimpleExpressionEvaluator.Compilation.Functions
{
/// <summary>
/// DateSubtract (endDate,startDate)
/// </summary>
/// <summary>
/// DateSubtract (endDate,startDate)
/// </summary>
public class DateSubtract: Function<TimeSpan>
{
private static readonly Regex VALIDATOR = new Regex("[dDwWmMyY]", RegexOptions.Compiled);
protected override int ExpectedArgumentCount
{
get
{
return 2;
}
}
private static void ValidatePeriodCode(string code)
{
if (String.IsNullOrEmpty(code))
throw new Exception(
"The second argument in DateAdd was not provided. It must be one of the following (case insensitive): d,w,m,y");
if (code.Length != 1 || !VALIDATOR.IsMatch(code))
{
if (String.IsNullOrEmpty(code))
throw new Exception(
"The second argument in DateAdd was not provided. It must be one of the following (case insensitive): d,w,m,y");
}
}
protected override TimeSpan EvaluateFunction(object[] args)
{
args[0] = args[0] == null ? DateTime.Today : TypeNormalizer.EnsureType(args[0], typeof (DateTime));
args[1] = args[1] == null ? DateTime.Today : TypeNormalizer.EnsureType(args[1], typeof (DateTime));
var endDate = (DateTime) args[0];
var startDate = (DateTime) args[1];
if (startDate == DateTime.MinValue)
startDate = DateTime.Today;
Console.WriteLine(endDate.Subtract(startDate));
var s = endDate.Subtract(startDate);
return endDate.Subtract(startDate);
}
}
protected override int ExpectedArgumentCount
{
get{return 2;}
}
protected override TimeSpan EvaluateFunction(object[] args)
{
args[0] = args[0] == null ? DateTime.Today : TypeNormalizer.EnsureType(args[0], typeof (DateTime));
args[1] = args[1] == null ? DateTime.Today : TypeNormalizer.EnsureType(args[1], typeof (DateTime));
var endDate = (DateTime) args[0];
if (endDate == DateTime.MinValue) {
endDate = DateTime.Today;
}
var startDate = (DateTime) args[1];
if (startDate == DateTime.MinValue)
startDate = DateTime.Today;
return endDate.Subtract(startDate);
}
}
}

45
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/Expressions/SimpleExpressionEvaluator/Utilities/TypeNormalizer.cs

@ -107,38 +107,23 @@ namespace SimpleExpressionEvaluator.Utilities @@ -107,38 +107,23 @@ namespace SimpleExpressionEvaluator.Utilities
public static object EnsureType(object value,Type targetType,object nullValue)
{
if (value == null)
return nullValue;
if (targetType == typeof(object))
return value;
if (value == null)
return nullValue;
if (targetType == typeof(object))
return value;
if (value.GetType() == targetType)
return value;
if (value.GetType() == targetType)
return value;
/*
TypeConverter converter = TypeDescriptor.GetConverter(targetType);
if (converter != null && converter.CanConvertFrom(value.GetType()))
{
return converter.ConvertFrom(value);
}
try
{
return Convert.ChangeType(value, targetType);
}
catch e
{ }
*/
try {
return Convert.ChangeType(value, targetType);
} catch (Exception e) {
Console.WriteLine("TypeNormalizer {0} - {1}",value.ToString(),e.Message);
return value.ToString();
//throw new Exception(String.Format("TypeNormalizer for <{0}> - {1}",value.ToString(),e.Message));
}
// return nullValue;
try {
return Convert.ChangeType(value, targetType);
} catch (Exception e) {
Console.WriteLine("TypeNormalizer {0} - {1}",value.ToString(),e.Message);
return value.ToString();
//throw new Exception()String.Format("TypeNormalizer for <{0}> - {1}",value.ToString(),e.Message));
}
}
}
}

4
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Project/Globals/GlobalLists.cs

@ -156,7 +156,9 @@ namespace ICSharpCode.Reports.Core.Globals @@ -156,7 +156,9 @@ namespace ICSharpCode.Reports.Core.Globals
"Ddd, dd MMM yyyy HH\':\'mm\'\"ss \'GMT\'",
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm:ss GMT",
"HH:mm", "HH:mm:ss",
"HH:mm",
"HH:mm:ss",
"hh:mm:ss",
"yyyy-MM-dd HH:mm:ss", "html"};
#endregion

1
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Test/ICSharpCode.Reports.Core.Test/ICSharpCode.Reports.Core.Test.csproj

@ -81,6 +81,7 @@ @@ -81,6 +81,7 @@
<Compile Include="DataManager\TableStragy\TableDataManagerFixture.cs" />
<Compile Include="DataManager\TableStragy\TableStrategyFixture.cs" />
<Compile Include="Globals\ServiceContainerFixture.cs" />
<Compile Include="Printing\FormattingFixture.cs" />
<Compile Include="Printing\SectionBoundFixture.cs" />
<Compile Include="Printing\Shapes\BaseLineFixture.cs" />
<Compile Include="Printing\Shapes\EllipseShapeFixture.cs" />

138
src/AddIns/Misc/Reports/ICSharpCode.Reports.Core/Test/ICSharpCode.Reports.Core.Test/Printing/FormattingFixture.cs

@ -0,0 +1,138 @@ @@ -0,0 +1,138 @@
/*
* Created by SharpDevelop.
* User: Peter Forstmeier
* Date: 13.02.2013
* Time: 19:48
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Globalization;
using ICSharpCode.Reports.Core.BaseClasses.Printing;
using NUnit.Framework;
namespace ICSharpCode.Reports.Core.Test.Printing
{
[TestFixture]
public class FormattingFixture
{
private const string stringType = "System.String";
private const string dateTimetype = "System.DateTime";
private const string nullValue ="NullValue";
[Test]
public void String_Is_Not_Formatted()
{
string toFormat = "Hello World";
string format = "dd/MM/yy";
var result = StandardFormatter.FormatOutput(toFormat,format,stringType,nullValue);
Assert.That(result,Is.EqualTo(toFormat));
}
[Test]
public void Empty_Input_Returns_NullValue()
{
string toFormat = string.Empty;;
string format = "dd/MM/yy";
var result = StandardFormatter.FormatOutput(toFormat,format,stringType,nullValue);
Assert.That(result,Is.EqualTo(nullValue));
}
#region DateTime
[Test]
public void DateTime_dd_MM_YY ()
{
string toFormat = "2012/02/12";
string format = "dd.MM.yy";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
Assert.That(result,Is.EqualTo("12.02.12"));
}
[Test]
public void TypeDateTimeOfResultIsString()
{
string toFormat = "2012/02/12";
string format = "dd.MM.yy";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
Assert.That(result,Is.TypeOf(typeof(string)));
}
[Test]
public void ConvertResultToDateTime()
{
DateTime date;
string toFormat = "2012/02/12";
string format = "dd.MM.yy";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
bool valid = DateTime.TryParse(toFormat, out date);
Assert.That(valid,Is.True);
Assert.That(date,Is.EqualTo(new DateTime(2012,02,12)));
}
#endregion
#region TimeSpan
[Test]
public void TimeSpan_HH_mm_ss ()
{
string toFormat = "5:50:10";
string format = "HH:mm:ss";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
Assert.That(result,Is.EqualTo("05:50:10"));
}
[Test]
public void TimeSpan_H_mm_ss ()
{
string toFormat = "5:50:10";
string format = "H:mm:ss";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
Assert.That(result,Is.EqualTo("5:50:10"));
}
[Test]
public void TimeSpan_D_H_mm_ss ()
{
string toFormat = "1,5:50:10";
string format = "H:mm:ss";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
Assert.That(result,Is.EqualTo("1,5:50:10"));
}
[Test]
public void TypeOfTimeSpanResultIsString()
{
string toFormat = "5,50,10";
string format = "H:mm:ss";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
Assert.That(result,Is.TypeOf(typeof(string)));
}
[Test]
public void ConvertResultToTimeSpan()
{
TimeSpan time;
string toFormat = "5:50:10";
string format = "H:mm:ss";
var result = StandardFormatter.FormatOutput(toFormat,format,dateTimetype,nullValue);
bool valid = TimeSpan.TryParseExact(result,
"c",
CultureInfo.CurrentCulture,
out time);
Assert.That(valid,Is.True);
Assert.That(time,Is.EqualTo(new TimeSpan(5,50,10)));
}
#endregion
}
}
Loading…
Cancel
Save