1use Test::More;
2use strict; use warnings FATAL => 'all';
3
4use List::Objects::WithUtils 'array';
5
6ok array->sort->is_empty, 'empty array sort ok';
7
8my $arr = array(4, 2, 3, 1);
9my $sorted = $arr->sort(sub { $_[0] <=> $_[1] });
10is_deeply
11  [ $sorted->all ],
12  [ 1, 2, 3, 4 ],
13  'sort with positional args ok';
14
15
16$sorted = $arr->sort;
17is_deeply
18  [ $sorted->all ],
19  [ 1, 2, 3, 4 ],
20  'sort with default sub ok';
21
22is_deeply [ $arr->sort(undef)->all ], [ $arr->sort->all ],
23  'sort non-subroutine (false) arg ok';
24eval {; $arr->sort(1) };
25ok $@, 'sort non-subroutine (true) arg dies ok';
26
27my $warned;
28$SIG{__WARN__} = sub { $warned = shift };
29
30$sorted = $arr->sort(sub { $a <=> $b });
31is_deeply
32  [ $sorted->all ],
33  [ 1, 2, 3, 4 ],
34  'sort with named args ok';
35
36ok !$warned, 'using $a/$b produced no warnings'
37  or fail 'using $a/$b produced warning: '.$warned;
38
39done_testing;
40