Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enum validation

Purpose is to validate Enum.

namespace Test.Enums
{ 
    public enum Type
    { 
        Audi,
        Porsche,
        Peugeot
    }
}

I would like to write these values into an array so that I can compare them.

[Fact]
public void Test()
{
   //arrange
    string[] expected = {"Audi", "Porsche", "Peugeot"};
    string[] actual = new Test.Enums.Type();      

   //assert
    Assert.Equal(expected, actual);
}

How to correctly get enum values into an array to be able to compare them? I will be grateful for your help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Something like this:

using System.Linq;

...

[Fact]
public void Test()
{
    string[] expected = {"Audi", "Porsche", "Peugeot"};
    // Enum names
    string[] actual = Enums.GetNames<Test.Enums.Type>();      

    // Assert: note the when comparing collections / enumerations
    // we should use SequenceEqual 
    Assert.True(expected.SequenceEqual(actual));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading