public void ShouldCreateSimpleFunc_New_MethodCall()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, int>> expression = x => new Customer().CalculateAge();
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(33);
            reflectionResult.Should().Be(33);
        }
        public void ShouldCreateSimpleFunc_New()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, Customer>> expression = x => new Customer();
            Console.WriteLine(expression.ToString());

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

            Customer emitResult = emit.Invoke(customer);
            Customer reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().NotBeNull();
            reflectionResult.Should().NotBeNull();
        }
        public void ShouldCreateSimpleFunc_ExtMethodCall_Constant()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, int>> expression = x => x.GetDefaultAgeEx(20);
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(53);
            reflectionResult.Should().Be(53);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldExecuteOperator_ArrayLength()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, int>> expression = x => x.Names.Length;
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(2);
            reflectionResult.Should().Be(2);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldExecuteOperator_Convert()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, string>> expression = x => x.Firstname + "-" + 50;
            Console.WriteLine(expression.ToString());

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

            string emitResult = emit.Invoke(customer);
            string reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be("John-50");
            reflectionResult.Should().Be("John-50");
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldExecuteOperator_Not()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe") { IsPremium = true };
            Expression<Func<Customer, bool>> expression = x => !x.IsPremium;
            Console.WriteLine(expression.ToString());

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

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

            // Assert
            emitResult.Should().BeFalse();
            reflectionResult.Should().BeFalse();
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_ArrayAccess()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, string>> expression = x => x.Names[1];
            Console.WriteLine(expression.ToString());

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

            string emitResult = emit.Invoke(customer);
            string reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be("Doe");
            reflectionResult.Should().Be("Doe");
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldExecuteConditional()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe") { IsPremium = true };
            Expression<Func<Customer, int>> expression = x => x.IsPremium ? 500 : 300;
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(500);
            reflectionResult.Should().Be(500);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldExecuteOperator_TypeIs()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            #pragma warning disable 183
            Expression<Func<Customer, bool>> expression = x => x is Customer;
            #pragma warning restore 183
            Console.WriteLine(expression.ToString());

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

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

            // Assert
            emitResult.Should().BeTrue();
            reflectionResult.Should().BeTrue();
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_ArrayAccess_Delegate_CallingInvoke()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Func<string[]> func = () => new string[]
            {
                "One", "Two"
            };
            Expression<Func<Customer, string>> expression = x => func.Invoke()[0];
            Console.WriteLine(expression.ToString());

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

            string emitResult = emit.Invoke(customer);
            string reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be("One");
            reflectionResult.Should().Be("One");
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_ListInitializer()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, IList<string>>> expression = x => new List<string>
            {
                "Hello",
                "World"
            };
            Console.WriteLine(expression.ToString());

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

            IList<string> emitResult = emit.Invoke(customer);
            IList<string> reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Count.Should().Be(2);
            emitResult[0].Should().Be("Hello");
            reflectionResult.Count.Should().Be(2);
            reflectionResult[0].Should().Be("Hello");
        }
        public void ShouldExecuteOperator_TypeAs()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, Customer>> expression = x => x.Object as Customer;
            Console.WriteLine(expression.ToString());

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

            Customer emitResult = emit.Invoke(customer);
            Customer reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(null);
            reflectionResult.Should().Be(null);
            reflectionResult.Should().Be(emitResult);
        }
예제 #13
0
 public int Calculate(Customer customer)
 {
     return this.CalculateAge() + customer.CalculationValue;
 }
예제 #14
0
 public int CalculateLength(string str, Customer customer, int value)
 {
     return str.Length + customer.CalculationValue + value;
 }
예제 #15
0
 public Customer(Customer toCopy)
 {
     this.calculationValue = toCopy.CalculationValue;
     this.Firstname = toCopy.Firstname;
     this.Lastname = toCopy.Lastname;
 }
        public void ShouldCreateSimpleFunc_PropertyGetter_MethodCall()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, string>> expression = x => x.Firstname.ToLower();
            Console.WriteLine(expression.ToString());

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

            string emitResult = emit.Invoke(customer);
            string reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be("john");
            reflectionResult.Should().Be("john");
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_PropertyGetter_UnaryExpression()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            int arg = 10;
            Expression<Func<Customer, int>> expression = x => -arg;
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(-10);
            reflectionResult.Should().Be(-10);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_CreateNewArray_Bounds()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, string[]>> expression = x => new string[12];
            Console.WriteLine(expression.ToString());

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

            string[] emitResult = emit.Invoke(customer);
            string[] reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Length.Should().Be(12);
            reflectionResult.Length.Should().Be(12);
            reflectionResult.Length.Should().Be(emitResult.Length);
        }
