1+ using NUnit . Framework ;
2+ using System ;
3+ using System . Globalization ;
4+ using System . Linq ;
5+
6+ namespace LogExpert . Tests
7+ {
8+ [ TestFixture ]
9+ public class DateFormatParserTest
10+ {
11+ [ Test ]
12+ public void CanParseAllCultures ( )
13+ {
14+ var cultures = CultureInfo . GetCultures ( CultureTypes . AllCultures ) ;
15+
16+ foreach ( var culture in cultures )
17+ {
18+ if ( culture . Name == "dz" || culture . Name . StartsWith ( "dz-" ) )
19+ {
20+ Console . WriteLine ( "The dz (Dzongkha) time format is not supported yet." ) ;
21+ continue ;
22+ }
23+
24+ var datePattern = GetDateAndTimeFormat ( culture ) ;
25+ var message = $ "Culture: { culture . Name } ({ culture . EnglishName } { datePattern } )";
26+ var sections = Parser . ParseSections ( datePattern , out bool syntaxError ) ;
27+
28+ Assert . IsFalse ( syntaxError , message ) ;
29+
30+ var dateSection = sections . FirstOrDefault ( ) ;
31+ Assert . IsNotNull ( dateSection , message ) ;
32+
33+ var now = DateTime . Now ;
34+ var expectedFormattedDate = now . ToString ( datePattern ) ;
35+ var actualFormattedDate = now . ToString ( string . Join ( "" , dateSection . GeneralTextDateDurationParts ) ) ;
36+ Assert . AreEqual ( expectedFormattedDate , actualFormattedDate , message ) ;
37+ }
38+ }
39+
40+ [ Test ]
41+ [ TestCase ( "en-US" , "MM" , "dd" , "yyyy" , "hh" , "mm" , "ss" , "tt" ) ]
42+ [ TestCase ( "en-ZA" , "yyyy" , "MM" , "dd" , "hh" , "mm" , "ss" , "tt" ) ]
43+ [ TestCase ( "fr-FR" , "dd" , "MM" , "yyyy" , "HH" , "mm" , "ss" ) ]
44+ [ TestCase ( "de-DE" , "dd" , "MM" , "yyyy" , "HH" , "mm" , "ss" ) ]
45+ [ TestCase ( "ar-TN" , "dd" , "MM" , "yyyy" , "HH" , "mm" , "ss" ) ]
46+ [ TestCase ( "as" , "dd" , "MM" , "yyyy" , "tt" , "hh" , "mm" , "ss" ) ]
47+ [ TestCase ( "bg" , "dd" , "MM" , "yyyy" , "HH" , "mm" , "ss" ) ]
48+ public void TestDateFormatParserFromCulture ( string cultureInfoName , params string [ ] expectedDateParts )
49+ {
50+ var culture = CultureInfo . GetCultureInfo ( cultureInfoName ) ;
51+
52+ var datePattern = GetDateAndTimeFormat ( culture ) ;
53+
54+ var sections = Parser . ParseSections ( datePattern , out bool syntaxError ) ;
55+
56+ var message = $ "Culture: { culture . EnglishName } , Actual date pattern: { datePattern } ";
57+
58+ Assert . IsFalse ( syntaxError , message ) ;
59+
60+ var dateSection = sections . FirstOrDefault ( ) ;
61+ Assert . IsNotNull ( dateSection ) ;
62+
63+ var dateParts = dateSection
64+ . GeneralTextDateDurationParts
65+ . Where ( Token . IsDatePart )
66+ . Select ( p => DateFormatPartAdjuster . AdjustDateTimeFormatPart ( p ) )
67+ . ToArray ( ) ;
68+
69+ Assert . AreEqual ( expectedDateParts . Length , dateParts . Length , message ) ;
70+
71+ for ( var i = 0 ; i < expectedDateParts . Length ; i ++ )
72+ {
73+ var expected = expectedDateParts [ i ] ;
74+ var actual = dateParts [ i ] ;
75+ Assert . AreEqual ( expected , actual , message ) ;
76+ }
77+ }
78+
79+ private string GetDateAndTimeFormat ( CultureInfo culture )
80+ {
81+ return string . Concat (
82+ culture . DateTimeFormat . ShortDatePattern ,
83+ " " ,
84+ culture . DateTimeFormat . LongTimePattern
85+ ) ;
86+ }
87+ }
88+ }
0 commit comments