Skip to content

Commit c3c7b17

Browse files
committed
add test7
1 parent 732db42 commit c3c7b17

File tree

8 files changed

+420
-5
lines changed

8 files changed

+420
-5
lines changed

META.json

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
},
5050
"test" : {
5151
"requires" : {
52+
"Test::Exception" : "0.43",
5253
"Test::More" : "1.302135"
5354
}
5455
}

cpanfile

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ requires 'Exception::Tiny', '0.2.1';
44

55
on 'test' => sub {
66
requires 'Test::More', '1.302135';
7+
requires 'Test::Exception', '0.43';
78
};
89

lib/Scalish.pm

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use Scalish::Either::Right;
99
use Scalish::Either::Left;
1010

1111
use Exporter qw( import );
12-
our @EXPORT_OK = qw( option some none right left for_each for_yield );
12+
our @EXPORT_OK = qw( option some none right left for_each for_yield );
13+
our %EXPORT_TAGS = ( all => \@EXPORT_OK );
1314

1415
sub option($) {
1516
defined $_[0] ? Scalish::Option::Some->new( $_[0] ) : Scalish::Option::None->new;

lib/Scalish/Either/Left.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ sub join_right {
3737

3838
sub swap {
3939
my $self = shift;
40-
Either::Right->new( $self->{content} );
40+
Scalish::Either::Right->new( $self->{content} );
4141
}
4242

4343
sub match {

lib/Scalish/Option/Some.pm

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ sub fold {
3939
# override
4040
sub flatten {
4141
my $self = shift;
42-
$self->{content}->isa('Option::Option')
42+
$self->{content}->isa('Scalish::Option::Option')
4343
? $self->{content}
44-
: Carp::croak '$self->{content} is not Option::Option type';
44+
: Carp::croak '$self->{content} is not Scalish::Option::Option type';
4545
}
4646

4747
# override
@@ -86,7 +86,7 @@ sub to_left {
8686
# override
8787
sub to_list {
8888
my $self = shift;
89-
if ( $self->{content}->isa('Option::Option') ) {
89+
if ( $self->{content}->isa('Scalish::Option::Option') ) {
9090
( $self->{content}, $self->{content}->to_list );
9191
}
9292
else {

t/01_option.t

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
use Scalish::Exporter;
2+
use Test::More;
3+
use Test::Exception;
4+
5+
use Scalish qw( option some none );
6+
7+
subtest 'exists' => sub {
8+
my $some = some(5);
9+
ok $some->exists(sub { $_ < 10 });
10+
ok !$some->exists(sub { $_ > 10 });
11+
my $none = none;
12+
ok !$none->exists(sub { $_ == 0 });
13+
};
14+
15+
subtest 'fold' => sub {
16+
ok my $none = option(undef);
17+
dies_ok { $none->fold(sub { die 'undef' })->(sub { 'some' }) };
18+
ok my $some = option('string');
19+
is $some->fold(sub { die 'undef' })->(sub { $_ . '_value' }), 'string_value';
20+
};
21+
22+
subtest 'foreach' => sub {
23+
my $some = option("aaa");
24+
ok! $some->foreach(sub { $_ . '+aaa' });
25+
dies_ok { $some->foreach(sub { die }) };
26+
my $none = option(undef);
27+
lives_ok { $none->foreach(sub { die }) };
28+
};
29+
30+
subtest 'flatten' => sub {
31+
32+
my $func = sub {
33+
my ($c1, $c2) = @_;
34+
my ($v1, $v2) = (option($c1), option($c2));
35+
$v1->map(sub { my $s1 = $_; $v2->map(sub { my $s2 = $_; $s1 * $s2 }) })->flatten;
36+
};
37+
38+
$func->(5, 6)->map(sub { is $_, 30 });
39+
ok $func->(5, undef)->isa('Scalish::Option::None');
40+
ok $func->(undef, 6)->isa('Scalish::Option::None');
41+
ok $func->(undef, undef)->isa('Scalish::Option::None');
42+
43+
my $func2 = sub {
44+
my @args = @_;
45+
my ($v1, $v2, $v3, $v4) = map { option($_) } @args;
46+
$v1->map(sub {
47+
my $s1 = shift;
48+
$v2->map(sub {
49+
my $s2 = shift;
50+
$v3->map(sub {
51+
my $s3 = shift;
52+
$v4->map(sub {
53+
my $s4 = shift;
54+
$s1 * $s2 * $s3 * $s4;
55+
})
56+
})->flatten
57+
})->flatten
58+
})->flatten
59+
};
60+
61+
my $some = $func2->(6, 4, 2, 5);
62+
$some->map(sub { is $_, 240 });
63+
ok $func2->(6, 4, 2, undef)->isa('Scalish::Option::None');
64+
ok $func2->(6, 4, undef, 5)->isa('Scalish::Option::None');
65+
ok $func2->(undef, 4, 2, 5)->isa('Scalish::Option::None');
66+
ok $func2->(undef, undef, undef, undef)->isa('Scalish::Option::None');
67+
68+
};
69+
70+
subtest 'flat_map' => sub {
71+
72+
my $func = sub {
73+
my @args = @_;
74+
my ($v1, $v2, $v3, $v4) = map { option($_) } @args;
75+
$v1->flat_map(sub {
76+
my $s1 = shift;
77+
$v2->flat_map(sub {
78+
my $s2 = shift;
79+
$v3->flat_map(sub {
80+
my $s3 = shift;
81+
$v4->map(sub {
82+
my $s4 = shift;
83+
$s1 * $s2 * $s3 * $s4;
84+
})
85+
})
86+
})
87+
})
88+
};
89+
90+
my $some = $func->(6, 4, 2, 5);
91+
$some->map(sub { is $_, 240 });
92+
ok $func->(6, 4, 2, undef)->isa('Scalish::Option::None');
93+
ok $func->(6, 4, undef, 5)->isa('Scalish::Option::None');
94+
ok $func->(undef, 4, 2, 5)->isa('Scalish::Option::None');
95+
ok $func->(undef, undef, undef, undef)->isa('Scalish::Option::None');
96+
97+
};
98+
99+
subtest 'get' => sub {
100+
my $s = some(6);
101+
is $s->get, 6;
102+
my $n = none;
103+
dies_ok { $n->get };
104+
};
105+
106+
subtest 'get_or_else' => sub {
107+
my $s = option(5);
108+
is $s->get_or_else(4), 5;
109+
my $n = none;
110+
is $n->get_or_else(4), 4;
111+
};
112+
113+
subtest 'is_defined' => sub {
114+
ok some(6)->is_defined;
115+
ok !none->is_defined;
116+
};
117+
118+
subtest 'is_empty' => sub {
119+
ok !some(10)->is_empty;
120+
ok none->is_empty;
121+
};
122+
123+
subtest 'map' => sub {
124+
my $s = option(9);
125+
ok $s->map(sub { is $_, 9; $_ * 9 })->isa('Scalish::Option::Some');
126+
my $n = none;
127+
lives_ok { $n->map(sub { die }) };
128+
};
129+
130+
subtest 'match' => sub {
131+
my $option = option('SOMETHING');
132+
my $ret = $option->match(
133+
Some => sub { 200 },
134+
None => sub { 404 },
135+
);
136+
is $ret, 200;
137+
138+
my $none = option(undef);
139+
my $ret_2 = $none->match(
140+
Some => sub { 200 },
141+
None => sub { 404 },
142+
);
143+
is $ret_2, 404;
144+
};
145+
146+
subtest 'to_left' => sub {
147+
my $s = some('o');
148+
my $sl = $s->to_left('p');
149+
ok $sl->isa('Scalish::Either::Left');
150+
lives_ok { $sl->map(sub { die }) };
151+
my $n = none;
152+
my $nl = $n->to_left('p');
153+
ok $nl->isa('Scalish::Either::Right');
154+
$nl->map(sub { is $_, 'p' });
155+
};
156+
157+
subtest 'to_list' => sub {
158+
ok my $opt = option(option(option(100)));
159+
my $val = do {
160+
my ($opt1, $has_100) = $opt->to_list;
161+
$has_100;
162+
}->get_or_else(404);
163+
is $val, 100;
164+
ok my $none = option(option(none));
165+
my $val2 = do {
166+
my ($opt1, $has_100) = $none->to_list;
167+
$has_100;
168+
}->get_or_else(404);
169+
is $val2, 404;
170+
};
171+
172+
subtest 'to_right' => sub {
173+
my $s = some('o');
174+
my $sl = $s->to_right('p');
175+
ok $sl->isa('Scalish::Either::Right');
176+
$sl->map(sub { is $_, 'o' });
177+
my $n = none;
178+
my $nl = $n->to_right('p');
179+
ok $nl->isa('Scalish::Either::Left');
180+
lives_ok { $nl->map(sub { die }) };
181+
};
182+
183+
done_testing;
184+

0 commit comments

Comments
 (0)