Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
rick-nguyen committed Oct 22, 2024
1 parent 13fa198 commit 650ba23
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 101 deletions.
8 changes: 0 additions & 8 deletions src/libraries/Microsoft.PowerFx.Core/Binding/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3071,14 +3071,6 @@ public override void Visit(FirstNameNode node)
{
_txb.ErrorContainer.EnsureError(node, TexlStrings.ErrInvalidControlReference);
}
}

if (_txb.BindingConfig.MarkAsAsyncOnLazilyLoadedControlRef &&
lookupType.IsControl &&
lookupInfo.Data is IExternalControl control &&
!control.IsAppGlobalControl)
{
_txb.FlagPathAsAsync(_txb.Top);
}

// Update _usesGlobals, _usesResources, etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ internal class BindingConfig

public bool AnalysisMode { get; }

public bool MarkAsAsyncOnLazilyLoadedControlRef { get; } = false;

public bool UserDefinitionsMode { get; }

public BindingConfig(bool allowsSideEffects = false, bool useThisRecordForRuleScope = false, bool numberIsFloat = false, bool analysisMode = false, bool markAsAsyncOnLazilyLoadedControlRef = false, bool userDefinitionsMode = false)
public BindingConfig(bool allowsSideEffects = false, bool useThisRecordForRuleScope = false, bool numberIsFloat = false, bool analysisMode = false, bool userDefinitionsMode = false)
{
AllowsSideEffects = allowsSideEffects;
UseThisRecordForRuleScope = useThisRecordForRuleScope;
NumberIsFloat = numberIsFloat;
AnalysisMode = analysisMode;
MarkAsAsyncOnLazilyLoadedControlRef = markAsAsyncOnLazilyLoadedControlRef;
UserDefinitionsMode = userDefinitionsMode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,95 +137,6 @@ public void ImperativeUdfsEnabledOrDisabled(string script)
errors.AddRange(parseResult.Errors ?? Enumerable.Empty<TexlError>());

Assert.False(errors.Any());
}

[Fact]
public void TestUdfsAndNfsAreMarkedAsAsync()
{
// Arrange
var script = "test(): Text = Button1InScreen2.Text;Nf=Button1InScreen2.Text;Test2(): Void={Set(x, Button1InScreen2.Text);};";
var parseResult = TexlParser.ParseUserDefinitionScript(script, new ParserOptions() { AllowsSideEffects = true });
var udfs = UserDefinedFunction.CreateFunctions(parseResult.UDFs.Where(udf => udf.IsParseValid), _primitiveTypes, out var errors);
var symbolTable = new MockSymbolTable();
var dummyContol = new DummyExternalControl() { DisplayName = "Button1InScreen2" };
symbolTable.AddControl("Button1InScreen2", dummyContol, TypeTree.Create(Enumerable.Empty<KeyValuePair<string, DType>>()).SetItem("Text", DType.String, true));
var config = new BindingConfig(markAsAsyncOnLazilyLoadedControlRef: true);

// Act & Assert
foreach (var udf in udfs)
{
udf.BindBody(symbolTable, new MockGlue(), config);
Assert.True(udf.IsAsync);
}

foreach (var nf in parseResult.NamedFormulas)
{
var binding = TexlBinding.Run(new MockGlue(), nf.Formula.ParseTree, symbolTable, config, DType.EmptyRecord);
Assert.True(binding.IsAsync(binding.Top));
}
}

[Fact]
public void TestFormulaUsingUdfsOrNamedFormulaReferringControlAsAsync()
{
// Arrange
var script = "test(): Text = Button1InScreen2.Text;Nf=Button1InScreen2.Text;Test2(): Void={Set(x, Button1InScreen2.Text);};";
var parseResult = TexlParser.ParseUserDefinitionScript(script, new ParserOptions() { AllowsSideEffects = true });
var udfs = UserDefinedFunction.CreateFunctions(parseResult.UDFs.Where(udf => udf.IsParseValid), _primitiveTypes, out var errors);
var symbolTable = new MockSymbolTable();
var dummyContol = new DummyExternalControl() { DisplayName = "Button1InScreen2" };
symbolTable.AddControl("Button1InScreen2", dummyContol, TypeTree.Create(Enumerable.Empty<KeyValuePair<string, DType>>()).SetItem("Text", DType.String, true));
var config = new BindingConfig(markAsAsyncOnLazilyLoadedControlRef: true);
var symbolTable2 = new MockSymbolTable();
var symbolTable3 = new SymbolTable();
foreach (var udf in udfs)
{
udf.BindBody(symbolTable, new MockGlue(), config);
Assert.True(udf.IsAsync);
symbolTable3.AddFunction(udf);
}

foreach (var nf in parseResult.NamedFormulas)
{
var binding = TexlBinding.Run(new MockGlue(), nf.Formula.ParseTree, symbolTable, config, DType.EmptyRecord);
Assert.True(binding.IsAsync(binding.Top));
symbolTable2.Add(nf.Ident.Name, new NameLookupInfo(BindKind.PowerFxResolvedObject, binding.GetType(binding.Top), DPath.Root, 0, isAsync: binding.IsAsync(binding.Top)));
}

var combinedSymbolTable = ReadOnlySymbolTable.Compose(symbolTable2, symbolTable3);

// Act
var checkResult1 = new Engine().Check("test() & \"ddd\"", null, combinedSymbolTable);
var checkResult2 = new Engine().Check("Nf & \"ddd\"", null, combinedSymbolTable);
var checkResult3 = new Engine().Check("Test2();", new ParserOptions { AllowsSideEffects = true }, combinedSymbolTable);
var checkResults = new List<CheckResult> { checkResult1, checkResult2, checkResult3 };

// Assert
foreach (var checkResult in checkResults)
{
Assert.True(checkResult.IsSuccess);
Assert.True(checkResult.Binding.IsAsync(checkResult.Parse.Root));
}
}

[Fact]
public void TestUdfsAreNotMarkedAsAsynWhenReferringToNonLazilyLoadedControl()
{
// Arrange
var script = "test(): Text = App.Text;";
var parseResult = TexlParser.ParseUserDefinitionScript(script, new ParserOptions() { AllowsSideEffects = true });
var udfs = UserDefinedFunction.CreateFunctions(parseResult.UDFs.Where(udf => udf.IsParseValid), _primitiveTypes, out var errors);
var symbolTable = new MockSymbolTable();
var dummyContol = new DummyExternalControl() { DisplayName = "App", IsAppInfoControl = true };
symbolTable.AddControl("App", dummyContol, TypeTree.Create(Enumerable.Empty<KeyValuePair<string, DType>>()).SetItem("Text", DType.String, true));
var config = new BindingConfig(markAsAsyncOnLazilyLoadedControlRef: true);

// Act & Assert
foreach (var udf in udfs)
{
udf.BindBody(symbolTable, new MockGlue(), config);
Assert.False(udf.IsAsync);
}
}
}
}

0 comments on commit 650ba23

Please sign in to comment.