File tree Expand file tree Collapse file tree
tests/RustPlusBot.ItemData.Generator.Tests
tools/RustPlusBot.ItemData.Generator/Sources Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using RustPlusBot . ItemData . Generator . Sources ;
2+
3+ namespace RustPlusBot . ItemData . Generator . Tests ;
4+
5+ public sealed class UpkeepQuantityTests
6+ {
7+ [ Fact ]
8+ public void Single_value_parses_to_equal_min_max ( )
9+ {
10+ var ( min , max ) = UpkeepQuantity . Parse ( "1" ) ;
11+ Assert . Equal ( 1 , min ) ;
12+ Assert . Equal ( 1 , max ) ;
13+ }
14+
15+ [ Fact ]
16+ public void EnDash_range_parses_min_and_max ( )
17+ {
18+ var ( min , max ) = UpkeepQuantity . Parse ( "8–25" ) ; // 8–25
19+ Assert . Equal ( 8 , min ) ;
20+ Assert . Equal ( 25 , max ) ;
21+ }
22+
23+ [ Fact ]
24+ public void Hyphen_range_parses_min_and_max ( )
25+ {
26+ var ( min , max ) = UpkeepQuantity . Parse ( "20-67" ) ;
27+ Assert . Equal ( 20 , min ) ;
28+ Assert . Equal ( 67 , max ) ;
29+ }
30+
31+ [ Fact ]
32+ public void Whitespace_is_trimmed ( )
33+ {
34+ var ( min , max ) = UpkeepQuantity . Parse ( " 5–17 " ) ;
35+ Assert . Equal ( 5 , min ) ;
36+ Assert . Equal ( 17 , max ) ;
37+ }
38+
39+ [ Theory ]
40+ [ InlineData ( "" ) ]
41+ [ InlineData ( "lots" ) ]
42+ [ InlineData ( "8–" ) ]
43+ [ InlineData ( "a–b" ) ]
44+ public void Unparseable_throws ( string raw )
45+ {
46+ Assert . Throws < FormatException > ( ( ) => UpkeepQuantity . Parse ( raw ) ) ;
47+ }
48+ }
Original file line number Diff line number Diff line change 1+ using System . Globalization ;
2+
3+ namespace RustPlusBot . ItemData . Generator . Sources ;
4+
5+ /// <summary>Parses a RustLabs upkeep quantity string (e.g. <c>"1"</c> or <c>"8–25"</c>).</summary>
6+ internal static class UpkeepQuantity
7+ {
8+ /// <summary>Parses <paramref name="raw"/> into a (min, max) pair. A single value yields min == max.</summary>
9+ /// <param name="raw">The quantity string: a number or an en-dash/hyphen range.</param>
10+ /// <returns>The parsed lower and upper bounds.</returns>
11+ /// <exception cref="FormatException">The string is neither a number nor a number range.</exception>
12+ public static ( int Min , int Max ) Parse ( string raw )
13+ {
14+ ArgumentNullException . ThrowIfNull ( raw ) ;
15+ var trimmed = raw . Trim ( ) ;
16+ var dash = trimmed . IndexOfAny ( [ '–' , '-' ] ) ;
17+ if ( dash < 0 )
18+ {
19+ var single = int . Parse ( trimmed , CultureInfo . InvariantCulture ) ;
20+ return ( single , single ) ;
21+ }
22+
23+ var min = int . Parse ( trimmed [ ..dash ] , CultureInfo . InvariantCulture ) ;
24+ var max = int . Parse ( trimmed [ ( dash + 1 ) ..] , CultureInfo . InvariantCulture ) ;
25+ return ( min , max ) ;
26+ }
27+ }
You can’t perform that action at this time.
0 commit comments