-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(xaml): support named params in MarkupExtension
- Loading branch information
Showing
6 changed files
with
200 additions
and
65 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/SourceGenerators/System.Xaml.Tests/Test/System.Xaml/ParsedMarkupExtensionInfoTests.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,68 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace Uno.Xaml.Tests.Test.System.Xaml | ||
{ | ||
[TestFixture] | ||
public class ParsedMarkupExtensionInfoTests | ||
{ | ||
public static IEnumerable GetSliceParametersTestCases() | ||
{ | ||
yield return new TestCaseData("{Binding Path}").Returns(new[] { "Path" }); | ||
yield return new TestCaseData("{Binding Path=Path}").Returns(new[] { "Path=Path" }); | ||
yield return new TestCaseData("{Binding Path, Converter={StaticResource asd}}").Returns(new[] | ||
{ | ||
"Path", | ||
"Converter={StaticResource asd}" | ||
}); | ||
yield return new TestCaseData("{Binding Test={some:NestedExtension With,Multiple,Paremeters}, Prop2='Asd'}").Returns(new[] | ||
{ | ||
"Test={some:NestedExtension With,Multiple,Paremeters}", | ||
"Prop2='Asd'" | ||
}); | ||
yield return new TestCaseData("{Binding Test='text,with,commas,in', Prop1={some:NestedExtension With,Multiple,Paremeters}, Prop2='Asd'}").Returns(new[] | ||
{ | ||
"Test='text,with,commas,in'", | ||
"Prop1={some:NestedExtension With,Multiple,Paremeters}", | ||
"Prop2='Asd'" | ||
}); | ||
yield return new TestCaseData("{Binding Test1='{}{escaped open bracket', Prop1={some:NestedExtension}, Test2='}close bracket'}").Returns(new[] | ||
{ | ||
"Test1='{}{escaped open bracket'", | ||
"Prop1={some:NestedExtension}", | ||
"Test2='}close bracket'" | ||
}); | ||
yield return new TestCaseData("{Binding Test1='{', Prop1={some:NestedExtension}, Test2='}close bracket'}").Returns(new[] | ||
{ | ||
"Test1='{'", | ||
"Prop1={some:NestedExtension}", | ||
"Test2='}close bracket'" | ||
}); | ||
yield return new TestCaseData("{Binding Test1=value without single-quot and with space is legal, Prop2=asd}").Returns(new[] | ||
{ | ||
"Test1=value without single-quot and with space is legal", | ||
"Prop2=asd" | ||
}); | ||
yield return new TestCaseData("{Binding Test1='{}{however to use the {} escape or comma, you need the single-quots}'}").Returns(new[] | ||
{ | ||
"Test1='{}{however to use the {} escape or comma, you need the single-quots}'" | ||
}); | ||
} | ||
|
||
[Test] | ||
[TestCaseSource(nameof(GetSliceParametersTestCases))] | ||
public string[] SliceParametersTest(string raw) | ||
{ | ||
// extract only the vargs portion without the starting `{Binding` and the ending `}` | ||
var vargs = Regex.Match(raw, "^{[^ ]+( (?<vargs>.+))?}$").Groups["vargs"].Value; | ||
|
||
return ParsedMarkupExtensionInfo.SliceParameters(vargs, raw).ToArray(); | ||
} | ||
} | ||
} |
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
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
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
16 changes: 16 additions & 0 deletions
16
src/SourceGenerators/XamlGenerationTests/MarkupExtensionTests.xaml
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,16 @@ | ||
<UserControl x:Class="XamlGenerationTests.MarkupExtensionTests" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:ext="using:XamlGenerationTests.MarkupExtensions"> | ||
|
||
<UserControl.Resources> | ||
<ext:NotImplementedConverter x:Key="SomeNotImplementedConverter" /> | ||
</UserControl.Resources> | ||
|
||
<StackPanel> | ||
<TextBlock Text="{Binding Converter={StaticResource SomeNotImplementedConverter}}" /> | ||
<!-- TODO: add support for positional parameters --> | ||
<!-- <TextBlock Text="{Binding Converter={StaticResource SomeNotImplementedConverter}, ConverterParameter={ext:MarkupWithArgsExtension 1, 2}}" /> --> | ||
<TextBlock Text="{Binding Converter={StaticResource SomeNotImplementedConverter}, ConverterParameter={ext:MarkupWithArgsExtension Prop1=1, Prop2=2}}" /> | ||
</StackPanel> | ||
</UserControl> |
51 changes: 51 additions & 0 deletions
51
src/SourceGenerators/XamlGenerationTests/MarkupExtensionTests.xaml.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,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Markup; | ||
using Windows.UI.Xaml.Data; | ||
// using System.Windows.Markup; // <-- will cause MarkupWithArgsExtension to fail | ||
// ^ as it would instead inherits from System.Windows.Markup rather than Windows.UI.Xaml.Markup | ||
|
||
namespace XamlGenerationTests | ||
{ | ||
public partial class MarkupExtensionTests : UserControl | ||
{ | ||
public MarkupExtensionTests() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} | ||
|
||
namespace XamlGenerationTests.MarkupExtensions | ||
{ | ||
public class NotImplementedConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, string language) => throw new NotImplementedException(); | ||
public object ConvertBack(object value, Type targetType, object parameter, string language) => throw new NotImplementedException(); | ||
} | ||
|
||
public class MarkupWithArgsExtension : MarkupExtension | ||
{ | ||
public MarkupWithArgsExtension() | ||
{ | ||
} | ||
public MarkupWithArgsExtension(object prop1, object prop2) | ||
{ | ||
this.Prop1 = prop1; | ||
this.Prop2 = prop2; | ||
} | ||
|
||
[System.Windows.Markup.ConstructorArgument("prop1")] | ||
public object Prop1 { get; set; } | ||
|
||
[System.Windows.Markup.ConstructorArgument("prop2")] | ||
public object Prop2 { get; set; } | ||
|
||
protected override object ProvideValue() => this; | ||
} | ||
} |