Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using System.Reflection;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class BinaryExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Add;
Type expectedType = typeof(DateTime);
MethodInfo expectedMethod = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });
// Act
BinaryExpressionFingerprint fingerprint = new BinaryExpressionFingerprint(expectedNodeType, expectedType, expectedMethod);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedMethod, fingerprint.Method);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Add;
Type type = typeof(DateTime);
MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });
// Act
BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, type, method);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Add;
Type type = typeof(DateTime);
MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });
// Act
BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Method()
{
// Arrange
ExpressionType nodeType = ExpressionType.Add;
Type type = typeof(DateTime);
MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });
// Act
BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, type, null /* method */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Add;
Type type = typeof(DateTime);
MethodInfo method = typeof(DateTime).GetMethod("op_Addition", new Type[] { typeof(DateTime), typeof(TimeSpan) });
// Act
BinaryExpressionFingerprint fingerprint1 = new BinaryExpressionFingerprint(nodeType, type, method);
BinaryExpressionFingerprint fingerprint2 = new BinaryExpressionFingerprint(nodeType, typeof(object), method);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,143 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class CachedExpressionCompilerTest
{
private delegate Func<TIn, TOut> Compiler<TIn, TOut>(Expression<Func<TIn, TOut>> expr);
[Fact]
public void Compiler_CompileFromConstLookup()
{
// Arrange
Expression<Func<string, int>> expr = model => 42;
var compiler = GetCompilerMethod<string, int>("CompileFromConstLookup");
// Act
var func = compiler(expr);
int result = func("any model");
// Assert
Assert.Equal(42, result);
}
[Fact]
public void Compiler_CompileFromFingerprint()
{
// Arrange
Expression<Func<string, int>> expr = s => 20 * s.Length;
var compiler = GetCompilerMethod<string, int>("CompileFromFingerprint");
// Act
var func = compiler(expr);
int result = func("hello");
// Assert
Assert.Equal(100, result);
}
[Fact]
public void Compiler_CompileFromIdentityFunc()
{
// Arrange
Expression<Func<string, string>> expr = model => model;
var compiler = GetCompilerMethod<string, string>("CompileFromIdentityFunc");
// Act
var func = compiler(expr);
string result = func("hello");
// Assert
Assert.Equal("hello", result);
}
[Fact]
public void Compiler_CompileFromMemberAccess_CapturedLocal()
{
// Arrange
string capturedLocal = "goodbye";
Expression<Func<string, string>> expr = _ => capturedLocal;
var compiler = GetCompilerMethod<string, string>("CompileFromMemberAccess");
// Act
var func = compiler(expr);
string result = func("hello");
// Assert
Assert.Equal("goodbye", result);
}
[Fact]
public void Compiler_CompileFromMemberAccess_ParameterInstanceMember()
{
// Arrange
Expression<Func<string, int>> expr = s => s.Length;
var compiler = GetCompilerMethod<string, int>("CompileFromMemberAccess");
// Act
var func = compiler(expr);
int result = func("hello");
// Assert
Assert.Equal(5, result);
}
[Fact]
public void Compiler_CompileFromMemberAccess_StaticMember()
{
// Arrange
Expression<Func<string, string>> expr = _ => String.Empty;
var compiler = GetCompilerMethod<string, string>("CompileFromMemberAccess");
// Act
var func = compiler(expr);
string result = func("hello");
// Assert
Assert.Equal("", result);
}
[Fact]
public void Compiler_CompileSlow()
{
// Arrange
Expression<Func<string, string>> expr = s => new StringBuilder(s).ToString();
var compiler = GetCompilerMethod<string, string>("CompileSlow");
// Act
var func = compiler(expr);
string result = func("hello");
// Assert
Assert.Equal("hello", result);
}
[Fact]
public void Process()
{
// Arrange
Expression<Func<string, string>> expr = s => new StringBuilder(s).ToString();
// Act
var func = CachedExpressionCompiler.Process(expr);
string result = func("hello");
// Assert
Assert.Equal("hello", result);
}
// helper to create a delegate to a private method on the compiler
private static Compiler<TIn, TOut> GetCompilerMethod<TIn, TOut>(string methodName)
{
Type openCompilerType = typeof(CachedExpressionCompiler).GetNestedType("Compiler`2", BindingFlags.NonPublic);
Type closedCompilerType = openCompilerType.MakeGenericType(typeof(TIn), typeof(TOut));
MethodInfo targetMethod = closedCompilerType.GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic);
return (Compiler<TIn, TOut>)Delegate.CreateDelegate(typeof(Compiler<TIn, TOut>), targetMethod);
}
}
}

