-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add support for partial xaml parsing
- Loading branch information
Showing
4 changed files
with
286 additions
and
29 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/Uno.Toolkit.RuntimeTests/Extensions/DictionaryExtensions.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,42 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Uno.Toolkit.RuntimeTests.Extensions; | ||
|
||
internal static class DictionaryExtensions | ||
{ | ||
/// <summary> | ||
/// Combine two dictionaries into a new one. | ||
/// </summary> | ||
/// <typeparam name="TKey"></typeparam> | ||
/// <typeparam name="TValue"></typeparam> | ||
/// <param name="dict"></param> | ||
/// <param name="other"></param> | ||
/// <param name="preferOther"></param> | ||
/// <param name="comparer"></param> | ||
/// <returns></returns> | ||
public static IDictionary<TKey,TValue> Combine<TKey, TValue>( | ||
this IReadOnlyDictionary<TKey,TValue> dict, | ||
IReadOnlyDictionary<TKey,TValue>? other, | ||
bool preferOther = true, | ||
IEqualityComparer<TKey>? comparer = null | ||
) where TKey : notnull | ||
{ | ||
var result = new Dictionary<TKey, TValue>(dict, comparer); | ||
if (other is { }) | ||
{ | ||
foreach (var kvp in other) | ||
{ | ||
if (preferOther || !result.ContainsKey(kvp.Key)) | ||
{ | ||
result[kvp.Key] = kvp.Value; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Uno.Toolkit.RuntimeTests/Extensions/StackExtensions.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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Uno.Toolkit.RuntimeTests.Extensions; | ||
|
||
internal static class StackExtensions | ||
{ | ||
public static IEnumerable<T> PopWhile<T>(this Stack<T> stack, Func<T, bool> predicate) | ||
{ | ||
while (stack.TryPeek(out var item) && predicate(item)) | ||
{ | ||
yield return stack.Pop(); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Uno.Toolkit.RuntimeTests.Helpers; | ||
|
||
namespace Uno.Toolkit.RuntimeTests.Tests; | ||
|
||
[TestClass] | ||
internal class XamlHelperTests | ||
{ | ||
[TestMethod] | ||
public void Complex_Test() | ||
{ | ||
var result = XamlHelper.XamlAutoFill(""" | ||
<DataTemplate> | ||
<StackPanel> | ||
<Grid> | ||
<!-- test --> | ||
<Button /> | ||
<TextBlock> | ||
<TextBlock> | ||
<Grid Background="SkyBlue" | ||
Tag="this unclosed node spans on multiple lines"> | ||
<Button> | ||
<TextBlock> | ||
<Button Tag="this one has closing"> | ||
<TextBlock> | ||
</Button> | ||
<Grid Tag="single-line multi-nesting"><Grid><Grid Tag="multi-line" | ||
Background="Pink"> | ||
<Button> | ||
<Grid Tag="self-closing tag, should not have have closing tag appended"/> | ||
<Button /> | ||
<Grid><Border><Grid> | ||
<Button Content="ThisShouldStillWork" /> | ||
</Grid></Border></Grid> | ||
<GridA><Border><GridB> | ||
""").TrimEnd(); | ||
var expectation = """ | ||
<DataTemplate> | ||
<StackPanel> | ||
<Grid> | ||
<!-- test --> | ||
<Button /> | ||
<TextBlock> | ||
</TextBlock> | ||
<TextBlock> | ||
</TextBlock> | ||
</Grid> | ||
<Grid Background="SkyBlue" | ||
Tag="this unclosed node spans on multiple lines"> | ||
<Button> | ||
<TextBlock> | ||
</TextBlock> | ||
</Button> | ||
<Button Tag="this one has closing"> | ||
<TextBlock> | ||
</TextBlock> | ||
</Button> | ||
</Grid> | ||
<Grid Tag="single-line multi-nesting"><Grid><Grid Tag="multi-line" | ||
Background="Pink"> | ||
<Button> | ||
</Button> | ||
</Grid> | ||
</Grid> | ||
</Grid> | ||
<Grid Tag="self-closing tag, should not have have closing tag appended"/> | ||
<Button /> | ||
</StackPanel> | ||
<Grid><Border><Grid> | ||
<Button Content="ThisShouldStillWork" /> | ||
</Grid></Border></Grid> | ||
<GridA><Border><GridB> | ||
</GridB> | ||
</Border> | ||
</GridA> | ||
</DataTemplate> | ||
""".TrimEnd(); | ||
|
||
Assert.AreEqual(expectation, result); | ||
} | ||
} |