public void Process_ChainedMemberAccess_LongChain_WithNullableValueType() { // Arrange var expected = "TestVal"; var model = new Chain0Model { Chain1 = new Chain1Model { NullableValueTypeModel = new ValueType1 { TestModel = new TestModel { DifferentModel = new DifferentModel { Name = expected } } } } }; var expression = GetExpression <Chain0Model, string>(m => m.Chain1.NullableValueTypeModel.Value.TestModel.DifferentModel.Name); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Equal(expected, result); }
public void CreateInstance_WithAbstractTypeAndPublicConstructor_ThrowsCorrectException() { // Act & Assert var ex = Assert2.Throws <InvalidOperationException>(() => ActivatorUtilities.CreateInstance(default(IServiceProvider), typeof(AbstractFoo))); //var msg = "A suitable constructor for type 'Microsoft.Extensions.DependencyInjection.Specification.DependencyInjectionSpecificationTests+AbstractFoo' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor."; Assert2.NotNull(ex.Message); }
//[Theory] //[MemberData(nameof(CreateInstanceFuncs))] public void TypeActivatorWorksWithStaticCtor_Theory(CreateInstanceFunc createFunc) { // Act var anotherClass = CreateInstance <ClassWithStaticCtor>(createFunc, provider: null); // Assert Assert2.NotNull(anotherClass); }
public void ObjectToDictionary_WithPlainObjectType_ReturnsEmptyDictionary() { // Arrange var value = new object(); // Act var dictValues = PropertyHelper.ObjectToDictionary(value); // Assert Assert2.NotNull(dictValues); Assert2.Equal(0, dictValues.Count); }
public void ObjectToDictionary_WithNullObject_ReturnsEmptyDictionary() { // Arrange object value = null; // Act var dictValues = PropertyHelper.ObjectToDictionary(value); // Assert Assert2.NotNull(dictValues); Assert2.Equal(0, dictValues.Count); }
public void PropertyHelper_ReturnsGetterDelegate() { // Arrange var anonymous = new { bar = "baz" }; var property = PropertyHelper.GetPropertyAccessors(anonymous.GetType()).First().Property; // Act var helper = new PropertyHelper.PropertyAccessor(property); // Assert Assert2.NotNull(helper.ValueGetter); Assert2.Equal("baz", helper.ValueGetter(anonymous)); }
public void Process_ConstFieldLookup_WthNullModel() { // Arrange var expression = GetTestModelExpression(m => DifferentModel.Constant); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(null); Assert2.Equal(10, result); }
public void Process_CachesStaticPropertyAccess() { // Arrange var expression1 = GetTestModelExpression(m => TestModel.StaticProperty); var expression2 = GetTestModelExpression(m => TestModel.StaticProperty); // Act var func1 = CachedExpressionCompiler.Process(expression1); var func2 = CachedExpressionCompiler.Process(expression2); // Assert Assert2.NotNull(func1); Assert2.Same(func1, func2); }
public void ObjectToDictionary_ReturnsCaseInsensitiveDictionary() { // Arrange var value = new { TEST = "value", oThEr = 1 }; // Act var dictValues = PropertyHelper.ObjectToDictionary(value); // Assert Assert2.NotNull(dictValues); Assert2.Equal(2, dictValues.Count); Assert2.Equal("value", dictValues["test"]); Assert2.Equal(1, dictValues["other"]); }
public void ObjectToDictionary_WithAnonymousType_LooksUpProperties() { // Arrange var value = new { test = "value", other = 1 }; // Act var dictValues = PropertyHelper.ObjectToDictionary(value); // Assert Assert2.NotNull(dictValues); Assert2.Equal(2, dictValues.Count); Assert2.Equal("value", dictValues["test"]); Assert2.Equal(1, dictValues["other"]); }
public void Process_CachesSimpleMemberAccess() { // Arrange var expression1 = GetTestModelExpression(m => m.Name); var expression2 = GetTestModelExpression(m => m.Name); // Act var func1 = CachedExpressionCompiler.Process(expression1); var func2 = CachedExpressionCompiler.Process(expression2); // Assert Assert2.NotNull(func1); Assert2.Same(func1, func2); }
public void Process_CachesIdentityExpression() { // Arrange var expression1 = GetTestModelExpression(m => m); var expression2 = GetTestModelExpression(m => m); // Act var func1 = CachedExpressionCompiler.Process(expression1); var func2 = CachedExpressionCompiler.Process(expression2); // Assert Assert2.NotNull(func1); Assert2.Same(func1, func2); }
public void Process_CachesChainedMemberAccess_ToValueType() { // Arrange var expression1 = GetTestModelExpression(m => m.Date.Year); var expression2 = GetTestModelExpression(m => m.Date.Year); // Act var func1 = CachedExpressionCompiler.Process(expression1); var func2 = CachedExpressionCompiler.Process(expression2); // Assert Assert2.NotNull(func1); Assert2.Same(func1, func2); }
public async Task TargetMethodReturningValueTaskOfValueType_CanBeInvokedViaExecuteAsync() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.ValueTaskOfValueType)); // Act var result = await executor.ExecuteAsync(_targetObject, new object[] { 123 }); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(int), executor.AsyncResultType); Assert2.NotNull(result); Assert2.Equal(123, (int)result); }
public async Task TargetMethodReturningCustomAwaitableOfReferenceType_CanInvokeViaExecute() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.CustomAwaitableOfReferenceTypeAsync)); // Act var result = await(TestAwaitable <TestObject>) executor.Execute(_targetObject, new object[] { "Hello", 123 }); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(TestObject), executor.AsyncResultType); Assert2.NotNull(result); Assert2.Equal("Hello 123", result.value); }
public void Process_ConstLookup_WithNullModel() { // Arrange var differentModel = new DifferentModel(); var expression = GetTestModelExpression(m => differentModel); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(null); Assert2.Same(differentModel, result); }
public void Process_ChainedMemberAccess_ToReferenceType_WithNullModel() { // Arrange var model = (TestModel)null; var expression = GetTestModelExpression(m => m.DifferentModel.Name); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Null(result); }
public void Process_IdentityExpression() { // Arrange var model = new TestModel(); var expression = GetTestModelExpression(m => m); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Same(model, result); }
public void Process_ConstLookup_ReturningNull() { // Arrange var model = new TestModel(); var expression = GetTestModelExpression(m => (DifferentModel)null); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Null(result); }
public void Process_SimpleMemberAccess_OnNullableValueType_WithNullValue() { // Arrange var nullableModel = (DateTime?)null; var expression = GetExpression <DateTime?, DateTime>(m => m.Value); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(nullableModel); Assert2.Null(result); }
public void Process_SimpleMemberAccess_OnValueType_WithDefaultValue() { // Arrange var model = default(DateTime); var expression = GetExpression <DateTime, int>(m => m.Year); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Equal(1, result); }
public void Process_ConstLookup_WithPrimitiveConstant() { // Arrange var model = new TestModel(); var expression = GetTestModelExpression(m => 10); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Equal(10, result); }
public void Process_SimpleMemberAccess_OnTypeWithBadEqualityComparer_WithNullModel() { // Arrange var model = (BadEqualityModel)null; var expression = GetExpression <BadEqualityModel, int>(m => m.Id); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Null(result); }
public void Process_SimpleMemberAccess_ToPrimitive_WithNullModel() { // Arrange var model = (TestModel)null; var expression = GetTestModelExpression(m => m.Age); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Null(result); }
public void Process_StaticFieldAccess() { // Arrange var model = new TestModel(); var expression = GetTestModelExpression(m => TestModel.StaticField); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Equal("StaticValue", result); }
public void Process_SimpleMemberAccess_OnTypeWithBadEqualityComparer_WithDefaultValue() { // Arrange var model = default(BadEqualityValueTypeModel); var expression = GetExpression <BadEqualityValueTypeModel, int>(m => m.Id); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Equal(model.Id, result); }
public async Task TargetMethodReturningCustomAwaitableOfValueType_CanInvokeViaExecuteAsync() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.CustomAwaitableOfValueTypeAsync)); // Act var result = await executor.ExecuteAsync(_targetObject, new object[] { 123, 456 }); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(int), executor.AsyncResultType); Assert2.NotNull(result); Assert2.IsType <int>(result); Assert2.Equal(579, (int)result); }
public void GetProperties_ExcludesIndexersAndPropertiesWithoutPublicGetters() { // Arrange var type = typeof(DerivedClassWithNonReadableProperties); // Act var result = PropertyHelper.GetPropertyAccessors(type).ToArray(); // Assert Assert2.NotNull(result); //Assert2.Equal(3, result.Length); //Assert2.Equal("Visible", result[0].Name); //Assert2.Equal("PropA", result[1].Name); //Assert2.Equal("PropB", result[2].Name); }
public void Process_SimpleMemberAccess_OnNullableValueType() { // Arrange var model = new DateTime(2000, 1, 1); var nullableModel = (DateTime?)model; var expression = GetExpression <DateTime?, DateTime>(m => m.Value); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(nullableModel); Assert2.Equal(model, result); }
public void Process_CapturedVariable_WithNullModel() { // Arrange var differentModel = new DifferentModel(); var model = (TestModel)null; var expression = GetTestModelExpression(m => differentModel); // Act var func = CachedExpressionCompiler.Process(expression); // Assert Assert2.NotNull(func); var result = func(model); Assert2.Same(differentModel, result); }