Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing and internal REPL maintenance #2289

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ public abstract class BaseRunner
// for tests that are run with and without NumberIsFloat set.
foreach (var exp in expectedStrArr)
{
if (!actualStrArr.Contains(exp) && !(NumberIsFloat && actualStrArr.Contains(Regex.Replace(exp, "(?<!Number,)(\\s|'|\\()Decimal(\\s|'|,|\\.|\\))", "$1Number$2"))))
if (!actualStrArr.Contains(exp) &&
!(NumberIsFloat && actualStrArr.Contains(Regex.Replace(exp, "(?<!Number,)(\\s|'|\\()Decimal(\\s|'|,|\\.|\\))", "$1Number$2"))) &&
!(NumberIsFloat && actualStrArr.Contains(Regex.Replace(exp, " Decimal, Number, ", " Number, Decimal, "))))
{
isValid = false;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,30 @@ private void RunExpressionTestCase(ExpressionTestCase testCase, Features feature
[Fact]
public void RunOne()
{
var path = @"D:\dev\pa2\Power-Fx\src\tests\Microsoft.PowerFx.Core.Tests\ExpressionTestCases\OptionSet.txt";
var line = 41;
var path = @"D:\...\src\tests\Microsoft.PowerFx.Core.Tests\ExpressionTestCases\StronglyTypedEnumParams.txt";
var line = 0; // or non-zero for a specific test

var runner = new InterpreterRunner();
var testRunner = new TestRunner(runner);

testRunner.AddFile(path);
testRunner.AddFile(new Dictionary<string, bool>() { { "PowerFxV1", true } }, path);

// We can filter to just cases we want
if (line > 0)
{
testRunner.Tests.RemoveAll(x => x.SourceLine != line);
}

var result = testRunner.RunTests();
var result = testRunner.RunTests();

if (result.Fail > 0)
{
Assert.True(false, result.Output);
}
else
{
Console.WriteLine(result.Output);
}
}
#endif

Expand Down
83 changes: 67 additions & 16 deletions src/tools/Repl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace Microsoft.PowerFx
{
public static class ConsoleRepl
{
{
private const string OptionFormatTable = "FormatTable";

private const string OptionNumberIsFloat = "NumberIsFloat";
Expand All @@ -43,10 +43,12 @@ public static class ConsoleRepl

private static StandardFormatter _standardFormatter;

private static readonly int _maxTableRows = 15;

private static bool _reset;

private static RecalcEngine ReplRecalcEngine()
{
{
var config = new PowerFxConfig(_features);

if (_largeCallDepth)
Expand Down Expand Up @@ -173,6 +175,8 @@ public static void REPL(TextReader input, bool prompt, bool echo, bool printResu
return;
}

_standardFormatter.MaxTableRows = _maxTableRows;

repl.HandleLineAsync(line, lineNumber: lineNumber++).Wait();

// Exit() function called
Expand Down Expand Up @@ -236,33 +240,73 @@ public FormulaValue Execute(StringValue file, BooleanValue echo)

private class Option0Function : ReflectionFunction
{
private static readonly RecordType OptionRecordType = RecordType.Empty()
.Add(new NamedFormulaType("Option", FormulaType.String))
.Add(new NamedFormulaType("Value", FormulaType.Boolean));

public class OptionRecordValue : RecordValue
{
private readonly Dictionary<string, FormulaValue> _fields = new Dictionary<string, FormulaValue>();

public OptionRecordValue(string name, bool value)
: base(OptionRecordType)
{
_fields.Add("Option", FormulaValue.New(name));
_fields.Add("Value", FormulaValue.New(value));
}

protected override bool TryGetField(FormulaType fieldType, string fieldName, out FormulaValue result)
{
return _fields.TryGetValue(fieldName, out result);
}
}

public class OptionTableValue : CollectionTableValue<RecordValue>
{
public OptionTableValue(IEnumerable<RecordValue> list)
: base(OptionRecordType, list)
{
}

protected override DValue<RecordValue> Marshal(RecordValue record)
{
return DValue<RecordValue>.Of(record);
}

protected override RecordValue MarshalInverse(RecordValue record)
{
return record;
}
}

public Option0Function()
: base("Option", FormulaType.String)
{
}

public FormulaValue Execute()
{
StringBuilder sb = new StringBuilder();

sb.Append("\n");
List<OptionRecordValue> optionList = new List<OptionRecordValue>()
{
new OptionRecordValue("FormatTable", _standardFormatter.FormatTable),
new OptionRecordValue("HashCodes", _standardFormatter.HashCodes),
new OptionRecordValue("NumberIsFloat", _numberIsFloat),
new OptionRecordValue("LargeCallDepth", _largeCallDepth),
new OptionRecordValue("StackTrace", _stackTrace),
new OptionRecordValue("TextFirst", _textFirst),
};

sb.Append($"{"FormatTable:",-42}{_standardFormatter.FormatTable}\n");
sb.Append($"{"HashCodes:",-42}{_standardFormatter.HashCodes}\n");
sb.Append($"{"NumberIsFloat:",-42}{_numberIsFloat}\n");
sb.Append($"{"LargeCallDepth:",-42}{_largeCallDepth}\n");
sb.Append($"{"StackTrace:",-42}{_stackTrace}\n");
sb.Append($"{"TextFirst:",-42}{_textFirst}\n");
_standardFormatter.MaxTableRows = int.MaxValue;

foreach (var prop in typeof(Features).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if (prop.PropertyType == typeof(bool) && prop.CanWrite)
{
sb.Append($"{prop.Name + ((bool)prop.GetValue(Features.PowerFxV1) ? " (V1)" : string.Empty) + ":",-42}{prop.GetValue(_features)}\n");
optionList.Add(new OptionRecordValue($"{prop.Name}{((bool)prop.GetValue(Features.PowerFxV1) ? " (v1)" : string.Empty)}", (bool)prop.GetValue(_features)));
}
}

return FormulaValue.New(sb.ToString());
return new OptionTableValue(optionList);
}
}

Expand Down Expand Up @@ -322,7 +366,7 @@ public Option2Function()
public FormulaValue Execute(StringValue option, BooleanValue value)
{
if (string.Equals(option.Value, OptionFormatTable, StringComparison.OrdinalIgnoreCase))
{
{
_standardFormatter.FormatTable = value.Value;
return value;
}
Expand Down Expand Up @@ -415,6 +459,14 @@ public override async Task Execute(PowerFxREPL repl, CancellationToken cancel, s
{
var msg =
@"
Run ""Option()"" to see the list of all the options and their current settings.

Set each option with ""Option( Options.<option name>, <true or false> )"".

For example, ""Option( Options.FormatTable, false )"" turns off table formatting.

In addition to Power Fx engine config Features, these items are available:

Options.FormatTable
Displays tables in a tabular format rather than using Table() function notation.

Expand All @@ -437,8 +489,7 @@ Displays the full stack trace when an exception is encountered.
Sets all the feature flags for Power Fx 1.0.

Options.None
Removed all the feature flags, which is even less than Canvas uses.

Removed all the feature flags, which is even less than Power Apps Canvas uses.
";

await WriteAsync(repl, msg, cancel)
Expand Down