← Back to index

lib/BDD/Behave/Matcher/Exception.rakumod

50 / 52 lines covered (96.2%)

1unit module BDD::Behave::Matcher::Exception;23use BDD::Behave::Matcher;4use BDD::Behave::TypeName;56class RaiseErrorMatcher does Matcher is export {7  has Mu $.expected-type;8  has Bool $.has-type = False;9  has $.expected-message;10  has $.raised-exception is rw;11  has Bool $.callable-given is rw = True;12  has Str $.miss-reason is rw;1314  method !miss(Str $reason --> Bool) {15    $!miss-reason = $reason;16    False;17  }1819  method !message-matches($actual-message --> Bool) {20    given $!expected-message {21      when Regex { ?($actual-message ~~ $_) }22      default    { $actual-message eq $_.Str }23    }24  }2526  method check-captured(--> Bool) {27    $!miss-reason = Str;28    return self!miss('non-callable') unless $!callable-given;29    return self!miss('none') unless $!raised-exception.defined;3031    if $!has-type && !($!raised-exception ~~ $!expected-type) {32      return self!miss('type');33    }3435    if $!expected-message.defined36        && !self!message-matches($!raised-exception.message) {37      return self!miss('message');38    }3940    True;41  }4243  method matches($actual --> Bool) {44    $!raised-exception = Nil;45    $!callable-given   = ?($actual ~~ Callable);46    return self!miss('non-callable') unless $!callable-given;4748    try {49      $actual.();50      CATCH {51        default { $!raised-exception = $_; }52      }53    }54    self.check-captured;55  }5657  method !head(--> Str) {58    $!has-type ?? "raise " ~ short-type-name($!expected-type) !! "raise an error";59  }6061  method !message-clause(--> Str) {62    given $!expected-message {63      when Regex { " with message matching " ~ $_.raku }64      default    { " with message " ~ $_.Str.raku }65    }66  }6768  method description(--> Str) {69    my $d = self!head;70    $!expected-message.defined ?? $d ~ self!message-clause !! $d;71  }7273  method !raised-detail(--> Str) {74    "{short-type-name($!raised-exception)}: {$!raised-exception.message}";75  }7677  method failure-message($actual --> Str) {78    unless $!callable-given {79      return "expected a Callable, but got " ~ $actual.raku;80    }81    given $!miss-reason {82      when 'none' {83        "expected block to " ~ self.description ~ ", but none was raised";84      }85      when 'type' | 'message' {86        "expected block to " ~ self.description87          ~ ", but raised " ~ self!raised-detail;88      }89      default {90        "expected block to " ~ self.description;91      }92    }93  }9495  method failure-message-negated($actual --> Str) {96    my $head = "expected block not to " ~ self.description;97    if $!raised-exception.defined {98      $head ~ ", but one was raised (" ~ self!raised-detail ~ ")";99    } else {100      $head ~ ", but one was raised";101    }102  }103104  method expected-value(--> Mu) {105    if $!has-type {106      $!expected-type;107    } elsif $!expected-message.defined {108      $!expected-message;109    } else {110      Nil;111    }112  }113}