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

test for dependency visiting. #2784

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -2,9 +2,11 @@
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Linq;
using Microsoft.PowerFx.Core.Public;
using Microsoft.PowerFx.Core.Utils;
using Microsoft.PowerFx.Syntax;
using Microsoft.PowerFx.Types;
using Xunit;

namespace Microsoft.PowerFx.Core.Tests
Expand Down Expand Up @@ -78,5 +80,64 @@ public void TestFormulaSet()
Assert.True(indexMap["A"] < indexMap["H"]);
Assert.True(indexMap["G"] < indexMap["H"]);
}

private class TestDependencyFinderVisitor2 : IdentityTexlVisitor
{
public readonly HashSet<string> _vars = new HashSet<string>();

// <identifier>
public override void Visit(FirstNameNode node)
{
var name = node.Ident.Name.Value;

_vars.Add(name);

base.Visit(node);
}

// <expr>.field
public override bool PreVisit(DottedNameNode node)
{
string fieldName = node.Right.Name.Value;

_vars.Add(fieldName);

return base.PreVisit(node);
}
}

// Find identifiers in an expression
[Fact]
public void DependencyTest()
{
var expr = "x+ 1 + rec.field1";

RecordType rec = RecordType.Empty()
.Add("field1", FormulaType.Number)
.Add("field2", FormulaType.Number);

var symbolTable = new SymbolTable();
symbolTable.AddVariable("x", FormulaType.Number);
symbolTable.AddVariable("y", FormulaType.Number);
symbolTable.AddVariable("rec", rec);

var engine = new Engine();

var check = new CheckResult(engine)
.SetText(expr)
.SetBindingInfo(symbolTable);

var parse = check.ApplyParse();
Assert.True(check.IsSuccess);

var vis = new TestDependencyFinderVisitor2();

parse.Root.Accept(vis);

var list = vis._vars.OrderBy(x => x).ToArray();
var listStr = string.Join(",", list);

Assert.Equal("field1,rec,x", listStr);
}
}
}
Loading