Ejemplo n.º 1
0
        public void AssignShouldAcceptRightableAndLeftable()
        {
            const string expected =
                @"public System.String Call(System.String par)
{
  par = ""another"";
  return par;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("par")
                                .WithBody(
                CodeLine.Assign(Operation.Variable("par"), Operation.Constant("another"))
                )
                                .Returns("par");

            var newSource = newExpression.ToString();

            AssertString.AreEqual(expected, newSource);

            var lambda = newExpression.ToLambda <Func <string, string> >();

            Assert.IsNotNull(lambda);

            var result = lambda("test");

            Assert.AreEqual("another", result);
        }
Ejemplo n.º 2
0
        public void AssignShouldWorkForInternalVariables()
        {
            const string expected =
                @"public System.String Call(System.String par)
{
  System.String var;
  var = ""another"";
  var += par;
  return var;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("par")
                                .WithBody(
                CodeLine.CreateVariable <string>("var"),
                CodeLine.Assign("var", Operation.Constant("another")),
                CodeLine.Assign(Operation.Variable("var"), Operation.Variable("par"), AssignementOperator.SumAssign)
                )
                                .Returns("var");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string> >();

            Assert.IsNotNull(lambda);

            var result = lambda("test");

            Assert.AreEqual("anothertest", result);
        }
Ejemplo n.º 3
0
        public void AssignShouldAssignObjects()
        {
            const string expected =
                @"public ExpressionBuilder.Test.TestObject Call(ExpressionBuilder.Test.TestObject first, ExpressionBuilder.Test.TestObject second)
{
  first = second;
  return first;
}";

            var newExpression = Function.Create()
                                .WithParameter <TestObject>("first")
                                .WithParameter <TestObject>("second")
                                .WithBody(
                CodeLine.Assign("first", "second")
                )
                                .Returns("first");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <TestObject, TestObject, TestObject> >();

            Assert.IsNotNull(lambda);

            var expectedResult = new TestObject();
            var result         = lambda(new TestObject(), expectedResult);

            Assert.AreSame(expectedResult, result);
        }
        public void ItShouldPossibleToInvokeStringFormat()
        {
            const string expected =
                @"public System.String Call(System.String par1, System.String par2)
{
  System.String result;
  result = System.String.Format(""{0}-{1}"", par1, par2);
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("par1")
                                .WithParameter <string>("par2")
                                .WithBody(
                CodeLine.CreateVariable <string>("result"),
                CodeLine.Assign(Operation.Variable("result"),
                                StringOperation.Format(
                                    "{0}-{1}",
                                    Operation.Variable("par1"),
                                    Operation.Variable("par2")))
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string, string> >();

            Assert.IsNotNull(lambda);
            var result = lambda("a", "b");

            Assert.AreEqual("a-b", result);
        }
Ejemplo n.º 5
0
        public void AssignShouldAssign()
        {
            const string expected =
                @"public System.String Call(System.String first, System.String second)
{
  first = second;
  return first;
}";

            var newExpression = Function.Create()
                                .WithParameter(typeof(string), "first")
                                .WithParameter <string>("first")
                                .WithParameter <string>("second")
                                .WithBody(
                CodeLine.Assign("first", "second"),
                CodeLine.Assign("FirstName", Operation.Get("person.icdoperson.firstname"))
                )
                                .Returns("first");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string, string> >();

            Assert.IsNotNull(lambda);

            var result = lambda("test", "another");

            Assert.AreEqual("another", result);
        }
Ejemplo n.º 6
0
        public void ItShouldPossibleToInvokeMethodsOnObjects()
        {
            const string expected =
                @"public System.String Call(ExpressionBuilder.Test.SimpleObject par)
{
  System.String result;
  result = par.GetName();
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <SimpleObject>("par")
                                .WithBody(
                CodeLine.CreateVariable <string>("result"),
                CodeLine.Assign("result", Operation.InvokeReturn("par", "GetName"))
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <SimpleObject, string> >();

            Assert.IsNotNull(lambda);
            var so     = new SimpleObject();
            var result = lambda(so);

            Assert.AreEqual("SimpleObject", result);
        }
Ejemplo n.º 7
0
        public void CastShouldCastSpecifiyingType()
        {
            const string expected =
                @"public System.Int64 Call(System.Int32 first, System.Int64 second)
{
  second = ((System.Int64)2);
  second += ((System.Int64)first);
  return second;
}";

            var newExpression = Function.Create()
                                .WithParameter <int>("first")
                                .WithParameter <long>("second")
                                .WithBody(
                CodeLine.Assign("second", Operation.CastConst(2, typeof(long))),
                CodeLine.Assign("second", Operation.Cast("first", typeof(long)), AssignementOperator.SumAssign)
                )
                                .Returns("second");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <int, long, long> >();

            Assert.IsNotNull(lambda);

            var result = lambda(3, 7);

            Assert.AreEqual(5, result);
        }
Ejemplo n.º 8
0
        public void ItShouldPossibleToInvokeStaticMethodsOnObjects()
        {
            const string expected =
                @"public System.Boolean Call(System.Int32 year)
{
  System.Boolean result;
  result = System.DateTime.IsLeapYear(year);
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <int>("year")
                                .WithBody(
                CodeLine.CreateVariable <bool>("result"),
                CodeLine.Assign("result", Operation.InvokeReturn <DateTime>("IsLeapYear", Operation.Variable("year")))
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <int, bool> >();

            Assert.IsNotNull(lambda);
            var result = lambda(2011);

            Assert.IsFalse(result);
            result = lambda(2012);
            Assert.IsTrue(result);
        }
        public void ItShouldPossibleToInvokeStringCompare()
        {
            const string expected =
                @"public System.Int32 Call(System.String par1, System.String par2)
{
  System.Int32 result;
  result = System.String.Compare(par1, par2, CurrentCulture);
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("par1")
                                .WithParameter <string>("par2")
                                .WithBody(
                CodeLine.CreateVariable <int>("result"),
                CodeLine.Assign(Operation.Variable("result"), StringOperation.Compare("par1", "par2"))
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string, int> >();

            Assert.IsNotNull(lambda);
            var result = lambda("a", "a");

            Assert.AreEqual(0, result);
            result = lambda("a", "b");
            Assert.AreEqual(-1, result);
        }
Ejemplo n.º 10
0
        public void ItShouldPossibleToInvokeConstructorWithParameters()
        {
            const string expected =
                @"public System.String Call(System.String name)
{
  System.String result;
  ExpressionBuilder.Test.SimpleObject par;
  par = new ExpressionBuilder.Test.SimpleObject(name);
  result = par.GetName();
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("name")
                                .WithBody(
                CodeLine.CreateVariable <string>("result"),
                CodeLine.CreateVariable <SimpleObject>("par"),
                CodeLine.Assign("par", Operation.CreateInstance <SimpleObject>(Operation.Variable("name"))),
                CodeLine.Assign("result", Operation.InvokeReturn("par", "GetName"))
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string> >();

            Assert.IsNotNull(lambda);
            var result = lambda("paramName");

            Assert.AreEqual("paramName", result);
        }
        public void ItShouldPossibleToInvokeToString()
        {
            const string expected =
                @"public System.String Call(System.Int32 par)
{
  System.String result;
  result = par.ToString();
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <int>("par")
                                .WithBody(
                CodeLine.CreateVariable <string>("result"),
                CodeLine.Assign(Operation.Variable("result"), StringOperation.ToString("par"))
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <int, string> >();

            Assert.IsNotNull(lambda);
            var result = lambda(1);

            Assert.AreEqual("1", result);
        }
Ejemplo n.º 12
0
        public void SumAssignShouldWorkForStrings()
        {
            const string expected =
                @"public System.String Call(System.String first, System.String second)
{
  first += second;
  return first;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("first")
                                .WithParameter <string>("second")
                                .WithBody(
                CodeLine.Assign("first", "second", AssignementOperator.SumAssign)
                )
                                .Returns("first");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string, string> >();

            Assert.IsNotNull(lambda);

            var result = lambda("test", "another");

            Assert.AreEqual("testanother", result);
        }
Ejemplo n.º 13
0
        public void SubtractAssignShouldWorkForValueTypes()
        {
            const string expected =
                @"public System.Int32 Call(System.Int32 first, System.Int32 second)
{
  first -= second;
  return first;
}";

            var newExpression = Function.Create()
                                .WithParameter <int>("first")
                                .WithParameter <int>("second")
                                .WithBody(
                CodeLine.Assign("first", "second", AssignementOperator.SubtractAssign)
                )
                                .Returns("first");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <int, int, int> >();

            Assert.IsNotNull(lambda);

            var result = lambda(4, 2);

            Assert.AreEqual(2, result);
        }
Ejemplo n.º 14
0
        public void ReturnShouldBeHandledCorrectlyInsideIfs()
        {
            const string expected =
                @"public System.String Call(System.String par)
{
  System.String result;
  if(par == ""then"")
  {
   result = ""then"";
   return result;
  }  
  else
  {
   if(par == ""elseif"")
   {
    result = ""elseif"";
    return result;
   }   
   else
   {
    result = ""else"";
    return result;
   };
  };
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("par")
                                .WithBody(
                CodeLine.CreateVariable <string>("result"),
                CodeLine.CreateIf(Condition.CompareConst("par", "then"))
                .Then(
                    CodeLine.AssignConstant("result", "then"),
                    CodeLine.Return())
                .ElseIf(Condition.Compare("par", Operation.Constant("elseif")))
                .Then(
                    CodeLine.AssignConstant("result", "elseif"),
                    CodeLine.Return())
                .Else(
                    CodeLine.AssignConstant("result", "else"),
                    CodeLine.Return())
                )
                                .Returns("result");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string> >();

            Assert.IsNotNull(lambda);

            var result = lambda("then");

            Assert.AreEqual("then", result);
            result = lambda("elseif");
            Assert.AreEqual("elseif", result);
            result = lambda("else");
            Assert.AreEqual("else", result);
        }
Ejemplo n.º 15
0
        public void CreateVariablesShouldWorkInsideWhileIfAndThen()
        {
            const string expected =
                @"public void Call(System.String par)
{
  System.Int32 count;
  count = 1;
  while(count == 0)
  {
   if(True == True)
   {
    System.String var;
    var = par;
   }   
   else
   {
    System.String var;
    var = par;
   };
  };
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("par")
                                .WithBody(
                CodeLine.CreateVariable <int>("count"),
                CodeLine.AssignConstant("count", 1),
                CodeLine.CreateWhile(Condition.CompareConst("count", 0))
                .Do(
                    CodeLine.CreateIf(Condition.CompareConst(Operation.Constant(true), true))
                    .Then(
                        CodeLine.CreateVariable <string>("var"),
                        CodeLine.Assign("var", "par")
                        )
                    .ElseIf(Condition.CompareConst(Operation.Constant(true), true))
                    .Then(
                        CodeLine.CreateVariable <string>("var"),
                        CodeLine.Assign("var", "par")
                        )
                    .Else(
                        CodeLine.CreateVariable <string>("var"),
                        CodeLine.Assign("var", "par")
                        )
                    )
                );

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Action <string> >();

            Assert.IsNotNull(lambda);

            lambda("test");
        }
Ejemplo n.º 16
0
        public void WhileLoopShouldAllowForcedReturn()
        {
            const string expected =
                @"public System.Int32 Call(System.Int32 par)
{
  System.Int32 first;
  first = 0;
  while(first < par)
  {
   first += 1;
   if(first >= 10)
   {
    return first;
   };
  };
  return first;
}";

            var newExpression = Function.Create()
                                .WithParameter <int>("par")
                                .WithBody(
                CodeLine.CreateVariable <int>("first"),
                CodeLine.AssignConstant("first", 0),
                CodeLine.CreateWhile(Condition.Compare("first", "par", ComparaisonOperator.Smaller))
                .Do(
                    CodeLine.AssignConstant("first", Operation.Constant(1), AssignementOperator.SumAssign),
                    CodeLine.CreateIf(Condition.CompareConst("first", 10, ComparaisonOperator.GreaterEqual))
                    .Then(
                        CodeLine.Return()
                        ))
                )
                                .Returns("first");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <int, int> >();

            Assert.IsNotNull(lambda);

            var result = lambda(4);

            Assert.AreEqual(4, result);

            result = lambda(20);
            Assert.AreEqual(10, result);
        }
Ejemplo n.º 17
0
        public void CanBeCreatedAFunctionWithoutParametersAndReturn()
        {
            const string expected =
                @"public void Call()
{
  //No Operation;
}";
            var newExpression = Function.Create()
                                .WithBody(CodeLine.Nop);

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Action>();

            Assert.IsNotNull(lambda);

            lambda();
        }
Ejemplo n.º 18
0
        public void CanBeCreatedAFunctionReturningVoid()
        {
            const string expected =
                @"public void Call(System.String first, System.String second)
{
  //No Operation;
}";
            var newExpression = Function.Create()
                                .WithParameter <string>("first")
                                .WithParameter <string>("second")
                                .WithBody(CodeLine.Nop);

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Action <string, string> >();

            Assert.IsNotNull(lambda);

            lambda("test", "another");
        }
Ejemplo n.º 19
0
        public void ShouldBePossibleToCallLambdaFunction()
        {
            const string expected =
                @"public System.String Call(System.String first, System.String second)
{
  System.String result;
  result = System.Func<System.Object, System.String, System.String>(first, second);
  return result;
}";

            var newExpression = Function.Create()
                                .WithParameter <string>("first")
                                .WithParameter <string>("second")
                                .WithBody(
                CodeLine.CreateVariable <string>("result"),
                CodeLine.Assign("result",
                                Operation.Func <object, string, string>(
                                    (a, b) =>
            {
                b += "-extra";
                return(string.Format("{0}-{1}", a, b));
            },
                                    Operation.Variable("first"),
                                    Operation.Variable("second"))
                                )
                )
                                .Returns("result");


            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string, string> >();

            Assert.IsNotNull(lambda);

            var result = lambda("a", "b");

            Assert.AreEqual("a-b-extra", result);
        }
Ejemplo n.º 20
0
        public void ItShouldBePossibleToDeclareParametersAndReturn()
        {
            const string expected =
                @"public System.String Call(System.String first, System.String second)
{
  //No Operation;
  return first;
}";
            var newExpression = Function.Create()
                                .WithParameter <string>("first")
                                .WithParameter <string>("second")
                                .WithBody(CodeLine.Nop)
                                .Returns("first");

            AssertString.AreEqual(expected, newExpression.ToString());

            var lambda = newExpression.ToLambda <Func <string, string, string> >();

            Assert.IsNotNull(lambda);

            Assert.AreEqual("test", lambda("test", "another"));
        }