Sunday, January 3, 2021

PowerShell: Interfaces

A rudimentary unit test class, PSUnit, was introduced in PowerShell: Unit Test Functions and Accessing the Invoking Code's Line Number. This current post extends the PSUnit class to support the testing of collections and arrays by making use of an well known interface, ICollection. All the code associated with the PSUnit class is found in the post PowerShell: A Practical Example (Logging and Testing).

It would not be feasible to implement an AreEqual class for every collection exposed by .NET and therefore accessible to PowerShell. Examples of the plethora of collection available include:

  • Namespace System.Collections 
    • Array
    • Queue
    • Stack and 
  • Namespace System.Collections.Generic 
    • LinkedList<T>
    • List<T>
    • Queue<T> 

.NET collections are derived from a set of common interfaces including System.Collection.ICollection. Interfaces define a set of methods and properties that class is required to implement. A practical approach to testing collections is to compare the values in the collection but using interfaces. This means that an AreEqual method that compares using ICollection can test any class that implements ICollection. The ICollection interface is documented at ICollection Interface. The documentation demonstrating the classes that implement the ICollection Interface include, but are not limited to, the following:


The above list of classes is just a subset of the classes derived from System.Collection.ICollection,

The documentation for ICollection shows the properties and methods required to be implemented by classes derived from this interface:


The ICollection interface inherits from the IEnumerable interface as the documentation demonstrates:


This means that any class that implement ICollection and its properties (Count, IsReadOnly) and methods (Add, Clear, Contains, CopyTo, and Remove) must also implement the properties and methods of IEnumerable (method GetEnumerator).

The PSUnit methods used to compare collections are shown below. The AreEqualCollection method of the PSUnit class is implemented at lines 147 to 187 in the code snippets below:



 

The first two parameters of AreEqualCollection are of type ICollection which means the method can be used to test arrays, lists, and dozens of collections types. Line 161 above shows the AreEqualCollection  method using the Count property of interface ICollection. Lines 174 to 176 show calling GetEnumerator from ICollection's base-interface, IEnumerable. The GetEnumerator method returns an IEnumerator instance which in lines 179-186 iterates through the the collection values to be compared.

The function AreEqualCollection wraps the AreEqualCollection method of the PSUnit class. The AreEqualCollection function is  shown below:


The code to test the AreEqualCollection function is as follows:



As stated previously, all code from this post can be found in PowerShell: A Practical Example (Logging and Testing.

No comments :

Post a Comment