beOfType<U> method

U? beOfType<U>()

Asserts that the object is not of the specified type U

Implementation

U? beOfType<U>() {
  final runtimeType = subject.runtimeType;
  final isNamesEqual = subject.runtimeType.toString() == U.toString();
  if (isReversed) {
    if (subject.runtimeType == U && isNamesEqual) {
      throw ShouldlyTestFailureError(
        '\n$subjectLabel\n    $runtimeType\nshould not be an instantiation of Type\n    `$U`\nbut does',
      );
    }
  } else {
    if (subject.runtimeType != U && !isNamesEqual) {
      throw ShouldlyTestFailureError(
        '\n$subjectLabel\n    $runtimeType\nshould be an instantiation of Type\n    `$U`\nbut does not',
      );
    }
  }

  try {
    final x = subject as U;
    return x;
  } catch (e) {
    // TODO: catch exact type cast error - example myIntNumber.should.not.beOfType<double>();
    return null;
  }
}