1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Globalization ;
4+ using System . IO ;
5+ using System . Linq ;
6+ using System . Reflection ;
7+ using System . Xml . Linq ;
8+ using SyncTrayzor . Localization ;
9+ using Xunit ;
10+
11+ namespace SyncTrayzor . Tests
12+ {
13+ /// <summary>
14+ /// DummyArgument is a class that can be implicitly converted to various types required by SmartFormat strings.
15+ /// </summary>
16+ public class DummyArgument : IEnumerable < object >
17+ {
18+ private readonly List < object > _items = new ( ) { "dummy" } ;
19+
20+ public override string ToString ( ) => "dummy" ;
21+
22+ public static implicit operator int ( DummyArgument _ ) => 1 ;
23+ public static implicit operator string ( DummyArgument d ) => d . ToString ( ) ;
24+
25+ public IEnumerator < object > GetEnumerator ( ) => _items . GetEnumerator ( ) ;
26+ System . Collections . IEnumerator System . Collections . IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
27+ }
28+
29+ public class LocalizerTests
30+ {
31+ private static readonly object [ ] DummyArgs =
32+ Enumerable . Repeat < object > ( new DummyArgument ( ) , 10 ) . ToArray ( ) ;
33+
34+ [ Fact ]
35+ public void FormatWithCulture_AllResourceStringsAreValidSmartFormatStrings ( )
36+ {
37+ var testAssembly = Assembly . GetExecutingAssembly ( ) ;
38+ var testDir = Path . GetDirectoryName ( testAssembly . Location ) ;
39+ var resourcesDir = Path . Combine ( testDir , "Resources" ) ;
40+ var resxFiles = Directory . GetFiles ( resourcesDir , "Resources*.resx" ) ;
41+
42+ foreach ( var resxFile in resxFiles )
43+ {
44+ var fileName = Path . GetFileNameWithoutExtension ( resxFile ) ;
45+ var culture = CultureInfo . GetCultureInfo ( "en-US" ) ;
46+ if ( fileName . Contains ( '.' ) )
47+ {
48+ var cultureName = fileName [ ( fileName . LastIndexOf ( '.' ) + 1 ) ..] ;
49+ culture = CultureInfo . GetCultureInfo ( cultureName ) ;
50+ }
51+
52+ var doc = XDocument . Load ( resxFile ) ;
53+ var dataElements = doc . Descendants ( "data" ) ;
54+
55+ foreach ( var data in dataElements )
56+ {
57+ var name = data . Attribute ( "name" ) ? . Value ;
58+ var value = data . Element ( "value" ) ? . Value ;
59+
60+ if ( string . IsNullOrEmpty ( value ) )
61+ continue ;
62+
63+ try
64+ {
65+ Localizer . FormatWithCulture ( culture , value , DummyArgs ) ;
66+ }
67+ catch ( Exception ex )
68+ {
69+ throw new Exception (
70+ $ "Invalid SmartFormat string in { Path . GetFileName ( resxFile ) } , key '{ name } ': { ex . Message } ",
71+ ex ) ;
72+ }
73+ }
74+ }
75+ }
76+ }
77+ }
0 commit comments