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

Explicitly specifying the TaskScheduler for an implicitly scheduled method
DataGridView setting row height doesn't work
Retrieving data from webservice & displaying it in a listbox
How to detect double click on window title in c#
How Can i display the output of SQL “PRINT” Command in C#?
How to get the (.lnk) shortcut filepath in a program which started by the shortcut?
Is it possible to change variable while I'm sending it?
Updating bindings on properties that are automatically calculated
Combo box SQL database connection and show
Design pattern for redirecting dependencies
MVC3 Pre-Loading many images in view
how do i list all possible directories and paths to subfolders ignoring files
SynchronizationContext.Post not working inside BackgroundWorker
Download File from Given URL
Insert time data into SQL Server with C#
can i have two connection string in two different web config in one asp.net project

Categories

HOME
google-cloud-pubsub
azure-documentdb
system-calls
qemu
jenkins-pipeline
packet
reference
conceptual
arguments
redhawksdr
sitemap
ejb-3.0
spring-data-solr
activejdbc
openbsd
kairosdb
distinct
ng2-charts
hdf5
element
responsive
web-testing
nsoperation
k-means
ringcentral
orange
connectiq
ppp
oracle10g
sqldependency
osgi-bundle
microstrategy
android-scrollview
restler
apache-falcon
maven-2
kepserverex
jquery-inputmask
gem-fury
ssdp
electron-packager
definition
gradlew
togglebutton
denodo
flask-socketio
standard-error
multi-targeting
tsc
spring-social-twitter
fileopendialog
timefield
toggleclass
x12
jira-agile
netlink
db2-zos
mrunit
jde
chomsky-normal-form
nugetgallery
factorization
firefox-os
dday
google-finance-api
wiql
jcomponent
libgcrypt
transport
dnvm
stereotype
bungeecord
nexusdb
static-files
system.io.packaging
diffmerge
redmine-plugins
drawable
mvcgrid
carrier
codio
php-amqplib
xtify
facebook-likebox
randomaccessfile
objectdatasource
wpd
kademlia
brew
gpl
qt5.1
nsregularexpression
yii-components
report-viewer2010
netbiscuits
php-gettext
android-memory
inserthtml
project-files
overlays
n2
netbeans7.0
mass-emails
google-local-search
silverlight-2.0
system-analysis
uccapi
mysql-pconnect
focus-stealing
time-management

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