File tree Expand file tree Collapse file tree
Sagara.Core.Tests/EnumsTests Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44 <TargetFrameworks >net10.0;net11.0</TargetFrameworks >
55
66 <!-- NuGet -->
7- <Version >6.0.0-preview5 </Version >
7+ <Version >6.0.0-preview6 </Version >
88 <AssemblyVersion >6.0.0</AssemblyVersion >
99 <FileVersion >6.0.0</FileVersion >
1010 <Authors >Jon Sagara</Authors >
Original file line number Diff line number Diff line change @@ -116,6 +116,43 @@ public void IsValidValue_ReturnsFalse()
116116 Assert . False ( EnumTraits < ValuesEnum > . IsValidValue ( ValuesEnum . Unknown ) ) ;
117117 }
118118
119+ [ Theory ]
120+ [ InlineData ( ValuesEnum . Value1 ) ]
121+ [ InlineData ( ValuesEnum . Value2 ) ]
122+ [ InlineData ( ValuesEnum . Value3 ) ]
123+ public void EnsureValidValue_ValidValue_DoesNotThrow ( ValuesEnum value )
124+ {
125+ EnumTraits < ValuesEnum > . EnsureValidValue ( value ) ;
126+ }
127+
128+ [ Fact ]
129+ public void EnsureValidValue_InvalidValue_Throws ( )
130+ {
131+ Assert . Throws < ArgumentException > ( ( ) =>
132+ EnumTraits < ValuesEnum > . EnsureValidValue ( ValuesEnum . Unknown )
133+ ) ;
134+ }
135+
136+ [ Fact ]
137+ public void EnsureValidValue_UndefinedCastValue_Throws ( )
138+ {
139+ Assert . Throws < ArgumentException > ( ( ) =>
140+ EnumTraits < ValuesEnum > . EnsureValidValue ( ( ValuesEnum ) 99 )
141+ ) ;
142+ }
143+
144+ [ Fact ]
145+ public void EnsureValidValue_InvalidValue_ExceptionMessage_ContainsEnumTypeAndValue ( )
146+ {
147+ var ex = Assert . Throws < ArgumentException > ( ( ) =>
148+ EnumTraits < ValuesEnum > . EnsureValidValue ( ValuesEnum . Unknown )
149+ ) ;
150+
151+ Assert . Contains ( typeof ( ValuesEnum ) . FullName ! , ex . Message , StringComparison . Ordinal ) ;
152+ Assert . Contains ( nameof ( ValuesEnum . Unknown ) , ex . Message , StringComparison . Ordinal ) ;
153+ Assert . Equal ( "value" , ex . ParamName ) ;
154+ }
155+
119156 [ Fact ]
120157 public void EnsureNoDuplicateValues_NoDuplicates_DoesNotThrow ( )
121158 {
Original file line number Diff line number Diff line change @@ -108,6 +108,19 @@ public static void EnsureNoDuplicateValues()
108108 }
109109 }
110110
111+ /// <summary>
112+ /// Throws an <see cref="ArgumentException"/> if the enum value is not in the list of valid values.
113+ /// </summary>
114+ /// <param name="value">The enum value to validate.</param>
115+ /// <exception cref="ArgumentException">Thrown if the enum value is not in the list of valid values.</exception>
116+ public static void EnsureValidValue ( TEnum value )
117+ {
118+ if ( ! IsValidValue ( value ) )
119+ {
120+ throw new ArgumentException ( $ "Invalid { _enumType . FullName } enum value '{ value } '.", nameof ( value ) ) ;
121+ }
122+ }
123+
111124 /// <summary>
112125 /// Return the display name for the enum value, which is either from the [Display] attribute
113126 /// or the property name.
You can’t perform that action at this time.
0 commit comments