Skip to content
LosManos edited this page Nov 22, 2022 · 7 revisions

CompulsoryCow.Permutation

Does

Permutate method for permutation all possible values/objects sent to it.
It takes a list of list where a latter list is the possible values/objects. Use it for permutating all possible values to send to a method.
See the test methods for example of its usage. When you get your head around it, it is quite nifty. Typically used for testing all possible inputs to a method.

Permutate

static IEnumerable<Parameters> Permutate(IEnumerable<Parameters> parametersCollection)

The problem solved

The thought of usage is for testing, to make up all possible parameter inputs for a method and then permutate them.

(The same result could be achieved by writing a bunch of nested loops for hand but what is the fun in that?)

Example of usage:

If the signature of the method to test looks like:

bool Authenticate( bool isUserLoggedOn, RoleEnum role, string companyName )

and we know that having a blank companyName has a business value;
the code would be:

var permutations = Permutation.Permutate(
    new object[][]{
        Permutation.AllBools(), 
        Permutation.AllIEnumItems<Role>(),
        new string[]{"", "whatever"}
    });
    foreach( perm in permutation ){
        var params = perm.ToList();
        var isUserLoggedOn = (bool)params[0];
        var role = (Role)params[1];
        var companyName = (string)params[2[;
        ...
    }

And a test would look like:

public static IEnumerable<object[]> TestData()
{
    return Permutation.Permutate(
        Permutation.AllBools(), 
        Permutation.AllIEnumItems<Role>(),
        new string[]{"", "whatever"}
    ).ToTestData();
}

[Theory]
[InlineData( nameof( TestData ) )]
public void Can_Authorise( bool isUserLoggedOn, Role role, string companyName )
{
    ...

For more example of usage, see test code.

Comments

It would probably be unwise to have an System.Int32 or System.Char as input parameters as the permutations would be ridiculously many.

A System.String as input parameter would be even worse as it has an infinit number of possible values.

Code

Permutation Scroll

Clone this wiki locally