FlexUnit 4 makes use of the Request class to handle sorting of TestCases. By default a Request class creates and returns it's own SortingRequest which makes use of the 'order' custom metadata to determine how all tests should be sorted. This method is called defaultSortFunction and is located in the MetadataSorter class.

/**
* Compares its two arguments for order. Returns a negative integer, zero, or a positive integer 
* as the first argument is less than, equal to, or greater than the second.
* 
* @param o1 <code>IDescription</code> the first object to be compared.
* @param o2 <code>IDescription</code> the second object to be compared.
* */
public static function defaultSortFunction( o1:IDescription, o2:IDescription ):int {
     var a:Number;
     var b:Number; 

     var o1Meta:XMLList = o1.getAllMetadata();
     var o2Meta:XMLList = o2.getAllMetadata();
     
     //Determine if the first object has an order
     if ( o1Meta ) { 
          a = getOrderValueFrom( o1 );
     } else {
          a = 0;
     }
               
     //Determine if the second object has an order
     if ( o2Meta ) {
          b = getOrderValueFrom( o2 );
     } else {
          b = 0;
     }
               
     //Determine the ordering of the two respected objects
     if (a < b)
          return -1;
     if (a > b)
          return 1;
     
     return 0;
}

It is possible to replace this functionality by passing your own compare function (comparable to a Flex DataGrid's sortCompareFunction) to the method sortWith on the Request. Building off the example shown in the Request example, here's the same function only in addition we're adding a sortWith method call and passing it our function 'yourSortCompareFunction'.


     public function runMe():void {
          var myRequest : Request;
          //Setting my variable to the Request object return from classes();              
          myRequest = Request.classes( SampleSuite, SampleTest1 ); 
          
          myRequest.sortWith( yourSortCompareFunction ); 

          core = new FlexUnitCore();
          //Listener for the UI
          core.addListener( new UIListener( uiListener ));
          core.run( myRequest );
     }
     var newRequest:Request = request.sortWith( yourSortCompareFunction );

The function must of course adhere to the same method signature as defaultSortFunction, accepting two IDecription objects and returning an integer. Once you override the default functionality you are no longer using the order attribute to determine the sort placement and must grab the value from the IDescription MetaData to do so (see methods getAllMetadata() and getOrderValueFrom() in the code snippet above). This allows for great versatility when it comes to sorting your tests.

  • No labels