Skip to content

Commit e54e40e

Browse files
committed
PR feedback
1 parent 96bbf3e commit e54e40e

File tree

3 files changed

+52
-81
lines changed

3 files changed

+52
-81
lines changed

src/Controls/src/SourceGen/GeneratorHelpers.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,29 +400,17 @@ static void SimplifyOnPlatform(XmlNode node, string? targetFramework, XmlNamespa
400400
}
401401

402402
/// <summary>
403-
/// Formats a numeric value as a culture-independent C# literal for source generation.
404-
/// Uses SymbolDisplay.FormatPrimitive to ensure proper handling of special values like NaN and Infinity.
403+
/// Formats a value as a culture-independent C# literal for source generation.
404+
/// Uses SymbolDisplay.FormatPrimitive to ensure proper handling of special values like NaN and Infinity
405+
/// but also numeric types and makes sure they are formatted correctly.
405406
/// </summary>
406-
public static string FormatInvariant(double value)
407+
/// <param name="value">The value to format</param>
408+
/// <param name="quoted">Whether to include quotes around the formatted value</param>
409+
/// <returns>A culture-independent string representation suitable for source generation</returns>
410+
public static string FormatInvariant(object value, bool quoted = false)
407411
{
408-
return SymbolDisplay.FormatPrimitive(value, false, false);
412+
return SymbolDisplay.FormatPrimitive(value, quoteStrings: quoted, useHexadecimalNumbers: false);
409413
}
410414

411-
/// <summary>
412-
/// Formats a numeric value as a culture-independent C# literal for source generation.
413-
/// Uses SymbolDisplay.FormatPrimitive to ensure proper handling of special values like NaN and Infinity.
414-
/// </summary>
415-
public static string FormatInvariant(float value)
416-
{
417-
return SymbolDisplay.FormatPrimitive(value, false, false);
418-
}
419415

420-
/// <summary>
421-
/// Formats a numeric value as a culture-independent C# literal with quote strings enabled.
422-
/// Uses SymbolDisplay.FormatPrimitive to ensure proper handling of special values like NaN and Infinity.
423-
/// </summary>
424-
public static string FormatInvariantWithQuotes(double value)
425-
{
426-
return SymbolDisplay.FormatPrimitive(value, true, false);
427-
}
428416
}

src/Controls/src/SourceGen/KnownTypeConverters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ public static string ConvertFontSize(string value, BaseNode node, ITypeSymbol to
432432
{
433433
value = value.Trim();
434434
if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out double size))
435-
return $"{FormatInvariantWithQuotes(size)}D";
435+
return $"{FormatInvariant(size, quoted: true)}D";
436436

437437

438438
var namedSizeSymbol = context.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.NamedSize")!;

src/Controls/tests/SourceGen.UnitTests/KnownTypeConvertersTests.cs

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
using System.Globalization;
22
using NUnit.Framework;
3-
using Microsoft.Maui.Controls.SourceGen;
4-
using Microsoft.Maui.Controls.Xaml;
53
using Microsoft.CodeAnalysis;
6-
using System.IO;
7-
using System.Text;
8-
using Microsoft.CodeAnalysis.Text;
9-
using Microsoft.CodeAnalysis.CSharp;
104
using System.Linq;
11-
using Microsoft.Maui.Controls.Xaml.UnitTests.SourceGen;
125