예제 #19
0
 public Customer(string firstname, Customer customer, int amount, string lastname)
 {
     this.Firstname = firstname;
     this.Lastname = lastname;
     this.calculationValue = customer.CalculateAge() + amount;
 }
        public void ShouldCreateSimpleFunc_MethodCall_WithExpressionParameters_NestedNew()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            int value = 666;
            Expression<Func<Customer, int>> expression = x => x.Calculate(new Customer(value));
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(699);
            reflectionResult.Should().Be(699);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_MethodCall_WithExpressionParameters_BinaryExpression_ConstantExpression()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, int>> expression = x => x.Calculate(x.Age + 100);
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(166);
            reflectionResult.Should().Be(166);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_Indexer_MultipleParameters()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            int arg = 5;
            Expression<Func<Customer, int>> expression = x => x[2, arg];
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(40);
            reflectionResult.Should().Be(40);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_Delegate_CallingMethod()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Func<string> func = () => "Hello";
            Expression<Func<Customer, string>> expression = x => func().ToLower();
            Console.WriteLine(expression.ToString());

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

            string emitResult = emit.Invoke(customer);
            string reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be("hello");
            reflectionResult.Should().Be("hello");
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_Delegate_CallingMethod_WithParameter()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Func<string, string> func = x => "hello " + x;
            string s = "test";
            Expression<Func<Customer, string>> expression = x => func(s).Trim('h', 't');
            Console.WriteLine(expression.ToString());

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

            string emitResult = emit.Invoke(customer);
            string reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be("ello tes");
            reflectionResult.Should().Be("ello tes");
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_MethodCall_WithExpressionParameters_NestedDelegateCall_WithParameters_Local()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Func<int, int> method = x => x + 100;
            int arg = 10;
            Expression<Func<Customer, int>> expression = x => x.Calculate(method(arg));
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(143);
            reflectionResult.Should().Be(143);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_ObjectInitializer_WithField()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, Customer>> expression = x => new Customer
            {
                Field = "SoylentGreen",
            };
            Console.WriteLine(expression.ToString());

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

            Customer emitResult = emit.Invoke(customer);
            Customer reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Field.Should().Be("SoylentGreen");
            reflectionResult.Field.Should().Be("SoylentGreen");
        }
        public void ShouldCreateSimpleFunc_MethodCall_WithMixedParameters()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, int>> expression = x => x.CalculateLength(x.Firstname, x, 10);
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(14);
            reflectionResult.Should().Be(14);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_PropertyGetter_BinaryExpression()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, bool>> expression = x => x.Firstname != "Jane";
            Console.WriteLine(expression.ToString());

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

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

            // Assert
            emitResult.Should().BeTrue();
            reflectionResult.Should().BeTrue();
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_StaticMethodCall_Delegate()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Func<int> value = () => 20;
            Expression<Func<Customer, int>> expression = x => Customer.GetDefaultAge(value());
            Console.WriteLine(expression.ToString());

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

            int emitResult = emit.Invoke(customer);
            int reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be(53);
            reflectionResult.Should().Be(53);
            reflectionResult.Should().Be(emitResult);
        }
        public void ShouldCreateSimpleFunc_PropertyGetter_Indexer()
        {
            // Arrange
            Customer customer = new Customer("John", "Doe");
            Expression<Func<Customer, char>> expression = x => x.Firstname[0];
            Console.WriteLine(expression.ToString());

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

            char emitResult = emit.Invoke(customer);
            char reflectionResult = reflection.Invoke(customer);

            // Assert
            emitResult.Should().Be('J');
            reflectionResult.Should().Be('J');
            reflectionResult.Should().Be(emitResult);
        }