c#


Comparing two enum *types* for equivalence?


In my application, I have two equivalent enums. One lives in the DAL, the other in the service contract layer. They have the same name (but are in different namespaces), and should have the same members and values.
I'd like to write a unit test that enforces this. So far, I've got the following:
public static class EnumAssert
{
public static void AreEquivalent(Type x, Type y)
{
// Enum.GetNames and Enum.GetValues return arrays sorted by value.
string[] xNames = Enum.GetNames(x);
string[] yNames = Enum.GetNames(y);
Assert.AreEqual(xNames.Length, yNames.Length);
for (int i = 0; i < xNames.Length; i++)
{
Assert.AreEqual(xNames[i], yNames[i]);
}
// TODO: How to validate that the values match?
}
}
This works fine for comparing the names, but how do I check that the values match as well?
(I'm using NUnit 2.4.6, but I figure this applies to any unit test framework)
Enum.GetValues:
var xValues = Enum.GetValues(x);
var yValues = Enum.GetValues(y);
for (int i = 0; i < xValues.Length; i++)
{
Assert.AreEqual((int)xValues.GetValue(i), (int)yValues.GetValue(i));
}
I would flip the way you check around. It is easier to get a name from a value instead of a value from a name. Iterate over the values and check the names at the same time.
public static class EnumAssert
{
public static void AreEquivalent(Type x, Type y)
{
// Enum.GetNames and Enum.GetValues return arrays sorted by value.
var xValues = Enum.GetValues(x);
var yValues = Enum.GetValues(y);
Assert.AreEqual(xValues.Length, yValues.Length);
for (int i = 0; i < xValues.Length; i++)
{
var xValue = xValues.GetValue( i );
var yValue = yValues.GetValue( i );
Assert.AreEqual(xValue, yValue);
Assert.AreEqual( Enum.GetName( x, xValue ), Enum.GetName( y, yValue ) );
}
}
}

Related Links

Try To Code HMAC-SHA256 using C#.Net
Using OpenXML, how can I associate a list for data validation
How to avoid big factory functions everywhere when creating a plugin API?
Is it possible to execute C# code represented as string?
Postback url untriggered
Insert radio button selected value into database
Change property type using reflection
ASP.net c# Windows Authentication - With optional login for users logged into generic accounts
How to use Async methods of in a queryable generic repository?
Replace single quote if it is alone
Show notification in DragDrop method without maintaining sender's DoDragDrop
Model to match this JSON for deserialization, field names with dashes
Populate listbox using for loop of year starting with 1913 and ending with 2013
Drawing a line by mouse in a panel
Listbox with UserControl ItemTemplate always shows default values
Insert new xml node for specific id c#

Categories

HOME
url
blob
reference
gaussian
cryengine
crm
coordinates
thermal-printer
uml-designer
servicenow
cdn
pip
android-webview
vsixmanifest
sahi
cracking
phpexcel
dcos
ethereum
ndepend
k-means
reportlab
web-parts
windows-phone
openam
project-reactor
novacode-docx
project-structure
android-wifi
vala
angular2-pipe
gnu-screen
procedural-generation
handle
jira-zephyr
git-submodules
request-uri
ds-5
sampling
ansible-inventory
hippocms
kepserverex
rtsp
collation
text-extraction
reqif
spring-webflow-2
geminabox
ionic2-select
blackberry
nsalert
tsc
acceleo
stripes
xcode7.1
db2-zos
weblogic-maven-plugin
django-celery
archiva
paypal-subscriptions
matlab-engine
recode
transbase
taverna
firewire
transparentproxy
gcc-warning
firebase-tools
jad
xmla
information-hiding
formbuilder
dirname
starling-framework
misfire-instruction
raddatepicker
xrandr
wpa
exi
biztalk-deployment
blotter
linqdatasource
freeimage
shapado
diazo
socketasynceventargs
towerjs
visual-leak-detector
google-local-search
iphone-maps
p4.net
mysql-pconnect
carbon-emacs
projectgen
urlscan

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App