View File

@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class ConditionalExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Conditional;
Type expectedType = typeof(object);
// Act
ConditionalExpressionFingerprint fingerprint = new ConditionalExpressionFingerprint(expectedNodeType, expectedType);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Conditional;
Type type = typeof(object);
// Act
ConditionalExpressionFingerprint fingerprint1 = new ConditionalExpressionFingerprint(nodeType, type);
ConditionalExpressionFingerprint fingerprint2 = new ConditionalExpressionFingerprint(nodeType, type);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Conditional;
Type type = typeof(object);
// Act
ConditionalExpressionFingerprint fingerprint1 = new ConditionalExpressionFingerprint(nodeType, type);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Conditional;
Type type = typeof(object);
// Act
ConditionalExpressionFingerprint fingerprint1 = new ConditionalExpressionFingerprint(nodeType, type);
ConditionalExpressionFingerprint fingerprint2 = new ConditionalExpressionFingerprint(nodeType, typeof(string));
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class ConstantExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Constant;
Type expectedType = typeof(object);
// Act
ConstantExpressionFingerprint fingerprint = new ConstantExpressionFingerprint(expectedNodeType, expectedType);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Constant;
Type type = typeof(object);
// Act
ConstantExpressionFingerprint fingerprint1 = new ConstantExpressionFingerprint(nodeType, type);
ConstantExpressionFingerprint fingerprint2 = new ConstantExpressionFingerprint(nodeType, type);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Constant;
Type type = typeof(object);
// Act
ConstantExpressionFingerprint fingerprint1 = new ConstantExpressionFingerprint(nodeType, type);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Constant;
Type type = typeof(object);
// Act
ConstantExpressionFingerprint fingerprint1 = new ConstantExpressionFingerprint(nodeType, type);
ConstantExpressionFingerprint fingerprint2 = new ConstantExpressionFingerprint(nodeType, typeof(string));
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class DefaultExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Default;
Type expectedType = typeof(object);
// Act
DefaultExpressionFingerprint fingerprint = new DefaultExpressionFingerprint(expectedNodeType, expectedType);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Default;
Type type = typeof(object);
// Act
DefaultExpressionFingerprint fingerprint1 = new DefaultExpressionFingerprint(nodeType, type);
DefaultExpressionFingerprint fingerprint2 = new DefaultExpressionFingerprint(nodeType, type);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Default;
Type type = typeof(object);
// Act
DefaultExpressionFingerprint fingerprint1 = new DefaultExpressionFingerprint(nodeType, type);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_NodeType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Default;
Type type = typeof(object);
// Act
DefaultExpressionFingerprint fingerprint1 = new DefaultExpressionFingerprint(nodeType, type);
DefaultExpressionFingerprint fingerprint2 = new DefaultExpressionFingerprint(nodeType, typeof(string));
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
namespace System.Web.Mvc.ExpressionUtil.Test
{
// Represents an ExpressionFingerprint that is of the wrong type.
internal sealed class DummyExpressionFingerprint : ExpressionFingerprint
{
public DummyExpressionFingerprint(ExpressionType nodeType, Type type)
: base(nodeType, type)
{
}
}
}

View File

