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

How to return and retrieve object value in C#?
Google Offline Conversion Adwords Upload Error
C# unit testing for a NotFound via a GET
Is it possible to search for index of multi type ObservableCollection?
Adding the web service can not generate the Reference.cs
Capturing images through webcam in c# winform
C# LINQ nested select query
context.Wait, messages are are still delivered to endpoint rather than the method to wait on
How to make lines straight on a top-view image having all camera properties?
MVC Group by, it worked some days ago. But now I get the error : The object reference is not set to an instance of an object [duplicate]
Inserting values in SOAP envelope using c#
Insert a record Entity framework
Parsing LuisResult to get values field
How can I make add-in tab and function in one project
Issue with Databind() Visual Studio 2015
Entity Framework share linked table through inheritance is possible?

Categories

HOME
airflow
excel-formula
openacc
ldap
android-ndk
android-webview
subprocess
ejb-3.0
google-my-business-api
theory
apache-httpclient-4.x
dynamics-crm-2011
xmlhttprequest
popup
openscad
internet-explorer-7
powerpivot
django-rest-auth
passbook
jq
angular2-pipe
javaagents
prepros
gsuite
nsopenpanel
workflow-foundation
emr
foxpro
ponylang
campaign-monitor
respect-validation
motherboard
android-databinding
const
office-interop
compiled-query
rexx
api-key
ocpjp
spring-data-commons
google-chrome-storage
emacs24
e4
dna-sequence
seyren
kendo-scheduler
multi-targeting
recursive-query
qt3d
konakart
csc
hoare-logic
pebble-js
code-translation
overlapping
borland-c++
pagecontrol
vspackage
opennebula
stderr
jeet-grid
maven-archetype
beagleboard
xcode5
kss
diffmerge
nsurlrequest
cocos2d-x-2.x
im4java
yandex-api
image-preloader
kuka-krl
aspnet-compiler
cab
caroufredsel
google-checkout
latex-suite
nx
reference-manual
setcookie
qt5.1
wpml
expresso-store
states
certificate-revocation
sqlbuddy
stl-algorithm
overlays
sessiontracking
cinder
webdb
versions
uccapi
time-management
office-2003

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