File: 10parse_args.t

package info (click to toggle)
libnumber-bytes-human-perl 0.11-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 140 kB
  • sloc: perl: 593; makefile: 2
file content (148 lines) | stat: -rwxr-xr-x 3,443 bytes parent folder | download | duplicates (3)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!perl -T

use strict;
use warnings;

use Test::More tests => 30;

# script for testing the internal sub &_parse_args()

use_ok('Number::Bytes::Human');

*_parse_args = \&Number::Bytes::Human::_parse_args;

## options to set BLOCK

is_deeply(
  _parse_args({}, { block => 1024 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { block_size => 1024 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { base => 1024 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { bs => 1024 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { block_1024 => 1 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { base_1024 => 1 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { 1024 => 1 }),
  { BLOCK => 1024 });

is_deeply(
  _parse_args({}, { block_1000 => 1 }),
  { BLOCK => 1000 });

is_deeply(
  _parse_args({}, { base_1000 => 1 }),
  { BLOCK => 1000 });

is_deeply(
  _parse_args({}, { 1000 => 1 }),
  { BLOCK => 1000 });

is_deeply(
  _parse_args({}, { bs => 1024000 }),
  { BLOCK => 1_024_000 });

# block + block_size
is_deeply(
  _parse_args({}, { block => 1024, block_size => 1000 }),
  { BLOCK => 1024 }, "block has precedence over block_size");

# block + block_1000
is_deeply(
  _parse_args({}, { block => 1024, block_1000 => 1 }),
  { BLOCK => 1024 }, "block has precedence over block_1000");

eval {
  my $ans = _parse_args({}, { block => 1010 });
  fail('block => 1010 should be bad');
};
like($@, qr/^invalid base/) if $@;

## options to set ROUND_*

my $ans;

my $dummy = sub { 'dummy' };
is_deeply(
  _parse_args({}, { round_function => $dummy }),
  { ROUND_FUNCTION => $dummy, ROUND_STYLE => 'unknown' });

is_deeply(
  _parse_args({}, { round_function => $dummy, round_style => 'dummy' }),
  { ROUND_FUNCTION => $dummy, ROUND_STYLE => 'dummy' });

$ans = _parse_args({}, { round_style => 'ceil' });
isa_ok($ans->{ROUND_FUNCTION}, 'CODE');
delete $ans->{ROUND_FUNCTION};
is_deeply($ans, { ROUND_STYLE => 'ceil' });

$ans = _parse_args({}, { round_style => 'floor' });
isa_ok($ans->{ROUND_FUNCTION}, 'CODE');
delete $ans->{ROUND_FUNCTION};
is_deeply($ans, { ROUND_STYLE => 'floor' });

eval {
  my $ans = _parse_args({}, { round_function => 1 });
  fail('round_function => 1 should be bad');
};
like($@, qr/^round function (.*) should be a code ref/, 'round_function => 1 is bad') if $@;

eval {
  my $ans = _parse_args({}, { round_function => {} });
  fail('round_function => {} should be bad');
};
like($@, qr/^round function (.*) should be a code ref/, 'round_function => {} is bad') if $@;

## OPTION SUFFIXES

my $suff = [];
is_deeply(
  _parse_args({}, { suffixes => $suff }),
  { SUFFIXES => $suff }, "suffixes => [] works");

## OPTION si
is_deeply(
  _parse_args({}, { si => 1, bs => 1000 }),
  { SI => 1, BLOCK => 1000 }, "si => 1, bs => 1000 works");

is_deeply(
  _parse_args({}, { si => 1, bs => 1024 }),
  { SI => 1, BLOCK => 1024 }, "si => 1, bs => 1024 works");

## option ZERO

is_deeply(
  _parse_args({}, { zero => '-' }),
  { ZERO => '-' }, "zero => '-' works");

is_deeply(
  _parse_args({ SUFFIXES => [ 'X' ]}, { zero => '0%S' }),
  { ZERO => '0X', SUFFIXES => [ 'X' ] },
  "zero => '0%S' works");

## option PRECISION

is_deeply(
  _parse_args({}, { precision => '2' }),
  { PRECISION => '2' }, "precision => '2' works");

## option PRECISION_CUTOFF

is_deeply(
  _parse_args({}, { precision_cutoff => '-1' }),
  { PRECISION_CUTOFF => '-1' }, "precision_cutoff => '-1' works");