@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class ExpressionFingerprintTest
{
[Fact]
public void Comparison_Equality()
{
// Act
DummyExpressionFingerprint fingerprint1 = new DummyExpressionFingerprint(ExpressionType.Default, typeof(object));
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(ExpressionType.Default, typeof(object));
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_NodeType()
{
// Act
DummyExpressionFingerprint fingerprint1 = new DummyExpressionFingerprint(ExpressionType.Default, typeof(object));
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(ExpressionType.Parameter, typeof(object));
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Act
DummyExpressionFingerprint fingerprint1 = new DummyExpressionFingerprint(ExpressionType.Default, typeof(object));
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(ExpressionType.Default, typeof(string));
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,303 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class FingerprintingExpressionVisitorTest
{
private const ExpressionFingerprint _nullFingerprint = null;
[Fact]
public void TypeOverridesAllMethods()
{
// Ensures that the FingerprintingExpressionVisitor type overrides all VisitXxx methods so that
// it can properly set the "I gave up" flag when it encounters an Expression it's not familiar
// with.
var methodsOnExpressionVisitorRequiringOverride = typeof(ExpressionVisitor).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(mi => mi.IsVirtual).Select(mi => mi.GetBaseDefinition()).Where(mi => mi.DeclaringType == typeof(ExpressionVisitor));
var methodsOnFingerprintingExpressionVisitor = typeof(FingerprintingExpressionVisitor).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance).Where(mi => mi.DeclaringType == typeof(FingerprintingExpressionVisitor));
var missingMethods = methodsOnExpressionVisitorRequiringOverride.Except(methodsOnFingerprintingExpressionVisitor.Select(mi => mi.GetBaseDefinition())).ToArray();
if (missingMethods.Length != 0)
{
StringBuilder sb = new StringBuilder("The following methods are declared on ExpressionVisitor and must be overridden on FingerprintingExpressionVisitor:");
foreach (MethodInfo method in missingMethods)
{
sb.AppendLine();
sb.Append(method);
}
Assert.True(false, sb.ToString());
}
}
[Fact]
public void Visit_Null()
{
// Arrange
// fingerprints as [ NULL ]
Expression expr = null;
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Empty(capturedConstants);
AssertChainEquals(fingerprint, _nullFingerprint);
}
[Fact]
public void Visit_Unknown()
{
// Arrange
// if we fingerprinted ctors, would fingerprint as [ NEW(StringBuilder(int)):StringBuilder, PARAM(0):int ]
// but since we don't fingerprint ctors, should just return null (signaling failure)
Expression expr = (Expression<Func<int, StringBuilder>>)(capacity => new StringBuilder(capacity));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Null(fingerprint); // Can't fingerprint ctor
Assert.Null(capturedConstants); // Can't fingerprint ctor
}
[Fact]
public void VisitBinary()
{
// Arrange
// fingerprints as [ OP_GREATERTHAN:bool, CONST:int, CONST:int ]
Expression expr = Expression.MakeBinary(ExpressionType.GreaterThan, Expression.Constant(42), Expression.Constant(84));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Equal(new object[] { 42, 84 }, capturedConstants.ToArray());
AssertChainEquals(fingerprint,
new BinaryExpressionFingerprint(ExpressionType.GreaterThan, typeof(bool), null /* method */),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)));
}
[Fact]
public void VisitConditional()
{
// Arrange
// fingerprints as [ CONDITIONAL:int, CONST:bool, CONST:int, CONST:int ]
Expression expr = Expression.Condition(Expression.Constant(true), Expression.Constant(42), Expression.Constant(84));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Equal(new object[] { true, 42, 84 }, capturedConstants.ToArray());
AssertChainEquals(fingerprint,
new ConditionalExpressionFingerprint(ExpressionType.Conditional, typeof(int)),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(bool)),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)));
}
[Fact]
public void VisitConstant()
{
// Arrange
// fingerprints as [ CONST:int ]
Expression expr = Expression.Constant(42);
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Equal(new object[] { 42 }, capturedConstants.ToArray());
AssertChainEquals(fingerprint,
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)));
}
[Fact]
public void VisitDefault()
{
// Arrange
// fingerprints as [ DEFAULT:int ]
Expression expr = Expression.Default(typeof(int));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Empty(capturedConstants);
AssertChainEquals(fingerprint, new DefaultExpressionFingerprint(ExpressionType.Default, typeof(int)));
}
[Fact]
public void VisitIndex()
{
// Arrange
// fingerprints as [ INDEX:object, PARAM(0):object[], CONST:int ]
Expression expr = Expression.MakeIndex(Expression.Parameter(typeof(object[])), null /* indexer */, new Expression[] { Expression.Constant(42) });
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Equal(new object[] { 42 }, capturedConstants.ToArray());
AssertChainEquals(fingerprint,
new IndexExpressionFingerprint(ExpressionType.Index, typeof(object), null /* indexer */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(object[]), 0 /* parameterIndex */),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)));
}
[Fact]
public void VisitLambda()
{
// Arrange
// fingerprints as [ LAMBDA:Func<string, int>, CONST:int, PARAM(0):string ]
Expression expr = (Expression<Func<string, int>>)(x => 42);
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Equal(new object[] { 42 }, capturedConstants.ToArray());
AssertChainEquals(fingerprint,
new LambdaExpressionFingerprint(ExpressionType.Lambda, typeof(Func<string, int>)),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(string), 0 /* parameterIndex */));
}
[Fact]
public void VisitMember()
{
// Arrange
// fingerprints as [ MEMBER(String.Empty):string, NULL ]
Expression expr = Expression.Field(null, typeof(string), "Empty");
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Empty(capturedConstants);
AssertChainEquals(fingerprint,
new MemberExpressionFingerprint(ExpressionType.MemberAccess, typeof(string), typeof(string).GetField("Empty")),
_nullFingerprint);
}
[Fact]
public void VisitMethodCall()
{
// Arrange
// fingerprints as [ CALL(GC.KeepAlive):void, NULL, PARAM(0):object ]
Expression expr = Expression.Call(typeof(GC).GetMethod("KeepAlive"), Expression.Parameter(typeof(object)));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Empty(capturedConstants);
AssertChainEquals(fingerprint,
new MethodCallExpressionFingerprint(ExpressionType.Call, typeof(void), typeof(GC).GetMethod("KeepAlive")),
_nullFingerprint,
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(object), 0 /* parameterIndex */));
}
[Fact]
public void VisitParameter()
{
// Arrange
// fingerprints as [ LAMBDA:Func<int, int, int>, OP_ADD:int, OP_ADD:int, OP_ADD:int, PARAM(0):int, PARAM(0):int, PARAM(1):int, PARAM(0):int, PARAM(1):int, PARAM(0):int ]
// (note that the parameters are out of order since 'y' is used first, but this is ok due
// to preservation of alpha equivalence within the VisitParameter method.)
Expression expr = (Expression<Func<int, int, int>>)((x, y) => y + y + x + y);
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Empty(capturedConstants);
AssertChainEquals(fingerprint,
new LambdaExpressionFingerprint(ExpressionType.Lambda, typeof(Func<int, int, int>)),
new BinaryExpressionFingerprint(ExpressionType.Add, typeof(int), null /* method */),
new BinaryExpressionFingerprint(ExpressionType.Add, typeof(int), null /* method */),
new BinaryExpressionFingerprint(ExpressionType.Add, typeof(int), null /* method */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 0 /* parameterIndex */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 0 /* parameterIndex */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 1 /* parameterIndex */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 0 /* parameterIndex */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 1 /* parameterIndex */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 0 /* parameterIndex */));
}
[Fact]
public void VisitTypeBinary()
{
// Arrange
// fingerprints as [ TYPEIS(DateTime):bool, CONST:string ]
Expression expr = Expression.TypeIs(Expression.Constant("hello"), typeof(DateTime));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Equal(new object[] { "hello" }, capturedConstants.ToArray());
AssertChainEquals(fingerprint,
new TypeBinaryExpressionFingerprint(ExpressionType.TypeIs, typeof(bool), typeof(DateTime)),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(string)));
}
[Fact]
public void VisitUnary()
{
// Arrange
// fingerprints as [ OP_NOT:int, PARAM:int ]
Expression expr = Expression.Not(Expression.Parameter(typeof(int)));
// Act
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(expr, out capturedConstants);
// Assert
Assert.Empty(capturedConstants);
AssertChainEquals(fingerprint,
new UnaryExpressionFingerprint(ExpressionType.Not, typeof(int), null /* method */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(int), 0 /* parameterIndex */));
}
internal static void AssertChainEquals(ExpressionFingerprintChain fingerprintChain, params ExpressionFingerprint[] expectedElements)
{
ExpressionFingerprintChain newChain = new ExpressionFingerprintChain();
newChain.Elements.AddRange(expectedElements);
Assert.Equal(fingerprintChain, newChain);
}
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class HoistingExpressionVisitorTest
{
[Fact]
public void Hoist()
{
// Arrange
Expression<Func<string, int>> expr = s => 2 * s.Length + 1;
// Act
Expression<Hoisted<string, int>> hoisted = HoistingExpressionVisitor<string, int>.Hoist(expr);
// Assert
// new expression should be (s, capturedConstants) => (int)(capturedConstants[0]) * s.Length + (int)(capturedConstants[1])
// with fingerprint [ LAMBDA:Hoisted<string, int>, OP_ADD:int, OP_MULTIPLY:int, OP_CAST:int, INDEX(List<object>.get_Item):object, PARAM(0):List<object>, CONST:int, MEMBER(String.Length):int, PARAM(1):string, OP_CAST:int, INDEX(List<object>.get_Item):object, PARAM(0):List<object>, CONST:int, PARAM(1):string, PARAM(0):List<object> ]
List<object> capturedConstants;
ExpressionFingerprintChain fingerprint = FingerprintingExpressionVisitor.GetFingerprintChain(hoisted, out capturedConstants);
Assert.Equal(new object[] { 0, 1 }, capturedConstants.ToArray()); // these are constants from the hoisted expression (array indexes), not the original expression
FingerprintingExpressionVisitorTest.AssertChainEquals(
fingerprint,
new LambdaExpressionFingerprint(ExpressionType.Lambda, typeof(Hoisted<string, int>)),
new BinaryExpressionFingerprint(ExpressionType.Add, typeof(int), null /* method */),
new BinaryExpressionFingerprint(ExpressionType.Multiply, typeof(int), null /* method */),
new UnaryExpressionFingerprint(ExpressionType.Convert, typeof(int), null /* method */),
new IndexExpressionFingerprint(ExpressionType.Index, typeof(object), typeof(List<object>).GetProperty("Item")),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(List<object>), 0 /* parameterIndex */),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)),
new MemberExpressionFingerprint(ExpressionType.MemberAccess, typeof(int), typeof(string).GetProperty("Length")),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(string), 1 /* parameterIndex */),
new UnaryExpressionFingerprint(ExpressionType.Convert, typeof(int), null /* method */),
new IndexExpressionFingerprint(ExpressionType.Index, typeof(object), typeof(List<object>).GetProperty("Item")),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(List<object>), 0 /* parameterIndex */),
new ConstantExpressionFingerprint(ExpressionType.Constant, typeof(int)),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(string), 1 /* parameterIndex */),
new ParameterExpressionFingerprint(ExpressionType.Parameter, typeof(List<object>), 0 /* parameterIndex */));
}
}
}

