Skip to content

Commit

Permalink
Fixed an issue with ArrayLength expression for null arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe Gyllebring committed Jul 18, 2018
1 parent 7216592 commit 9251645
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
10 changes: 8 additions & 2 deletions Source/Cone/Core/EvaluationResult.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq.Expressions;

namespace Cone.Core
Expand Down Expand Up @@ -47,5 +47,11 @@ class EvaluationError
}

public EvaluationResult Then<T>(Func<T, EvaluationResult> next) { return Then(x => next((T)x.Result)); }
}

public override string ToString() {
if(IsNull)
return "null";
return value.ToString();
}
}
}
10 changes: 8 additions & 2 deletions Source/Cone/Core/ExpressionEvaluatorContext.cs
Expand Up @@ -78,11 +78,17 @@ class ExpressionEvaluatorContext
var index = (int)Evaluate(rank1.Right).Result;
if(index >= 0 && index <= array.Length)
return Success(rank1.Type, array.GetValue(index));
return Failure(rank1, new IndexOutOfRangeException());
return Failure(rank1, new IndexOutOfRangeException($"Tried to access element {index} target has only {array.Length} elements."));
}

EvaluationResult ArrayLength(Expression expression) {
var array = (Array)Evaluate(((UnaryExpression)expression).Operand).Result;
var target = Evaluate(((UnaryExpression)expression).Operand);
if(target.IsNull)
return Failure(expression,
((UnaryExpression)expression).Operand is MemberExpression x
? new NullSubexpressionException(x.Member.Name, expression, context)
: new NullSubexpressionException(expression, context));
var array = (Array)target.Result;
return Success(expression.Type, array.Length);
}

Expand Down
9 changes: 7 additions & 2 deletions Source/Cone/NullSubexpressionException.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq.Expressions;

namespace Cone
Expand All @@ -8,7 +8,12 @@ public class NullSubexpressionException : ArgumentNullException
readonly Expression context;
readonly Expression expression;

public NullSubexpressionException(Expression expression, Expression context) {
public NullSubexpressionException(string paramName, Expression expression, Expression context) : base(paramName) {
this.context = expression;
this.expression = context;
}

public NullSubexpressionException(Expression expression, Expression context) {
this.context = expression;
this.expression = context;
}
Expand Down
10 changes: 9 additions & 1 deletion Source/Cone/Runners/ConesoleRunner.cs
Expand Up @@ -28,7 +28,7 @@ public class ConesoleRunner

if (!config.NoLogo)
logger.WriteInfo(x => {
x.Info("Cone {0}\n", runner.GetType().Assembly.GetName().Version.ToString(3));
x.Info("Cone {0} on {1}\n", runner.GetType().Assembly.GetName().Version.ToString(3), GetRuntimeHostName());
x.Write(" " + string.Join(", ", config.AssemblyPaths) + "\n");
});

Expand All @@ -53,6 +53,14 @@ public class ConesoleRunner
return results.FailureCount;
}

static string GetRuntimeHostName() {
var pathParts = Path.GetFullPath(new Uri(typeof(object).Assembly.CodeBase).LocalPath).Split(new[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
var n = Array.IndexOf(pathParts, "Microsoft.NETCore.App");
if(n != -1)
return $"{pathParts[n]} {pathParts[n + 1]}";
return $".NET Framework {Version.Parse(((AssemblyFileVersionAttribute)typeof(object).Assembly.GetCustomAttribute(typeof(AssemblyFileVersionAttribute))).Version).ToString(3)}";
}

static TestSession CreateTestSession(ISessionLogger logger, WorkerConfiguration config) {
return new TestSession(logger) {
IncludeSuite = config.IncludeSuite,
Expand Down
1 change: 1 addition & 0 deletions Source/dotnet-conesole/Program.cs
Expand Up @@ -63,6 +63,7 @@ class CommandSettings
Console.WriteLine("usage is: dotnet conesole [--no-build] [-- <conesole settings>]");
return -1;
}

try {
var targetInfo = GetTargetInfo(settings);
var allOk = true;
Expand Down
7 changes: 6 additions & 1 deletion Specs/Cone.Specs/ExpressionEvaluatorSpec.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -51,6 +51,11 @@ public class ExpressionEvaluatorSpec
Check<NullSubexpressionException>.When(() => foo.ThisValueIsNull.Contains("foo"));
}

public void null_check_array_expression() {
var foo = new { ThisValueIsNull = (string[])null };
Check<NullSubexpressionException>.When(() => foo.ThisValueIsNull.Length == 3);
}

public void new_expression_propagates_correct_exception() {
Check<ArgumentNullException>.When(() => new List<object>(null));
}
Expand Down

0 comments on commit 9251645

Please sign in to comment.