File: when.t

package info (click to toggle)
libnumber-tolerant-perl 1.710-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 372 kB
  • sloc: perl: 757; makefile: 10
file content (40 lines) | stat: -rw-r--r-- 757 bytes parent folder | download | duplicates (2)
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
use Test::More;

use strict;
use warnings;

BEGIN {
  plan skip_all => "switch only in Perl 5.10 and newer"
      if $^V lt v5.10.0;
}
use if $] >= 5.010, feature => qw<switch>;
no if $] >= 5.018, warnings => "experimental::smartmatch";

use Number::Tolerant;

my $range = Number::Tolerant->new(4.75 => offset => (-0.25, 0.75));

guess($range, 5, 1);
guess($range, 5.5, 1);
guess($range, 5.6, 0);
guess($range, 4.5, 1);
guess($range, 4.4, 0);

sub guess {
  my ($range, $guess, $should_match) = @_;

  my $verb = $should_match ? 'matches' : q[doesn't match];
  my $msg = "guess $guess $verb $range in when";

  given ($guess) {
    when ($range) {
      ok $should_match, $msg;
    }
    default {
      ok !$should_match, $msg;
    }
  }

}

done_testing;