11using System . Globalization ;
22using NUnit . Framework ;
3- using Microsoft . Maui . Controls . SourceGen ;
4- using Microsoft . Maui . Controls . Xaml ;
53using Microsoft . CodeAnalysis ;
6- using System . IO ;
7- using System . Text ;
8- using Microsoft . CodeAnalysis . Text ;
9- using Microsoft . CodeAnalysis . CSharp ;
104using System . Linq ;
11- using Microsoft . Maui . Controls . Xaml . UnitTests . SourceGen ;
125
136namespace 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;
183172using Microsoft.Maui.Controls;
184173using 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