Skip to content

Commit

Permalink
Merge pull request #15 from mgernand/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mgernand committed Nov 20, 2014
2 parents 9a36292 + 88e3b6c commit 035f936
Show file tree
Hide file tree
Showing 18 changed files with 590 additions and 225 deletions.
6 changes: 3 additions & 3 deletions Build/ExpressionReflect.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</metadata>
<files>
<!-- Add nuget package files -->
<file src="ExpressionReflect.dll" target="lib\portable-win+net45+MonoAndroid16+MonoMac40+MonoTouch40+sl40+wp71\ExpressionReflect.dll" />
<file src="ExpressionReflect.xml" target="lib\portable-win+net45+MonoAndroid16+MonoMac40+MonoTouch40+sl40+wp71\ExpressionReflect.xml" />
<file src="ExpressionReflect.pdb" target="lib\portable-win+net45+MonoAndroid16+MonoMac40+MonoTouch40+sl40+wp71\ExpressionReflect.pdb" />
<file src="ExpressionReflect.dll" target="lib\portable-net45+sl50+win+wpa81+wp80+MonoAndroid10+MonoTouch10\ExpressionReflect.dll" />
<file src="ExpressionReflect.xml" target="lib\portable-net45+sl50+win+wpa81+wp80+MonoAndroid10+MonoTouch10\ExpressionReflect.xml" />
<file src="ExpressionReflect.pdb" target="lib\portable-net45+sl50+win+wpa81+wp80+MonoAndroid10+MonoTouch10\ExpressionReflect.pdb" />
</files>
</package>
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ Why?
----

Provides the ability to "compile" expressions to delegates without using Reflection.Emit but only using reflection.
The created delegate will make use of reflection to execute the expression when it is invoked. This is much slower
The created delegate will make use of reflection to evaluate the expression when it is invoked. This is much slower
than executing a compiled delegate of an expression!

This framework is intended to be used where dynamic code creation is not possible. The main purpose is the useage with
This framework is intended to be used where dynamic code creation is not possible. The main purpose is the usage with
Xamarin.iOS due to it's restriction on Reflection.Emit.

How?
Expand Down Expand Up @@ -197,4 +197,7 @@ All this features can be combined to more complex expressions.
Contributors
------------

Bernhard Richter (seesharper)
Bernhard Richter (seesharper)


Thank you!
74 changes: 37 additions & 37 deletions Source/ExpressionReflect.Tests/BinaryOperatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,43 @@ public void ShouldExecuteOperator_TypeIs()
reflectionResult.Should().Be(emitResult);
}

[Test]
public void ShouldExecuteOperator_Equals()
{
// Arrange
Expression<Func<string, bool>> expression = s => s == "SomeValue";

// Act
Func<string, bool> emit = expression.Compile();
Func<string, bool> reflection = expression.Reflect();

bool emitResult = emit.Invoke("SomeValue");
bool reflectionResult = reflection.Invoke("SomeValue");

// Assert
emitResult.Should().BeTrue();
reflectionResult.Should().BeTrue();
reflectionResult.Should().Be(emitResult);
}

[Test]
public void ShouldExecuteOperator_EqualsWithNullValue()
{
// Arrange
Expression<Func<string, bool>> expression = s => s == null;

// Act
Func<string, bool> emit = expression.Compile();
Func<string, bool> reflection = expression.Reflect();

bool emitResult = emit.Invoke(null);
bool reflectionResult = reflection.Invoke(null);

// Assert
emitResult.Should().BeTrue();
reflectionResult.Should().BeTrue();
reflectionResult.Should().Be(emitResult);
}
[Test]
public void ShouldExecuteOperator_Equals()
{
// Arrange
Expression<Func<string, bool>> expression = s => s == "SomeValue";

// Act
Func<string, bool> emit = expression.Compile();
Func<string, bool> reflection = expression.Reflect();

bool emitResult = emit.Invoke("SomeValue");
bool reflectionResult = reflection.Invoke("SomeValue");

// Assert
emitResult.Should().BeTrue();
reflectionResult.Should().BeTrue();
reflectionResult.Should().Be(emitResult);
}

