@@ -100,4 +100,54 @@ public void Parse_ReadsTheExponentSignWithTheProvider()
100100 {
101101 Assert . AreEqual ( P ( "0.015" ) , PreciseNumber . Parse ( "1,5e−2" , CommaDecimal ) ) ;
102102 }
103+
104+ [ TestMethod ]
105+ [ DataRow ( "1.5" , NumberStyles . Integer ) ]
106+ [ DataRow ( "1e5" , NumberStyles . Integer ) ]
107+ [ DataRow ( "-5" , NumberStyles . None ) ]
108+ [ DataRow ( "1e 5" , NumberStyles . Float ) ]
109+ [ DataRow ( "1e5" , NumberStyles . Number ) ]
110+ [ DataRow ( "1.5" , NumberStyles . None ) ]
111+ [ DataRow ( "1.5" , NumberStyles . Float ) ]
112+ [ DataRow ( "-1.5e-3" , NumberStyles . Float ) ]
113+ [ DataRow ( "-1.5e-3" , NumberStyles . Any ) ]
114+ [ DataRow ( "1,234.5" , NumberStyles . Any ) ]
115+ [ DataRow ( "-5" , NumberStyles . Integer ) ]
116+ [ DataRow ( "1e+5" , NumberStyles . Float ) ]
117+ public void TryParse_AcceptsWhatDecimalAccepts ( string text , NumberStyles style )
118+ {
119+ bool decimalParsed = decimal . TryParse ( text , style , CultureInfo . InvariantCulture , out decimal expected ) ;
120+ bool parsed = PreciseNumber . TryParse ( text , style , CultureInfo . InvariantCulture , out PreciseNumber actual ) ;
121+
122+ Assert . AreEqual ( decimalParsed , parsed , $ "PreciseNumber and decimal disagree on whether \" { text } \" parses under { style } .") ;
123+ if ( parsed )
124+ {
125+ Assert . AreEqual ( P ( expected . ToString ( CultureInfo . InvariantCulture ) ) , actual ) ;
126+ }
127+ }
128+
129+ [ TestMethod ]
130+ public void Parse_ThrowsForWhatTheStyleDoesNotAllow ( )
131+ {
132+ Assert . ThrowsExactly < FormatException > ( ( ) => PreciseNumber . Parse ( "1.5" , NumberStyles . Integer , CultureInfo . InvariantCulture ) ) ;
133+ Assert . ThrowsExactly < FormatException > ( ( ) => PreciseNumber . Parse ( "1e5" , NumberStyles . Integer , CultureInfo . InvariantCulture ) ) ;
134+ Assert . ThrowsExactly < FormatException > ( ( ) => PreciseNumber . Parse ( "-5" , NumberStyles . None , CultureInfo . InvariantCulture ) ) ;
135+ Assert . ThrowsExactly < FormatException > ( ( ) => PreciseNumber . Parse ( "1e 5" , NumberStyles . Float , CultureInfo . InvariantCulture ) ) ;
136+ }
137+
138+ [ TestMethod ]
139+ public void Parse_WithoutAStyleStillReadsFractionsExponentsAndSigns ( )
140+ {
141+ Assert . AreEqual ( P ( "1.5" ) , PreciseNumber . Parse ( "1.5" , CultureInfo . InvariantCulture ) ) ;
142+ Assert . AreEqual ( P ( "-0.0015" ) , PreciseNumber . Parse ( "-1.5e-3" , CultureInfo . InvariantCulture ) ) ;
143+ Assert . AreEqual ( P ( "1234.5" ) , PreciseNumber . Parse ( "1,234.5" , CultureInfo . InvariantCulture ) ) ;
144+ }
145+
146+ [ TestMethod ]
147+ public void Parse_ReadsACulturesNegativeSignOnlyWhenTheStyleAllowsIt ( )
148+ {
149+ Assert . AreEqual ( P ( "-2.5" ) , PreciseNumber . Parse ( "−2,5" , NumberStyles . Float , CommaDecimal ) ) ;
150+ Assert . IsFalse ( PreciseNumber . TryParse ( "−2" , NumberStyles . None , CommaDecimal , out _ ) ) ;
151+ Assert . IsFalse ( PreciseNumber . TryParse ( "-2" , NumberStyles . None , CommaDecimal , out _ ) ) ;
152+ }
103153}
0 commit comments