Skip to content

Commit

Permalink
Merge pull request #18 from bruce-dunwiddie/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
bruce-dunwiddie authored Sep 1, 2019
2 parents 43d5e84 + 0d296b2 commit 3d635ad
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 388 deletions.
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
################################################################################

/Data_Eval/.vs/
/Data_Eval/Data_Eval/bin
/Data_Eval/Data_Eval/obj
/Data_Eval/*/bin
/Data_Eval/*/obj
/Data_Eval/packages
/Data_Eval/Tests/bin
/Data_Eval/Tests/obj
/Data_Eval/TestExternalReference/bin
/Data_Eval/TestExternalReference/obj
/Data_Eval/Tests/.sonarqube
*.user
*.nupkg
4 changes: 2 additions & 2 deletions Data_Eval/Data_Eval/CodeWriting/CSharpCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public string GetClassTextWithReturn(
foreach (Variable variable in variables)
{
classText.AppendFormat(
"\tprivate {0} {1};\r\n",
"\tpublic {0} {1};\r\n",
formatter.GetFullName(variable.Type),
variable.Name);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public string GetClassTextWithNoReturn(
foreach (Variable variable in variables)
{
classText.AppendFormat(
"\tprivate {0} {1};\r\n",
"\tpublic {0} {1};\r\n",
formatter.GetFullName(variable.Type),
variable.Name);
}
Expand Down
6 changes: 6 additions & 0 deletions Data_Eval/Data_Eval/Compilation/CompilationException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public CompilationException(string message, Exception innerException)

}

private CompilationException(SerializationInfo info, StreamingContext context)
: base(info, context)
{

}

[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
Expand Down
25 changes: 9 additions & 16 deletions Data_Eval/Data_Eval/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Data.Eval
{
public sealed class Evaluator
{
private string expression;
private Dictionary<string, Variable> variables = new Dictionary<string, Variable>();
private List<string> references = new List<string>();
private List<string> usings = new List<string>();
private List<string> methods = new List<string>();
private readonly string expression;
private readonly Dictionary<string, Variable> variables = new Dictionary<string, Variable>();
private readonly List<string> references = new List<string>();
private readonly List<string> usings = new List<string>();
private readonly List<string> methods = new List<string>();
private bool initialized = false;
private Execution execution = null;
private bool callerInitialized = false;
Expand Down Expand Up @@ -50,11 +50,6 @@ public void SetVariable(
{
// TODO: check variable naming standards

if (variables == null)
{
variables = new Dictionary<string, Variable>();
}

if (variables.ContainsKey(name))
{
variables[name].Value = value;
Expand Down Expand Up @@ -309,16 +304,16 @@ public T Eval<T>()

public static object Eval(string expression)
{
string caller = Assembly.GetCallingAssembly().Location;
string callerLocation = Assembly.GetCallingAssembly().Location;

return new Evaluator(expression).EvalInternal(caller);
return new Evaluator(expression).EvalInternal(callerLocation);
}

public static T Eval<T>(string expression)
{
string caller = Assembly.GetCallingAssembly().Location;
string callerLocation = Assembly.GetCallingAssembly().Location;

return (T) new Evaluator(expression).EvalInternal(caller);
return (T) new Evaluator(expression).EvalInternal(callerLocation);
}

public void Exec()
Expand Down Expand Up @@ -382,8 +377,6 @@ private sealed class Execution

public Func<object> Constructor = null;

public Type Type = null;

public Dictionary<string, ExecutionVariable> Variables = new Dictionary<string, ExecutionVariable>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,5 @@ public Func<object> GetFunc(

return func;
}

// http://stackoverflow.com/questions/390578/creating-instance-of-type-without-default-constructor-in-c-sharp-using-reflectio/16162475#16162475

//public static class New<T>
//{
// public static readonly Func<T> Instance = Creator();

// static Func<T> Creator()
// {
// Type t = typeof(T);
// if (t == typeof(string))
// return Expression.Lambda<Func<T>>(Expression.Constant(string.Empty)).Compile();

// if (t.HasDefaultConstructor())
// return Expression.Lambda<Func<T>>(Expression.New(t)).Compile();

// return () => (T)FormatterServices.GetUninitializedObject(t);
// }
//}

//public static bool HasDefaultConstructor(this Type t)
//{
// return t.IsValueType || t.GetConstructor(Type.EmptyTypes) != null;
//}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,10 @@ public Func<object, object[], object> GetFuncWithReturn(

ParameterExpression allParameters = Expression.Parameter(typeof(object[]), "params");

ParameterInfo[] methodParameters = method.GetParameters();

List<Expression> parameters = new List<Expression>();

for (int i = 0; i < methodParameters.Length; i++)
{
ParameterInfo parameter = methodParameters[i];

ConstantExpression indexExpr = Expression.Constant(i);

BinaryExpression item = Expression.ArrayIndex(
allParameters,
indexExpr);

UnaryExpression converted = Expression.Convert(
item,
parameter.ParameterType);

parameters.Add(converted);
}

Expression methodExp = Expression.Call(
Expression.Convert(instance, method.DeclaringType),
method,
parameters.ToArray());
new Expression[] { });

// http://stackoverflow.com/questions/8974837/expression-of-type-system-datetime-cannot-be-used-for-return-type-system-obje
if (methodExp.Type.IsValueType)
Expand Down Expand Up @@ -70,31 +49,10 @@ public Action<object, object[]> GetFuncWithNoReturn(

ParameterExpression allParameters = Expression.Parameter(typeof(object[]), "params");

ParameterInfo[] methodParameters = method.GetParameters();

List<Expression> parameters = new List<Expression>();

for (int i = 0; i < methodParameters.Length; i++)
{
ParameterInfo parameter = methodParameters[i];

ConstantExpression indexExpr = Expression.Constant(i);

BinaryExpression item = Expression.ArrayIndex(
allParameters,
indexExpr);

UnaryExpression converted = Expression.Convert(
item,
parameter.ParameterType);

parameters.Add(converted);
}

Expression methodExp = Expression.Call(
Expression.Convert(instance, method.DeclaringType),
method,
parameters.ToArray());
new Expression[] { });

Expression<Action<object, object[]>> methodCall = Expression.Lambda<Action<object, object[]>>(
methodExp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Func<object, object> GetFunc(
{
FieldInfo member = instanceType.GetField(
memberName,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
BindingFlags.Public | BindingFlags.Instance);

ParameterExpression instance = Expression.Parameter(typeof(object), "i");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Action<object, object> GetAction(
{
FieldInfo member = instanceType.GetField(
memberName,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
BindingFlags.Public | BindingFlags.Instance);

ParameterExpression instance = Expression.Parameter(typeof(object), "i");
ParameterExpression argument = Expression.Parameter(typeof(object), "a");
Expand Down
3 changes: 3 additions & 0 deletions Data_Eval/Tests/CodeCoverage.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%userprofile%\.nuget\packages\opencover\4.7.922\tools\OpenCover.Console.exe -target:"%ProgramFiles%\dotnet\dotnet.exe" -targetargs:"test Tests.csproj" -filter:"+[*Eval*]*" -excludebyattribute:"System.CodeDom.Compiler.GeneratedCodeAttribute" -register:user -output:"bin\Debug\CodeCoverageResult.xml" -oldStyle
%userprofile%\.nuget\packages\reportgenerator\4.2.17\tools\net47\ReportGenerator.exe "-reports:bin\Debug\CodeCoverageResult.xml" "-targetdir:bin\Debug\CodeCoverageReport"
pause
9 changes: 9 additions & 0 deletions Data_Eval/Tests/CodeQuality.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
rem run CodeCoverage.bat first

SonarScanner.MSBuild.exe begin /k:"data-eval" /d:sonar.organization="bruce-dunwiddie-github" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.login=%SONARQUBE_TOKEN% /d:sonar.cs.opencover.reportsPaths="bin\Debug\CodeCoverageResult.xml"

"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MsBuild.exe" ..\Data_Eval.sln /t:Rebuild

SonarScanner.MSBuild.exe end /d:sonar.login=%SONARQUBE_TOKEN%

pause
6 changes: 3 additions & 3 deletions Data_Eval/Tests/CodeWriting/CSharpCodeWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using Data.Eval.CodeWriting;

using Tests.Properties;
using Tests.Resources;

namespace Tests.CodeWriting
{
Expand All @@ -31,7 +31,7 @@ public void CSharpCodeWriter_SimpleExpression()
Assert.AreEqual(
// line ending types don't matter.
// making sure tests work on Windows and *nix platforms.
Resources.CSharpSimpleExpression.Replace("\r\n", "\n"),
ResourceReader.CSharpSimpleExpression.Replace("\r\n", "\n"),
classText.Replace("\r\n", "\n"));
}

Expand All @@ -56,7 +56,7 @@ public void CSharpCodeWriter_SimpleVariable()
methods: new List<string> { });

Assert.AreEqual(
Resources.CSharpSimpleVariable.Replace("\r\n", "\n"),
ResourceReader.CSharpSimpleVariable.Replace("\r\n", "\n"),
classText.Replace("\r\n", "\n"));
}
}
Expand Down
33 changes: 29 additions & 4 deletions Data_Eval/Tests/Compilation/CompilerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using Data.Eval.Compilation;

using Tests.Properties;
using Tests.Resources;

namespace Tests.Compilation
{
Expand All @@ -21,7 +21,7 @@ public void Compiler_CSharpSimpleExpression()
var compiler = new Compiler();

Type newType = compiler.Compile(
Resources.CSharpSimpleExpression);
ResourceReader.CSharpSimpleExpression);

Assert.IsNotNull(newType);
}
Expand All @@ -32,7 +32,7 @@ public void Compiler_CSharpSimpleVariable()
var compiler = new Compiler();

Type newType = compiler.Compile(
Resources.CSharpSimpleVariable);
ResourceReader.CSharpSimpleVariable);

Assert.IsNotNull(newType);
}
Expand All @@ -43,9 +43,34 @@ public void Compiler_CSharpNullableInt()
var compiler = new Compiler();

Type newType = compiler.Compile(
Resources.CSharpNullableInt);
ResourceReader.CSharpNullableInt);

Assert.IsNotNull(newType);
}

[Test]
public void Compiler_Exception()
{
var compiler = new Compiler();

string codeToCompile = @"
using System;
public sealed class CustomEvaluator{
public System.Int32? intValue;
public object Eval(){
return intValue + 1
}
}";

CompilationException ex = Assert.Throws<CompilationException>(
delegate
{
compiler.Compile(codeToCompile);
});

Assert.AreEqual("Class failed to compile.\n\tLine 6: ; expected", ex.Message);
Assert.AreEqual(codeToCompile, ex.GeneratedClassCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,42 +35,9 @@ public void SetInstanceMemberValueExpression_Int()
example.IntValue);
}

[Test]
public void SetInstanceMemberValueExpression_PrivateInt()
{
var example = new ExampleClass();

var action = new SetInstanceMemberValueExpression()
.GetAction(
typeof(ExampleClass),
"PrivateIntValue");

action(
example,
3);

Assert.AreEqual(
3,
example.GetPrivateIntValue());
}

public class ExampleClass
{
public int IntValue;

// warning CS0649: Field 'SetInstanceMemberValueExpressionTests.ExampleClass.PrivateIntValue'
// is never assigned to, and will always have its default value 0

#pragma warning disable 0649

private int PrivateIntValue;

#pragma warning restore 0649

public int GetPrivateIntValue()
{
return PrivateIntValue;
}
}
}
}
Loading

0 comments on commit 3d635ad

Please sign in to comment.