all files / core/decorators/ test-case-decorator.ts

100% Statements 18/18
100% Branches 6/6
100% Functions 3/3
100% Lines 15/15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41   1479× 582×     708×     708× 42×     42×     3992× 186×     186×       708×     708× 232×       708×         708×      
import "reflect-metadata";
 
export function TestCase(...testCaseArguments: Array<any>) {
  return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
 
    // check if this has been registered as a test already
    let tests: Array<any> = Reflect.getMetadata("alsatian:tests", target);
 
    // if there are no tests registered yet then register it
    if (!tests) {
      tests = [  {
         key: propertyKey
      } ];
      Reflect.defineMetadata("alsatian:tests", tests, target);
    }
    // otherwise add it to the register
    else if (tests.filter(test => test.key === propertyKey).length === 0) {
      tests.push( {
         key: propertyKey
      });
      Reflect.defineMetadata("alsatian:tests", tests, target);
    }
 
    // check if there are test cases already associated with this test
    let testCases: Array<any> = Reflect.getMetadata("alsatian:testcases", target, propertyKey);
 
    // if not create an empty array
    if (!testCases) {
      testCases = [];
    }
 
    // add the test case to the list
    testCases.unshift({
      arguments: testCaseArguments
    });
 
    // update the list of test cases
    Reflect.defineMetadata("alsatian:testcases", testCases, target, propertyKey);
};
}