Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/Controls/src/SourceGen/NodeSGExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ void reportDiagnostic()
if (string.IsNullOrEmpty(valueString))
return "default";
if (sbyte.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out var sbyteValue))
return SymbolDisplay.FormatPrimitive(sbyteValue, true, false);
return $"(sbyte){SymbolDisplay.FormatPrimitive(sbyteValue, true, false)}";
else
reportDiagnostic();
}
Expand All @@ -310,7 +310,7 @@ void reportDiagnostic()
if (string.IsNullOrEmpty(valueString))
return "default";
if (byte.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out var byteValue))
return SymbolDisplay.FormatPrimitive(byteValue, true, false);
return $"(byte){SymbolDisplay.FormatPrimitive(byteValue, true, false)}";
else
reportDiagnostic();
}
Expand All @@ -319,16 +319,16 @@ void reportDiagnostic()
if (string.IsNullOrEmpty(valueString))
return "default";
if (short.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out var shortValue))
return SymbolDisplay.FormatPrimitive(shortValue, true, false);
return $"(short){SymbolDisplay.FormatPrimitive(shortValue, true, false)}";
else
reportDiagnostic();
}
if (toType.SpecialType == SpecialType.System_UInt16)
{
if (string.IsNullOrEmpty(valueString))
return "default";
if (short.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out var ushortValue))
return SymbolDisplay.FormatPrimitive(ushortValue, true, false);
if (ushort.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out var ushortValue))
return $"(ushort){SymbolDisplay.FormatPrimitive(ushortValue, true, false)}";
else
reportDiagnostic();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Linq;
using Xunit;

namespace Microsoft.Maui.Controls.SourceGen.UnitTests;

public class NumericBindablePropertyPrimitives : SourceGenXamlInitializeComponentTestBase
{
[Fact]
public void BindableProperty_PrimitiveSmallIntegers_EmitTypedCastsAndBoundaries()
{
var xaml =
"""
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test"
x:Class="Test.TestPage">
<VerticalStackLayout>
<local:NumericView SByteValue="-128" ByteValue="255" ShortValue="0" UShortValue="65535" />
</VerticalStackLayout>
</ContentPage>
""";

var code =
"""
using System;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Test;

[XamlProcessing(XamlInflator.SourceGen)]
public partial class TestPage : ContentPage
{
public TestPage()
{
InitializeComponent();
}
}

public class NumericView : BindableObject
{
public static readonly BindableProperty SByteValueProperty =
BindableProperty.Create(nameof(SByteValue), typeof(sbyte), typeof(NumericView), default(sbyte));

public static readonly BindableProperty ByteValueProperty =
BindableProperty.Create(nameof(ByteValue), typeof(byte), typeof(NumericView), default(byte));

public static readonly BindableProperty ShortValueProperty =
BindableProperty.Create(nameof(ShortValue), typeof(short), typeof(NumericView), default(short));

public static readonly BindableProperty UShortValueProperty =
BindableProperty.Create(nameof(UShortValue), typeof(ushort), typeof(NumericView), default(ushort));

public sbyte SByteValue
{
get => (sbyte)GetValue(SByteValueProperty);
set => SetValue(SByteValueProperty, value);
}

public byte ByteValue
{
get => (byte)GetValue(ByteValueProperty);
set => SetValue(ByteValueProperty, value);
}

public short ShortValue
{
get => (short)GetValue(ShortValueProperty);
set => SetValue(ShortValueProperty, value);
}

public ushort UShortValue
{
get => (ushort)GetValue(UShortValueProperty);
set => SetValue(UShortValueProperty, value);
}
}
""";

var (result, generated) = RunGenerator(xaml, code);

Assert.Empty(result.Diagnostics.Where(d => d.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error));
Assert.NotNull(generated);

Assert.Contains("(sbyte)-128", generated, StringComparison.Ordinal);
Assert.Contains("(byte)255", generated, StringComparison.Ordinal);
Assert.Contains("(short)0", generated, StringComparison.Ordinal);
Assert.Contains("(ushort)65535", generated, StringComparison.Ordinal);
}
}
2 changes: 1 addition & 1 deletion src/Controls/tests/Xaml.UnitTests/SetValue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<d:IgnorableElement />
</ContentView.Content>
</ContentView>
<local:MockViewWithValues x:Name="mockView0" AChar="!" AByte="2" ASByte="-12" AShort="-22" UShort="32" ADecimal="42" />
<local:MockViewWithValues x:Name="mockView0" AChar="!" AByte="2" ASByte="-12" AShort="-22" UShort="32" ADecimal="42" BPByte="150" />
<local:ViewWithEnums x:Name="enums" IntEnum="Foo" ByteEnum="Bar" />
<local:MockViewWithValues x:Name="implicit0">
<local:MockViewWithValues.BPBar>
Expand Down
19 changes: 19 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/SetValue.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public string BPBar
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}

public static readonly BindableProperty BPByteProperty =
BindableProperty.Create("BPByte", typeof(byte), typeof(MockViewWithValues), (byte)255);

public byte BPByte
{
get => (byte)GetValue(BPByteProperty);
set => SetValue(BPByteProperty, value);
}
}

[TypeConverter(typeof(SV_FooTypeConveter))]
Expand Down Expand Up @@ -390,6 +399,15 @@ public string BPBar
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}

public static readonly BindableProperty BPByteProperty =
BindableProperty.Create("BPByte", typeof(byte), typeof(MockViewWithValues), (byte)0);

public byte BPByte
{
get => (byte)GetValue(BPByteProperty);
set => SetValue(BPByteProperty, value);
}
}

[TypeConverter(typeof(SV_FooTypeConveter))]
Expand Down Expand Up @@ -550,6 +568,7 @@ internal void MorePrimitiveTypes(XamlInflator inflator)
Assert.Equal((short)-22, page.mockView0.AShort);
Assert.Equal((ushort)32, page.mockView0.UShort);
Assert.Equal((decimal)42, page.mockView0.ADecimal);
Assert.Equal((byte)150, page.mockView0.BPByte);
}

[Theory]
Expand Down
Loading