136
namespace Microsoft.Maui.Controls.SourceGen.UnitTests
147
{
158
[TestFixture]
9+
[NonParallelizable]
1610
public class KnownTypeConvertersTests : SourceGenXamlInitializeComponentTestBase
1711
{
12+
private CultureInfo? _originalCulture;
13+
private CultureInfo? _originalUICulture;
14+
15+
[SetUp]
16+
public void SetUp()
17+
{
18+
_originalCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
19+
_originalUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
20+
}
21+
22+
[TearDown]
23+
public void TearDown()
24+
{
25+
if (_originalCulture is not null)
26+
{
27+
System.Threading.Thread.CurrentThread.CurrentCulture = _originalCulture;
28+
}
29+
30+
if (_originalUICulture is not null)
31+
{
32+
System.Threading.Thread.CurrentThread.CurrentUICulture = _originalUICulture;
33+
}
34+
}
35+
1836
[TestCase("en-US", "1.5*")]
1937
[TestCase("fr-FR", "2.5*")]
2038
[TestCase("de-DE", "3.14*")]
2139
[TestCase("es-ES", "0.75*")]
2240
[TestCase("ru-RU", "1.414*")]
2341
public void GridLengthTypeConverter_StarValues_ProducesConsistentOutput_AcrossCultures(string cultureName, string gridLengthValue)
2442
{
25-
var originalCulture = CultureInfo.CurrentCulture;
26-
var originalUICulture = CultureInfo.CurrentUICulture;
27-
28-
try
29-
{
30-
// Set both current and UI cultures to test locale
31-
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
32-
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
43+
// Set both current and UI cultures to test locale
44+
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
45+
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
3346

34-
var xaml = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
47+
var xaml = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
3548
<ContentPage
3649
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
3750
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
@@ -68,21 +81,13 @@ public TestPage()
6881
// The generated code should contain a properly formatted GridLength with period as decimal separator
6982
// regardless of the current culture
7083
Assert.IsNotNull(generated, "Generated code should not be null");
71-
72-
// Extract the numeric value from the input (e.g., "2.5*" -> "2.5")
73-
var numericPart = gridLengthValue.Substring(0, gridLengthValue.Length - 1);
74-
75-
// The generated code should use period as decimal separator (culture-invariant)
76-
// and should contain the GridLength constructor with Star unit type
77-
Assert.That(generated, Does.Contain($"new global::Microsoft.Maui.GridLength({numericPart}, global::Microsoft.Maui.GridUnitType.Star)"),
78-
$"Generated code should contain culture-invariant GridLength with value {numericPart}. Generated code: {generated}");
79-
}
80-
finally
81-
{
82-
// Restore original cultures
83-
CultureInfo.CurrentCulture = originalCulture;
84-
CultureInfo.CurrentUICulture = originalUICulture;
85-
}
84+
// Extract the numeric value from the input (e.g., "2.5*" -> "2.5")
85+
var numericPart = gridLengthValue.Substring(0, gridLengthValue.Length - 1);
86+
87+
// The generated code should use period as decimal separator (culture-invariant)
88+
// and should contain the GridLength constructor with Star unit type
89+
Assert.That(generated, Does.Contain($"new global::Microsoft.Maui.GridLength({numericPart}, global::Microsoft.Maui.GridUnitType.Star)"),
90+
$"Generated code should contain culture-invariant GridLength with value {numericPart}. Generated code: {generated}");
8691
}
8792

8893
[TestCase("en-US", "100.5")]
@@ -92,15 +97,10 @@ public TestPage()
9297
[TestCase("ru-RU", "75.875")]
9398
public void GridLengthTypeConverter_AbsoluteValues_ProducesConsistentOutput_AcrossCultures(string cultureName, string gridLengthValue)
9499
{
95-
var originalCulture = CultureInfo.CurrentCulture;
96-
var originalUICulture = CultureInfo.CurrentUICulture;
97-
98-
try
99-
{
100-
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
101-
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
100+
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
101+
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
102102

103-
var xaml = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
103+
var xaml = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
104104
<ContentPage
105105
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
106106
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
@@ -138,12 +138,6 @@ public TestPage()
138138
// The generated code should use period as decimal separator and Absolute unit type
139139
Assert.That(generated, Does.Contain($"new global::Microsoft.Maui.GridLength({gridLengthValue}, global::Microsoft.Maui.GridUnitType.Absolute)"),
140140
$"Generated code should contain culture-invariant GridLength with absolute value {gridLengthValue}. Generated code: {generated}");
141-
}
142-
finally
143-
{
144-
CultureInfo.CurrentCulture = originalCulture;
145-
CultureInfo.CurrentUICulture = originalUICulture;
146-
}
147141
}
148142

149143
[TestCase("en-US")]
@@ -153,15 +147,10 @@ public TestPage()
153147
[TestCase("ru-RU")]
154148
public void GridLengthTypeConverter_SpecialValues_ProducesConsistentOutput_AcrossCultures(string cultureName)
155149
{
156-
var originalCulture = CultureInfo.CurrentCulture;
157-
var originalUICulture = CultureInfo.CurrentUICulture;
158-
159-
try
160-
{
161-
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
162-
CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
150+
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
151+
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
163152

164-
var xaml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
153+
var xaml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
165154
<ContentPage
166155
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
167156
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
@@ -179,7 +168,7 @@ public void GridLengthTypeConverter_SpecialValues_ProducesConsistentOutput_Acros
179168
</Grid>
180169
</ContentPage>";
181170

182-
var code = @"using System;
171+
var code = @"using System;
183172
using Microsoft.Maui.Controls;
184173
using Microsoft.Maui.Controls.Xaml;
185174
@@ -206,12 +195,6 @@ public TestPage()
206195
"Generated code should contain GridLength.Star for '*' values");
207196
Assert.That(generated, Does.Contain("global::Microsoft.Maui.GridLength.Auto"),
208197
"Generated code should contain GridLength.Auto for 'Auto' values");
209-
}
210-
finally
211-
{
212-
CultureInfo.CurrentCulture = originalCulture;
213-
CultureInfo.CurrentUICulture = originalUICulture;
214-
}
215198
}
216199

217200
[Test]

0 commit comments

Comments
 (0)