File: 12_list.t

package info (click to toggle)
libiterator-simple-perl 0.07-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 180 kB
  • sloc: perl: 1,116; makefile: 2
file content (98 lines) | stat: -rwxr-xr-x 1,463 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
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
use Test::More tests => 8;

use strict;
use warnings;

use Iterator::Simple qw(list iterator);

{
	my $now = 0;
	my $itr = iterator {
		return if $now > 5;
		return $now++;
	};
	is_deeply list($itr), [0,1,2,3,4,5], 'list(iterator)';
}

{
	my @ary = ('dogs', 'cats','cow');
	is_deeply list(\@ary), ['dogs', 'cats', 'cow'], 'list(ARRAY REF)';
}

{
	is_deeply list(\*DATA), ["foo\n", "bar\n", "baz\n"], 'list(GLOB)';
}

{
	my $foo = Foo->new();
	is_deeply list($foo), ['hoge', 'hage'], 'list(@{} overloaded)';
}

{
	my $bar = Bar->new();
	is_deeply list($bar) , [0,1,2,3], 'list(__iter__ implemented)';
}

{
	my $baz = Baz->new();
	is_deeply list($baz) , [0,1,2,3,4,5], 'list(<> overloaded)';
}

{
	my $fiz = Fiz->new();
	is_deeply list($fiz) , [0,1,2,3,4,5,6,7], 'list(next implemented)';
}

{
	is_deeply list(), [], 'list()';
}

{
	package Foo;
	sub new { bless {i => 0}, $_[0]; }
	use overload (
		'@{}' => sub { [ 'hoge', 'hage'] },
	);
}

{
	package Bar;
	use Iterator::Simple qw(iterator);
	sub new { bless {i => 0}, $_[0]; }
	sub __iter__ {
		my($self) = @_;
		my $k = 0;
		iterator {
			return if $k > 3;
			return $k++;
		}
	}
}

{
	package Baz;
	sub new { bless {i => 0}, $_[0]; }
	use overload (
		'<>' => sub {
			my($self) = @_;
			return if $self->{i} > 5;
			return $self->{i}++;
		}
	);
}

{
	package Fiz;
	sub new { bless {i => 0}, $_[0]; }
	sub next {
		my($self) = @_;
		return if $self->{i} > 7;
		return $self->{i}++;
	}
}


__END__
foo
bar
baz