[Test]
public void ShouldExecuteOperator_EqualsWithNullValue()
{
// Arrange
Expression<Func<string, bool>> expression = s => s == null;

// Act
Func<string, bool> emit = expression.Compile();
Func<string, bool> reflection = expression.Reflect();

bool emitResult = emit.Invoke(null);
bool reflectionResult = reflection.Invoke(null);

// Assert
emitResult.Should().BeTrue();
reflectionResult.Should().BeTrue();
reflectionResult.Should().Be(emitResult);
}

}
}
Expand Down
82 changes: 51 additions & 31 deletions Source/ExpressionReflect.Tests/EvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
namespace ExpressionReflect.Tests
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ExpressionReflect.Tests.Model;
using FluentAssertions;
Expand Down Expand Up @@ -35,10 +38,10 @@ public void ShouldPreEvaluate_LocalVariable()
{
// Arrange
string str = "test";
Expression<Func<Customer, bool>> expresion = x => x.Firstname == str;
Expression<Func<Customer, bool>> expression = x => x.Firstname == str;

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -50,10 +53,10 @@ public void ShouldPreEvaluate_LocalFuncWithoutParameter()
{
// Arrange
Func<string> func = () => "test";
Expression<Func<Customer, bool>> expresion = x => x.Firstname == func();
Expression<Func<Customer, bool>> expression = x => x.Firstname == func();

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -65,10 +68,10 @@ public void ShouldPreEvaluate_LocalFuncWithConstantParameter()
{
// Arrange
Func<int, string> func = x => "test" + x;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == func(5);
Expression<Func<Customer, bool>> expression = x => x.Firstname == func(5);

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -81,10 +84,10 @@ public void ShouldPreEvaluate_LocalFuncWithLocalVariableParameter()
// Arrange
Func<int, string> func = x => "test" + x;
int i = 5;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == func(i);
Expression<Func<Customer, bool>> expression = x => x.Firstname == func(i);

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -97,10 +100,10 @@ public void ShouldPreEvaluate_LocalFuncWithLocalFuncParameter()
// Arrange
Func<int, string> func = x => "test" + x;
Func<int> i = () => 5;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == func(i());
Expression<Func<Customer, bool>> expression = x => x.Firstname == func(i());

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -111,10 +114,10 @@ public void ShouldPreEvaluate_LocalFuncWithLocalFuncParameter()
public void ShouldPreEvaluate_InstanceMethodCallWithoutParameter()
{
// Arrange
Expression<Func<Customer, bool>> expresion = x => x.Firstname == this.GetString();
Expression<Func<Customer, bool>> expression = x => x.Firstname == this.GetString();

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -125,10 +128,10 @@ public void ShouldPreEvaluate_InstanceMethodCallWithoutParameter()
public void ShouldPreEvaluate_StaticMethodCallWithoutParameter()
{
// Arrange
Expression<Func<Customer, bool>> expresion = x => x.Firstname == GetStringStatic();
Expression<Func<Customer, bool>> expression = x => x.Firstname == GetStringStatic();

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -139,10 +142,10 @@ public void ShouldPreEvaluate_StaticMethodCallWithoutParameter()
public void ShouldPreEvaluate_InstanceMethodCallWithConstantParameter()
{
// Arrange
Expression<Func<Customer, bool>> expresion = x => x.Firstname == this.GetString(5);
Expression<Func<Customer, bool>> expression = x => x.Firstname == this.GetString(5);

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -153,10 +156,10 @@ public void ShouldPreEvaluate_InstanceMethodCallWithConstantParameter()
public void ShouldPreEvaluate_StaticMethodCallWithConstantParameter()
{
// Arrange
Expression<Func<Customer, bool>> expresion = x => x.Firstname == GetStringStatic(5);
Expression<Func<Customer, bool>> expression = x => x.Firstname == GetStringStatic(5);

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -168,10 +171,10 @@ public void ShouldPreEvaluate_InstanceMethodCallWithLocalVariableParameter()
{
// Arrange
int i = 5;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == this.GetString(i);
Expression<Func<Customer, bool>> expression = x => x.Firstname == this.GetString(i);

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -183,10 +186,10 @@ public void ShouldPreEvaluate_StaticMethodCallWithLocalVariableParameter()
{
// Arrange
int i = 5;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == GetStringStatic(i);
Expression<Func<Customer, bool>> expression = x => x.Firstname == GetStringStatic(i);

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -198,10 +201,10 @@ public void ShouldPreEvaluate_InstanceMethodCallWithLocalFuncParameter()
{
// Arrange
Func<int> i = () => 5;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == this.GetString(i());
Expression<Func<Customer, bool>> expression = x => x.Firstname == this.GetString(i());

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
Expand All @@ -213,29 +216,46 @@ public void ShouldPreEvaluate_StaticMethodCallWithLocalFuncParameter()
{
// Arrange
Func<int> i = () => 5;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == GetStringStatic(i());
Expression<Func<Customer, bool>> expression = x => x.Firstname == GetStringStatic(i());

// Act
string expressionString = expresion.PartialEval().ToString();
string expressionString = expression.PartialEval().ToString();
Console.WriteLine(expressionString);

// Assert
expressionString.Should().Be(@"x => (x.Firstname == ""test5"")");
}

//[Test]
//public void ShouldPreEvaluate_LocalFuncWithLambdaParameterParameter()
//{
// // Arrange
// Func<string, string> func = x => x;
// Expression<Func<Customer, bool>> expression = x => x.Firstname == func(x.Lastname);

// // Act
// string expressionString = expression.PartialEval().ToString();
// Console.WriteLine(expressionString);

// // Assert
// expressionString.Should().Be(@"x => (x.Firstname == func(x.Lastname))");
//}

[Test]
public void ShouldPreEvaluate_LocalFuncWithLambdaParameterParameter()
public void ShouldPreEvaluate_LocalCollectionContains()
{
// Arrange
Func<string, string> func = x => x;
Expression<Func<Customer, bool>> expresion = x => x.Firstname == func(x.Lastname);
var ids = new List<int> { 4, 5, 7 };
Expression<Func<Customer, bool>> expression = x => ids.Contains(x.Age);

// Act
string expressionString = expresion.PartialEval().ToString();
Expression exp = expression.PartialEval();
exp = exp.ExpandCollection();
string expressionString = exp.ToString();
Console.WriteLine(expressionString);

// Assert
expressionString.Should().Be(@"x => (x.Firstname == func(x.Lastname))");
expressionString.Should().Be(@"x => {4|5|7}.Contains(x.Age)");
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions Source/ExpressionReflect.Tests/ExpressionReflect.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ExpressionReflect.Tests</RootNamespace>
<AssemblyName>ExpressionReflect.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -32,19 +33,22 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="FluentAssertions">
<HintPath>..\packages\FluentAssertions.2.0.1\lib\net45\FluentAssertions.dll</HintPath>
</Reference>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="FluentAssertions.Core">
<HintPath>..\packages\FluentAssertions.3.2.1\lib\net45\FluentAssertions.Core.dll</HintPath>
</Reference>
<Reference Include="FluentAssertions">
<HintPath>..\packages\FluentAssertions.3.2.1\lib\net45\FluentAssertions.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ActionExecutionTests.cs" />
Expand All @@ -67,7 +71,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ExpressionReflect\ExpressionReflect.csproj">
<Project>{320289ae-b78b-4c5a-b52e-08f81ae7f2ed}</Project>
<Project>{320289AE-B78B-4C5A-B52E-08F81AE7F2ED}</Project>
<Name>ExpressionReflect</Name>
</ProjectReference>
</ItemGroup>
Expand Down
Loading

0 comments on commit 035f936

Please sign in to comment.