private dynamic CompileAndGetClassWith(string functionCode) { var classCode = string.Format(@" public class Test {0} end ", functionCode).Trim(); return(CompilationHelper.CompileAndGetInstance(classCode, "Test")); }
public void ReturnThis() { var code = string.Format(@" public class Test public function GetThis() return this end end ").Trim(); var instance = CompilationHelper.CompileAndGetInstance(code, "Test"); var value = instance.GetThis(); Assert.AreEqual(instance, value); }
public void ReturnPropertyThroughThis() { var code = string.Format(@" public class Test private integer value = 5 public function GetValue() return this.value end end ").Trim(); var instance = CompilationHelper.CompileAndGetInstance(code, "Test"); var value = instance.GetValue(); Assert.AreEqual(new Integer(5), value); }
public void ExternalMethodCallOnValueFromNearbyCall() { var caller = CompilationHelper.CompileAndGetInstance(string.Format(@" public class Callee function GetValue() return 'abc' end end public class Caller function GetValueFromCallee() let callee = new Callee() return callee.GetValue().Substring((1).ToInt32()) end end ").Trim(), "Caller"); Assert.AreEqual("bc", caller.GetValueFromCallee()); }
public void NearbyMethodCallOnNewObject(string parameters, string returnValue, string arguments, object expectedValue) { var caller = CompilationHelper.CompileAndGetInstance(string.Format(@" public class Callee function GetValue({0}) return {2} end end public class Caller function GetValueFromCallee() let callee = new Callee() return callee.GetValue({1}) end end ", parameters, arguments, returnValue).Trim(), "Caller"); Assert.AreEqual(TestArgumentConverter.Convert(expectedValue), caller.GetValueFromCallee()); }
public void WriteAndReadPropertyThroughMethods <T>(string propertyType, object rawValue) { var value = (T)TestArgumentConverter.Convert(rawValue); var code = string.Format(@" public class Test private {0} x public function SetValue({0} value) x = value end public function GetValue() return x end end ", propertyType).Trim(); var instance = CompilationHelper.CompileAndGetInstance(code, "Test"); instance.SetValue(value); Assert.AreEqual(value, instance.GetValue()); }
public void Problem2_FindSumOfEvenValuedFibonacciNumbers_Under4000000() { var code = @" import System import System.Linq public class Problem2 decimal sqrtOf5 = Math.Sqrt((5.0).ToDouble()).ToDecimal() decimal ψ = (1.0-sqrtOf5)/2.0 decimal φ = (1.0+sqrtOf5)/2.0 function Fibonacci(integer n) return Math.Round(((φ ** n.ToDecimal() - ψ ** n.ToDecimal()) / sqrtOf5).ToDouble()).ToInteger() end function Evaluate() return (1..10000000).Select(Fibonacci).TakeWhile(x => x < 4000000).Where(x => x mod 2 == 0).Sum() end end ".Trim(); var instance = CompilationHelper.CompileAndGetInstance(code, "Problem2"); Assert.AreEqual <object>(new Integer(4613732), instance.Evaluate()); }