Skip to content

Commit

Permalink
Unit tests refactoring + typeof operator
Browse files Browse the repository at this point in the history
  • Loading branch information
davideicardi committed Jan 27, 2013
1 parent afc14f5 commit c3dceae
Show file tree
Hide file tree
Showing 14 changed files with 934 additions and 730 deletions.
2 changes: 2 additions & 0 deletions src/DynamicExpresso.Core/Parser/ErrorMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ internal class ErrorMessages
public const string InvalidRealLiteral = "Invalid real literal '{0}'";
public const string UnknownIdentifier = "Unknown identifier '{0}'";
public const string NoItInScope = "No 'it' is in scope";
public const string TypeofRequiresAType = "The 'typeof' keyword requires a type as an argument";
public const string TypeofRequiresOneArg = "The 'typeof' keyword requires 1 argument";
public const string IifRequiresThreeArgs = "The 'iif' function requires three arguments";
public const string FirstExprMustBeBool = "The first expression must be of type 'Boolean'";
public const string BothTypesConvertToOther = "Both of the types '{0}' and '{1}' convert to the other";
Expand Down
16 changes: 16 additions & 0 deletions src/DynamicExpresso.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ Expression ParseIdentifier()
// return ParseIt();
if (token.text == ParserConstants.keywordNew)
return ParseNew();
if (token.text == ParserConstants.keywordTypeof)
return ParseTypeof();

Type knownType;
if (_settings.KnownTypes.TryGetValue(token.text, out knownType))
Expand Down Expand Up @@ -655,6 +657,20 @@ Expression ParseIdentifier()
// return GenerateConditional(args[0], args[1], args[2], errorPos);
//}

Expression ParseTypeof()
{
int errorPos = token.pos;
NextToken();
Expression[] args = ParseArgumentList();
if (args.Length != 1)
throw ParseError(errorPos, ErrorMessages.TypeofRequiresOneArg);
var constExp = args[0] as ConstantExpression;
if (!(constExp.Type is Type))
throw ParseError(errorPos, ErrorMessages.TypeofRequiresAType);

return constExp;
}

Expression GenerateConditional(Expression test, Expression expr1, Expression expr2, int errorPos)
{
if (test.Type != typeof(bool))
Expand Down
1 change: 1 addition & 0 deletions src/DynamicExpresso.Core/Parser/ParserConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ internal static class ParserConstants

public const string keywordIt = "it";
public const string keywordNew = "new";
public const string keywordTypeof = "typeof";
}
}
55 changes: 55 additions & 0 deletions test/DynamicExpresso.UnitTest/ConstructorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DynamicExpresso.UnitTest
{
[TestClass]
public class ConstructorTest
{
[TestMethod]
public void New_Of_Base_Type()
{
var target = new Interpreter();

Assert.AreEqual(new DateTime(2015, 1, 24), target.Eval("new DateTime(2015, 1, 24)"));
Assert.AreEqual(new string('a', 10), target.Eval("new string('a', 10)"));
}

[TestMethod]
public void New_Of_Custom_Type()
{
var target = new Interpreter();

target.Using(typeof(Uri));

Assert.AreEqual(new Uri("http://www.google.com"), target.Eval("new Uri(\"http://www.google.com\")"));
}

[TestMethod]
public void New_And_Member_Access()
{
var target = new Interpreter();

Assert.AreEqual(new DateTime(2015, 1, 24).Month, target.Eval("new DateTime(2015, 1, 24).Month"));
Assert.AreEqual(new DateTime(2015, 1, 24).Month + 34, target.Eval("new DateTime( 2015, 1, 24).Month + 34"));
}

[TestMethod]
[ExpectedException(typeof(ParseException))]
public void Constructor_invocation_without_new_is_not_supported()
{
var target = new Interpreter();

target.Parse("DateTime(2010, 5, 23)");
}

[TestMethod]
[ExpectedException(typeof(ParseException))]
public void Unknown_New_Type_Is_Not_Supported()
{
var target = new Interpreter();

target.Parse("new unkkeyword()");
}
}
}
12 changes: 11 additions & 1 deletion test/DynamicExpresso.UnitTest/DynamicExpresso.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,14 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="ExpressionEngine_Test.cs" />
<Compile Include="TypesTest.cs" />
<Compile Include="LiteralsTest.cs" />
<Compile Include="OtherTests.cs" />
<Compile Include="ConstructorTest.cs" />
<Compile Include="OperatorsTest.cs" />
<Compile Include="StaticTest.cs" />
<Compile Include="ParametersTest.cs" />
<Compile Include="VariablesTest.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand All @@ -64,6 +71,9 @@
<Name>DynamicExpresso.Core</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="note.txt" />
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
Expand Down
Loading

0 comments on commit c3dceae

Please sign in to comment.