All files / src/auxiliary Validate.ts

100% Statements 22/22
100% Branches 12/12
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46    25x   25x 25x             25x 36x 36x 36x 29x   36x   36x 1x     35x   35x 1x     34x 2x   32x 1x       30x 1x     7x      
import { AutoClose } from "@jonloucks/contracts-ts/api/AutoClose";
import { Contract } from "@jonloucks/contracts-ts/api/Contract";
import { ContractException } from "@jonloucks/contracts-ts/api/ContractException";
import { Contracts } from "@jonloucks/contracts-ts/api/Contracts";
import { create as createContract } from "@jonloucks/contracts-ts/api/RatifiedContract";
import { contractsCheck } from "@jonloucks/contracts-ts/auxiliary/Checks";
 
/**
* A simple runtime validation of deployed implementation
*
* @param contracts the contracts to check
*/
export function validateContracts(contracts: Contracts): void {
  const validContracts: Contracts = contractsCheck(contracts);
  try {
    const contract: Contract<Date> = createContract<Date>({
      test: (instance: unknown): instance is Date => { return instance instanceof Date; }
    });
    const deliverableValue: Date = new Date();
 
    if (validContracts.isBound(contract)) {
      throw new ContractException("Contract should not be bound.");
    }
    {
      using bindReturn: AutoClose = validContracts.bind(contract, () => deliverableValue);
 
      if (null === bindReturn || bindReturn === undefined) {
        throw new ContractException("Contract bind returned null.");
      }
 
      if (!validContracts.isBound(contract)) {
        throw new ContractException("Contract should have been bound.");
      }
      if (deliverableValue !== validContracts.claim(contract)) {
        throw new ContractException("Contract claiming not working.");
      }
    }
 
    if (validContracts.isBound(contract)) {
      throw new ContractException("Contract unbinding not working.");
    }
  } catch (thrown: unknown) {
    ContractException.rethrow(thrown, "Contracts unexpected validation error.");
  }
}