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_CapturesInnerException_OfTargetInvocationException() { // Act & Assert var ex = Assert2.Throws <InvalidOperationException>(() => ActivatorUtilities.CreateInstance(default(IServiceProvider), typeof(Bar))); var msg = "some error"; Assert2.Equal(msg, ex.Message); }
//[Theory] //[MemberData(nameof(ExpressionAndTexts))] private void GetExpressionText_ReturnsExpectedExpressionText_Theory(LambdaExpression expression, string expressionText) { // Act var text = ExpressionHelper.GetExpressionText(expression, _expressionTextCache); // Assert Assert2.Equal(expressionText, text); }
public void GetDefaultValueForParameters_ReturnsSuppliedValues() { var suppliedDefaultValues = new object[] { 123, "test value" }; var executor = GetExecutorForMethod(nameof(TestObject.MethodWithMultipleParameters), suppliedDefaultValues); Assert2.Equal(suppliedDefaultValues[0], executor.GetDefaultValueForParameter(0)); Assert2.Equal(suppliedDefaultValues[1], executor.GetDefaultValueForParameter(1)); Assert2.Throws <ArgumentOutOfRangeException>(() => executor.GetDefaultValueForParameter(2)); }
public void PropertyHelper_DoesNotFindStaticProperties() { // Arrange var anonymous = new Static(); // Act + Assert var helper = Assert2.Single(PropertyHelper.GetPropertyAccessors(anonymous.GetType().GetTypeInfo())); Assert2.Equal("Prop5", helper.Name); }
public void ExecuteValueMethod() { var executor = GetExecutorForMethod(nameof(TestObject.ValueMethod)); var result = executor.Execute( _targetObject, new object[] { 10, 20 }); Assert2.False(executor.IsMethodAsync); Assert2.Equal(30, (int)result); }
public async Task ExecuteValueMethodAsync() { var executor = GetExecutorForMethod(nameof(TestObject.ValueMethodAsync)); var result = await executor.ExecuteAsync( _targetObject, new object[] { 10, 20 }); Assert2.True(executor.IsMethodAsync); Assert2.Equal(30, (int)result); }
public void PropertyHelper_RefStructProperties() { // Arrange var obj = new RefStructProperties(); // Act + Assert var helper = Assert2.Single(PropertyHelper.GetPropertyAccessors(obj.GetType().GetTypeInfo())); Assert2.Equal("Prop5", helper.Name); }
public void PropertyHelper_DoesNotChangeUnderscores() { // Arrange var anonymous = new { bar_baz2 = "foo" }; // Act + Assert var helper = Assert2.Single(PropertyHelper.GetPropertyAccessors(anonymous.GetType().GetTypeInfo())); Assert2.Equal("bar_baz2", helper.Name); }
//[Theory] //[MemberData(nameof(IgnoreCaseTestData))] private void ObjectToDictionary_IgnoresPropertyCase_Theory(object testObject, KeyValuePair <string, object> expectedEntry) { // Act var result = PropertyHelper.ObjectToDictionary(testObject); // Assert var entry = Assert2.Single(result); Assert2.Equal(expectedEntry, entry); }
public void ExecuteValueMethodWithReturnType() { var executor = GetExecutorForMethod(nameof(TestObject.ValueMethodWithReturnType)); var result = executor.Execute( _targetObject, new object[] { 10 }); var resultObject = Assert2.IsType <TestObject>(result); Assert2.False(executor.IsMethodAsync); Assert2.Equal("Hello", resultObject.value); }
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 async Task ExecuteValueMethodWithReturnTypeAsync() { var executor = GetExecutorForMethod(nameof(TestObject.ValueMethodWithReturnTypeAsync)); var result = await executor.ExecuteAsync( _targetObject, new object[] { 10 }); var resultObject = Assert2.IsType <TestObject>(result); Assert2.True(executor.IsMethodAsync); Assert2.Equal("Hello", resultObject.value); }
//[Theory] //[MemberData(nameof(TypesWithNonPublicConstructorData))] public void TypeActivatorRequiresPublicConstructor_Theory(CreateInstanceFunc createFunc, Type type) { // Arrange var expectedMessage = $"A suitable constructor for type '{type}' could not be located. " + "Ensure the type is concrete and services are registered for all parameters of a public constructor."; // Act and Assert var ex = Assert2.Throws <InvalidOperationException>(() => createFunc(provider: null, type: type, args: new object[0])); Assert2.Equal(expectedMessage, ex.Message); }
public void ExecuteValueMethodUpdateValue() { var executor = GetExecutorForMethod(nameof(TestObject.ValueMethodUpdateValue)); var parameter = new TestObject(); var result = executor.Execute( _targetObject, new object[] { parameter }); var resultObject = Assert2.IsType <TestObject>(result); Assert2.False(executor.IsMethodAsync); Assert2.Equal("HelloWorld", resultObject.value); }
//[Theory] //[MemberData(nameof(IndexerExpressions))] //[MemberData(nameof(UnsupportedExpressions))] private void GetExpressionText_DoesNotCacheIndexerOrUnsupportedExpression_Theory(LambdaExpression expression) { // Act - 1 var text1 = ExpressionHelper.GetExpressionText(expression, _expressionTextCache); // Act - 2 var text2 = ExpressionHelper.GetExpressionText(expression, _expressionTextCache); // Assert Assert2.Equal(text1, text2, StringComparer.Ordinal); Assert2.NotSame(text1, text2); // not cached }
public void GivenTheSameInputs_ItProducesTheSameOutput() { var hashCode1 = new HashCodeCombiner(); var hashCode2 = new HashCodeCombiner(); hashCode1.Add(42); hashCode1.Add("foo"); hashCode2.Add(42); hashCode2.Add("foo"); Assert2.Equal(hashCode1.CombinedHash, hashCode2.CombinedHash); }
public void PropertyHelper_UnwrapsNullableTypes() { // Arrange var myType = typeof(MyStruct?); // Act var properties = PropertyHelper.GetPropertyAccessors(myType); // Assert var property = Assert2.Single(properties); Assert2.Equal("Foo", property.Name); }
public void PropertyHelper_ReturnsValueCorrectly() { // Arrange var anonymous = new { bar = "baz" }; var property = PropertyHelper.GetPropertyAccessors(anonymous.GetType()).First().Property; // Act var helper = new PropertyHelper.PropertyAccessor(property); // Assert Assert2.Equal("bar", helper.Name); Assert2.Equal("baz", helper.GetValue(anonymous)); }
public async Task TargetMethodReturningValueTaskOfReferenceType_CanBeInvokedViaExecuteAsync() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.ValueTaskOfReferenceType)); // Act var result = await executor.ExecuteAsync(_targetObject, new object[] { "test result" }); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(string), executor.AsyncResultType); Assert2.Equal("test result", result); }
public async Task TargetMethodReturningValueTaskOfValueType_CanBeInvokedViaExecute() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.ValueTaskOfValueType)); // Act var result = await(ValueTask <int>) executor.Execute(_targetObject, new object[] { 123 }); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(int), executor.AsyncResultType); Assert2.Equal(123, result); }
public async Task TargetMethodReturningAwaitableWithoutICriticalNotifyCompletion_UsesOnCompleted() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.CustomAwaitableWithoutICriticalNotifyCompletion)); // Act var result = await executor.ExecuteAsync(_targetObject, new object[0]); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(string), executor.AsyncResultType); Assert2.Equal("Used OnCompleted", (string)result); }
public async Task TargetMethodReturningCustomAwaitableOfValueType_CanInvokeViaExecute() { // Arrange var executor = GetExecutorForMethod(nameof(TestObject.CustomAwaitableOfValueTypeAsync)); // Act var result = await(TestAwaitable <int>) executor.Execute(_targetObject, new object[] { 123, 456 }); // Assert Assert2.True(executor.IsMethodAsync); Assert2.Same(typeof(int), executor.AsyncResultType); Assert2.Equal(579, result); }
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 PropertyHelper_ReturnsValueCorrectly_ForValueTypes() { // Arrange var anonymous = new { foo = 32 }; var property = PropertyHelper.GetPropertyAccessors(anonymous.GetType()).First().Property; // Act var helper = new PropertyHelper.PropertyAccessor(property); // Assert Assert2.Equal("foo", helper.Name); Assert2.Equal(32, helper.GetValue(anonymous)); }
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 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 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 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); }