public void tryToCreateCSharpCodeWith_Class_Method_WithMethodText()
        {
            Assert.IsEmpty(csharpFastCompiler.InvocationParameters);
            Assert.IsTrue(csharpFastCompiler.ResolveInvocationParametersType);

            var codeSnippet1 = "return 2+2;";
            var codeSnippet2 = "return 2+2; BAD CODE";
            var codeSnippet3 = "return parameterName;";

            //Compile with no Invocation parameters
            var cSharpCode1 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet1);
            var cSharpCode2 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet2);
            var cSharpCode3 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet3);
            var assembly1   = csharpFastCompiler.compileSourceCode_Sync(cSharpCode1);
            var assembly2   = csharpFastCompiler.compileSourceCode_Sync(cSharpCode2);
            var assembly3   = csharpFastCompiler.compileSourceCode_Sync(cSharpCode3);

            Assert.IsTrue(cSharpCode1.valid());
            Assert.IsFalse(cSharpCode2.valid());
            Assert.IsTrue(cSharpCode3.valid());
            Assert.IsNotNull(assembly1);
            Assert.IsNull(assembly2);
            Assert.IsNull(assembly3);

            //Check invocation parameters
            csharpFastCompiler.InvocationParameters.Add("parameterName", "parameterValue");
            var cSharpCode4      = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet3);
            var assembly4        = csharpFastCompiler.compileSourceCode_Sync(cSharpCode4);
            var firstMethod      = assembly4.firstMethod();
            var methodParameters = firstMethod.parameters();
            var firstParameter   = methodParameters.first();
            var parameterName    = firstParameter.Name;
            var parameterType    = firstParameter.ParameterType.fullName();

            Assert.IsTrue(cSharpCode4.valid());
            Assert.IsNotNull(assembly4);
            Assert.IsNotNull(firstMethod);
            Assert.AreEqual(methodParameters.size(), 1);
            Assert.IsNotNull(firstParameter);
            Assert.AreEqual(parameterName, "parameterName");
            Assert.AreEqual(parameterType, "System.String");

            //todo: add code and test for extra return null; (which shouldn't be there when the last statement is a return
        }
Пример #2
0
        public void tryToCreateCSharpCodeWith_Class_Method_WithMethodText()
        {
            Assert.IsEmpty(csharpFastCompiler.invocationParameters());
            Assert.IsTrue(csharpFastCompiler.resolveInvocationParametersType());

            var codeSnippet1 = "return 2+2;";
            var codeSnippet2 = "return 2+2; BAD CODE";
            var codeSnippet3 = "return parameterName;";
            var codeSnippet4 = "var a = 2+2; a.str().info();";

            //Compile with no Invocation parameters
            var cSharpCode1 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet1).assert_Valid();
            var cSharpCode2 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet2).assert_Not_Valid();
            var cSharpCode3 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet3).assert_Valid();
            var cSharpCode4 = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet4).assert_Valid();

            csharpFastCompiler.compileSourceCode_Sync(cSharpCode1).assert_Not_Null();
            csharpFastCompiler.compileSourceCode_Sync(cSharpCode2).assert_Null();
            csharpFastCompiler.compileSourceCode_Sync(cSharpCode3).assert_Null();
            csharpFastCompiler.compileSourceCode_Sync(cSharpCode4).assert_Not_Null();

            //Check invocation parameters
            csharpFastCompiler.invocationParameters().Add("parameterName", "parameterValue");
            var cSharpCode5      = csharpFastCompiler.tryToCreateCSharpCodeWith_Class_Method_WithMethodText(codeSnippet3);
            var assembly4        = csharpFastCompiler.compileSourceCode_Sync(cSharpCode5);
            var firstMethod      = assembly4.firstMethod();
            var methodParameters = firstMethod.parameters();
            var firstParameter   = methodParameters.first();
            var parameterName    = firstParameter.Name;
            var parameterType    = firstParameter.ParameterType.fullName();

            Assert.IsTrue(cSharpCode5.valid());
            Assert.IsNotNull(assembly4);
            Assert.IsNotNull(firstMethod);
            Assert.AreEqual(methodParameters.size(), 1);
            Assert.IsNotNull(firstParameter);
            Assert.AreEqual(parameterName, "parameterName");
            Assert.AreEqual(parameterType, "System.String");

            //Check that return doesn't exist when the last line is a return
        }