View File

@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using System.Reflection;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class IndexExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Index;
Type expectedType = typeof(char);
PropertyInfo expectedIndexer = typeof(string).GetProperty("Chars");
// Act
IndexExpressionFingerprint fingerprint = new IndexExpressionFingerprint(expectedNodeType, expectedType, expectedIndexer);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedIndexer, fingerprint.Indexer);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Index;
Type type = typeof(char);
PropertyInfo indexer = typeof(string).GetProperty("Chars");
// Act
IndexExpressionFingerprint fingerprint1 = new IndexExpressionFingerprint(nodeType, type, indexer);
IndexExpressionFingerprint fingerprint2 = new IndexExpressionFingerprint(nodeType, type, indexer);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Index;
Type type = typeof(char);
PropertyInfo indexer = typeof(string).GetProperty("Chars");
// Act
IndexExpressionFingerprint fingerprint1 = new IndexExpressionFingerprint(nodeType, type, indexer);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Indexer()
{
// Arrange
ExpressionType nodeType = ExpressionType.Index;
Type type = typeof(char);
PropertyInfo indexer = typeof(string).GetProperty("Chars");
// Act
IndexExpressionFingerprint fingerprint1 = new IndexExpressionFingerprint(nodeType, type, indexer);
IndexExpressionFingerprint fingerprint2 = new IndexExpressionFingerprint(nodeType, type, null /* indexer */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Index;
Type type = typeof(char);
PropertyInfo indexer = typeof(string).GetProperty("Chars");
// Act
IndexExpressionFingerprint fingerprint1 = new IndexExpressionFingerprint(nodeType, type, indexer);
IndexExpressionFingerprint fingerprint2 = new IndexExpressionFingerprint(nodeType, typeof(object), indexer);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class LambdaExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Lambda;
Type expectedType = typeof(Action<object>);
// Act
LambdaExpressionFingerprint fingerprint = new LambdaExpressionFingerprint(expectedNodeType, expectedType);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Lambda;
Type type = typeof(Action<object>);
// Act
LambdaExpressionFingerprint fingerprint1 = new LambdaExpressionFingerprint(nodeType, type);
LambdaExpressionFingerprint fingerprint2 = new LambdaExpressionFingerprint(nodeType, type);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Lambda;
Type type = typeof(Action<object>);
// Act
LambdaExpressionFingerprint fingerprint1 = new LambdaExpressionFingerprint(nodeType, type);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_NodeType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Lambda;
Type type = typeof(Action<object>);
// Act
LambdaExpressionFingerprint fingerprint1 = new LambdaExpressionFingerprint(nodeType, type);
LambdaExpressionFingerprint fingerprint2 = new LambdaExpressionFingerprint(nodeType, typeof(Action));
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using System.Reflection;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class MemberExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.MemberAccess;
Type expectedType = typeof(int);
MemberInfo expectedMember = typeof(TimeSpan).GetProperty("Seconds");
// Act
MemberExpressionFingerprint fingerprint = new MemberExpressionFingerprint(expectedNodeType, expectedType, expectedMember);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedMember, fingerprint.Member);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.MemberAccess;
Type type = typeof(int);
MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");
// Act
MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, type, member);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.MemberAccess;
Type type = typeof(int);
MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");
// Act
MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Member()
{
// Arrange
ExpressionType nodeType = ExpressionType.MemberAccess;
Type type = typeof(int);
MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");
// Act
MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, type, null /* member */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.MemberAccess;
Type type = typeof(int);
MemberInfo member = typeof(TimeSpan).GetProperty("Seconds");
// Act
MemberExpressionFingerprint fingerprint1 = new MemberExpressionFingerprint(nodeType, type, member);
MemberExpressionFingerprint fingerprint2 = new MemberExpressionFingerprint(nodeType, typeof(object), member);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using System.Reflection;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class MethodCallExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Call;
Type expectedType = typeof(string);
MethodInfo expectedMethod = typeof(string).GetMethod("Intern");
// Act
MethodCallExpressionFingerprint fingerprint = new MethodCallExpressionFingerprint(expectedNodeType, expectedType, expectedMethod);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedMethod, fingerprint.Method);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Call;
Type type = typeof(string);
MethodInfo method = typeof(string).GetMethod("Intern");
// Act
MethodCallExpressionFingerprint fingerprint1 = new MethodCallExpressionFingerprint(nodeType, type, method);
MethodCallExpressionFingerprint fingerprint2 = new MethodCallExpressionFingerprint(nodeType, type, method);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Call;
Type type = typeof(string);
MethodInfo method = typeof(string).GetMethod("Intern");
// Act
MethodCallExpressionFingerprint fingerprint1 = new MethodCallExpressionFingerprint(nodeType, type, method);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Method()
{
// Arrange
ExpressionType nodeType = ExpressionType.Call;
Type type = typeof(string);
MethodInfo method = typeof(string).GetMethod("Intern");
// Act
MethodCallExpressionFingerprint fingerprint1 = new MethodCallExpressionFingerprint(nodeType, type, method);
MethodCallExpressionFingerprint fingerprint2 = new MethodCallExpressionFingerprint(nodeType, type, null /* method */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Call;
Type type = typeof(string);
MethodInfo method = typeof(string).GetMethod("Intern");
// Act
MethodCallExpressionFingerprint fingerprint1 = new MethodCallExpressionFingerprint(nodeType, type, method);
MethodCallExpressionFingerprint fingerprint2 = new MethodCallExpressionFingerprint(nodeType, typeof(object), method);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,92 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class ParameterExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Parameter;
Type expectedType = typeof(object);
int expectedParameterIndex = 1;
// Act
ParameterExpressionFingerprint fingerprint = new ParameterExpressionFingerprint(expectedNodeType, expectedType, expectedParameterIndex);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedParameterIndex, fingerprint.ParameterIndex);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Parameter;
Type type = typeof(object);
int parameterIndex = 1;
// Act
ParameterExpressionFingerprint fingerprint1 = new ParameterExpressionFingerprint(nodeType, type, parameterIndex);
ParameterExpressionFingerprint fingerprint2 = new ParameterExpressionFingerprint(nodeType, type, parameterIndex);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Parameter;
Type type = typeof(object);
int parameterIndex = 1;
// Act
ParameterExpressionFingerprint fingerprint1 = new ParameterExpressionFingerprint(nodeType, type, parameterIndex);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Method()
{
// Arrange
ExpressionType nodeType = ExpressionType.Parameter;
Type type = typeof(object);
int parameterIndex = 1;
// Act
ParameterExpressionFingerprint fingerprint1 = new ParameterExpressionFingerprint(nodeType, type, parameterIndex);
ParameterExpressionFingerprint fingerprint2 = new ParameterExpressionFingerprint(nodeType, type, -1 /* parameterIndex */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Parameter;
Type type = typeof(object);
int parameterIndex = 1;
// Act
ParameterExpressionFingerprint fingerprint1 = new ParameterExpressionFingerprint(nodeType, type, parameterIndex);
ParameterExpressionFingerprint fingerprint2 = new ParameterExpressionFingerprint(nodeType, typeof(string), parameterIndex);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,92 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class TypeBinaryExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.TypeIs;
Type expectedType = typeof(bool);
Type expectedTypeOperand = typeof(object);
// Act
TypeBinaryExpressionFingerprint fingerprint = new TypeBinaryExpressionFingerprint(expectedNodeType, expectedType, expectedTypeOperand);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedTypeOperand, fingerprint.TypeOperand);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.TypeIs;
Type type = typeof(bool);
Type typeOperand = typeof(object);
// Act
TypeBinaryExpressionFingerprint fingerprint1 = new TypeBinaryExpressionFingerprint(nodeType, type, typeOperand);
TypeBinaryExpressionFingerprint fingerprint2 = new TypeBinaryExpressionFingerprint(nodeType, type, typeOperand);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.TypeIs;
Type type = typeof(bool);
Type typeOperand = typeof(object);
// Act
TypeBinaryExpressionFingerprint fingerprint1 = new TypeBinaryExpressionFingerprint(nodeType, type, typeOperand);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_TypeOperand()
{
// Arrange
ExpressionType nodeType = ExpressionType.TypeIs;
Type type = typeof(bool);
Type typeOperand = typeof(object);
// Act
TypeBinaryExpressionFingerprint fingerprint1 = new TypeBinaryExpressionFingerprint(nodeType, type, typeOperand);
TypeBinaryExpressionFingerprint fingerprint2 = new TypeBinaryExpressionFingerprint(nodeType, type, typeof(string) /* typeOperand */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.TypeIs;
Type type = typeof(bool);
Type typeOperand = typeof(object);
// Act
TypeBinaryExpressionFingerprint fingerprint1 = new TypeBinaryExpressionFingerprint(nodeType, type, typeOperand);
TypeBinaryExpressionFingerprint fingerprint2 = new TypeBinaryExpressionFingerprint(nodeType, typeof(object), typeOperand);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}

View File

@@ -0,0 +1,93 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Linq.Expressions;
using System.Reflection;
using Xunit;
namespace System.Web.Mvc.ExpressionUtil.Test
{
public class UnaryExpressionFingerprintTest
{
[Fact]
public void Properties()
{
// Arrange
ExpressionType expectedNodeType = ExpressionType.Not;
Type expectedType = typeof(int);
MethodInfo expectedMethod = typeof(object).GetMethod("GetHashCode");
// Act
UnaryExpressionFingerprint fingerprint = new UnaryExpressionFingerprint(expectedNodeType, expectedType, expectedMethod);
// Assert
Assert.Equal(expectedNodeType, fingerprint.NodeType);
Assert.Equal(expectedType, fingerprint.Type);
Assert.Equal(expectedMethod, fingerprint.Method);
}
[Fact]
public void Comparison_Equality()
{
// Arrange
ExpressionType nodeType = ExpressionType.Not;
Type type = typeof(int);
MethodInfo method = typeof(object).GetMethod("GetHashCode");
// Act
UnaryExpressionFingerprint fingerprint1 = new UnaryExpressionFingerprint(nodeType, type, method);
UnaryExpressionFingerprint fingerprint2 = new UnaryExpressionFingerprint(nodeType, type, method);
// Assert
Assert.Equal(fingerprint1, fingerprint2);
Assert.Equal(fingerprint1.GetHashCode(), fingerprint2.GetHashCode());
}
[Fact]
public void Comparison_Inequality_FingerprintType()
{
// Arrange
ExpressionType nodeType = ExpressionType.Not;
Type type = typeof(int);
MethodInfo method = typeof(object).GetMethod("GetHashCode");
// Act
UnaryExpressionFingerprint fingerprint1 = new UnaryExpressionFingerprint(nodeType, type, method);
DummyExpressionFingerprint fingerprint2 = new DummyExpressionFingerprint(nodeType, type);
// Assert
Assert.NotEqual<ExpressionFingerprint>(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Method()
{
// Arrange
ExpressionType nodeType = ExpressionType.Not;
Type type = typeof(int);
MethodInfo method = typeof(object).GetMethod("GetHashCode");
// Act
UnaryExpressionFingerprint fingerprint1 = new UnaryExpressionFingerprint(nodeType, type, method);
UnaryExpressionFingerprint fingerprint2 = new UnaryExpressionFingerprint(nodeType, type, null /* method */);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
[Fact]
public void Comparison_Inequality_Type()
{
// Arrange
ExpressionType nodeType = ExpressionType.Not;
Type type = typeof(int);
MethodInfo method = typeof(object).GetMethod("GetHashCode");
// Act
UnaryExpressionFingerprint fingerprint1 = new UnaryExpressionFingerprint(nodeType, type, method);
UnaryExpressionFingerprint fingerprint2 = new UnaryExpressionFingerprint(nodeType, typeof(object), method);
// Assert
Assert.NotEqual(fingerprint1, fingerprint2);
}
}
}