forked from dotnet/maui
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#6 from jkurdek/binding-source-generator
setup unit tests for binding intermediate representation
- Loading branch information
Showing
3 changed files
with
234 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Controls/tests/BindingSourceGen.UnitTests/BindingRepresentationGenTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Microsoft.Maui.Controls.BindingSourceGen; | ||
using Xunit; | ||
using Binding = Microsoft.Maui.Controls.BindingSourceGen.Binding; | ||
|
||
|
||
namespace BindingSourceGen.UnitTests; | ||
|
||
|
||
public class BindingRepresentationGenTests | ||
{ | ||
[Fact] | ||
public void GenerateSimpleBinding() | ||
{ | ||
var source = """ | ||
var label = new Label(); | ||
label.SetBinding(Label.RotationProperty, static (string s) => s.Length); | ||
"""; | ||
|
||
var result = SourceGenHelpers.Run(source); | ||
var results = result.Results.Single(); | ||
var steps = results.TrackedSteps; | ||
var actualBinding = (Binding)steps["BindingsWithDiagnostics"][0].Outputs[0].Value; | ||
|
||
var sourceCodeLocation = new SourceCodeLocation("", 2, 7); | ||
|
||
var expectedBinding = new Binding( | ||
1, | ||
sourceCodeLocation, | ||
new TypeName("string", false, false), | ||
new TypeName("int", false, false), | ||
[ | ||
new PathPart("s", false), | ||
new PathPart("Length", false), | ||
], | ||
true | ||
); | ||
Assert.Equivalent(expectedBinding, actualBinding); | ||
} | ||
|
||
[Fact] | ||
public void GenerateBindingWithLongerPath() | ||
{ | ||
var source = """ | ||
using Microsoft.Maui.Controls; | ||
var label = new Label(); | ||
label.SetBinding(Label.RotationProperty, static (Button b) => b.Text.Length); | ||
"""; | ||
|
||
var result = SourceGenHelpers.Run(source); | ||
var results = result.Results.Single(); | ||
var steps = results.TrackedSteps; | ||
var actualBinding = (Binding)steps["BindingsWithDiagnostics"][0].Outputs[0].Value; | ||
|
||
var sourceCodeLocation = new SourceCodeLocation("", 3, 7); | ||
|
||
var expectedBinding = new Binding( | ||
2, | ||
sourceCodeLocation, | ||
new TypeName("global::Microsoft.Maui.Controls.Button", false, false), | ||
new TypeName("int", false, false), | ||
[ | ||
new PathPart("b", false), | ||
new PathPart("Text", false), | ||
new PathPart("Length", false), | ||
], | ||
true | ||
); | ||
Assert.Equivalent(expectedBinding, actualBinding); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters