[gh-88] Added limit on variables values (removed total limit for now).

This commit is contained in:
Andrey Shchekin 2017-07-22 11:58:10 +12:00
Родитель de8b5f7976
Коммит 09024f2b40
5 изменённых файлов: 28 добавлений и 21 удалений

Просмотреть файл

@ -6,11 +6,13 @@ using System.Text;
namespace SharpLab.Runtime.Internal {
public static class Flow {
private const int MaxReportLength = 20;
private const int MaxVariableNameLength = 10;
private const int MaxReportEnumerableItemCount = 3;
private const int MaxReportStepNotesPerLineCount = 3;
private const int MaxReportVariablesPerStepCount = 3;
private static class ReportLimits {
public const int MaxVariableNameLength = 10;
public const int MaxVariableValueLength = 10;
public const int MaxEnumerableItems = 3;
public const int MaxStepNotesPerLine = 3;
public const int MaxVariablesPerStep = 3;
}
private static readonly IDictionary<int, int> _stepNotesCountPerLine = new Dictionary<int, int>();
private static readonly List<Step> _steps = new List<Step>();
@ -29,19 +31,21 @@ namespace SharpLab.Runtime.Internal {
countPerLine += 1;
_stepNotesCountPerLine[step.LineNumber] = countPerLine;
if (countPerLine == MaxReportStepNotesPerLineCount + 1) {
if (countPerLine == ReportLimits.MaxStepNotesPerLine + 1) {
step.Notes = new StringBuilder("…");
_steps[_steps.Count - 1] = step;
return;
}
}
if (countPerLine >= MaxReportStepNotesPerLineCount + 1)
if (countPerLine >= ReportLimits.MaxStepNotesPerLine + 1)
return;
step.VariableCount += 1;
if (step.VariableCount > MaxReportVariablesPerStepCount + 1)
if (step.VariableCount > ReportLimits.MaxVariablesPerStep + 1) {
_steps[_steps.Count - 1] = step;
return;
}
var notes = step.Notes;
if (notes == null) {
@ -52,12 +56,12 @@ namespace SharpLab.Runtime.Internal {
if (notes.Length > 0)
notes.Append(", ");
if (step.VariableCount == MaxReportVariablesPerStepCount + 1) {
if (step.VariableCount == ReportLimits.MaxVariablesPerStep + 1) {
notes.Append("…");
return;
}
AppendString(notes, name, MaxVariableNameLength);
AppendString(notes, name, ReportLimits.MaxVariableNameLength);
notes.Append(": ");
AppendValue(notes, value);
// Have to reassign in case we set Notes
@ -77,7 +81,7 @@ namespace SharpLab.Runtime.Internal {
switch (value) {
case IList<int> e: return AppendEnumerable(builder, e);
case ICollection e: return AppendEnumerable(builder, e.Cast<object>());
default: return AppendString(builder, value.ToString(), MaxReportLength - builder.Length);
default: return AppendString(builder, value.ToString(), ReportLimits.MaxVariableValueLength);
}
}
@ -88,7 +92,7 @@ namespace SharpLab.Runtime.Internal {
if (index > 0)
builder.Append(", ");
if (index > MaxReportEnumerableItemCount) {
if (index > ReportLimits.MaxEnumerableItems) {
builder.Append("…");
break;
}
@ -101,9 +105,6 @@ namespace SharpLab.Runtime.Internal {
}
private static StringBuilder AppendString(StringBuilder builder, string value, int limit) {
if (limit <= 0)
return builder;
if (value.Length <= limit) {
builder.Append(value);
}

Просмотреть файл

@ -38,7 +38,8 @@ namespace SharpLab.Tests {
[Theory]
[InlineData("Loop.For.10Iterations.cs", 3, "i: 0; i: 1; i: 2; …")]
[InlineData("Variable.MultipleDeclarationsOnTheSameLine.cs", 3, "a: 0, b: 0, c: 0, …")]
[InlineData("Variable.VeryLongName.cs", 3, "whyMyVari…: 0")]
[InlineData("Variable.LongName.cs", 3, "abcdefghi…: 0")]
[InlineData("Variable.LongValue.cs", 3, "x: 123456789…")]
public async Task SlowUpdate_ReportsVariableNotesWithLengthLimits(string resourceName, int lineNumber, string expectedNotes) {
var driver = await NewTestDriverAsync(LoadCodeFromResource(resourceName));

Просмотреть файл

@ -0,0 +1,5 @@
public class C {
public void M() {
var abcdefghijklmnoprstquvwxyz = 0;
}
}

Просмотреть файл

@ -0,0 +1,5 @@
public class C {
public void M() {
var x = "12345678901234567890";
}
}

Просмотреть файл

@ -1,5 +0,0 @@
public class C {
public void M() {
var whyMyVariableNameIsThisLong7GladThatYouAsked1ItStartsOneFatefulEvening = 0;
}
}