File: 11method-signatures.t

package info (click to toggle)
libobject-pad-perl 0.821-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ansic: 3,361; perl: 3,328; pascal: 28; makefile: 3
file content (92 lines) | stat: -rw-r--r-- 1,822 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
#!/usr/bin/perl

use v5.18;
use warnings;

use Test2::V0;

BEGIN {
   $] >= 5.026000 or plan skip_all => "No parse_subsignature()";
}

use Object::Pad 0.800;

class List {
   field @values;

   method push ( @more ) { push @values, @more }
   method nshift ( $n )  { splice @values, 0, $n }
   method empty ()       { @values = (); }
}

{
   my $l = List->new;
   $l->push(qw( a b c d ));
   is( [ $l->nshift( 2 ) ],
      [qw( a b )],
      '$l->nshift yields values' );
}

class Greeter {
   field $_who;

   BUILD ( %args ) {
      $_who = $args{who};
   }

   method greet ( $message = "Hello, $_who" ) {
      return $message;
   }
}

{
   my $g = Greeter->new(who => "unit test");

   is( $g->greet, "Hello, unit test",
      'subroutine signature default exprs can see instance fields'
   );
}

{
   my @keys;

   class WithAdjustParams {
      ADJUSTPARAMS ( $params ) { @keys = sort keys %$params; %$params = () }
   }

   WithAdjustParams->new( x => 1, y => 2, z => 3 );
   is( \@keys, [qw( x y z )], 'Keys captured from $params' );
}

{
   my $warnings;
   my $LINE;

   BEGIN { $SIG{__WARN__} = sub { $warnings .= $_[0] }; }
   class WithAdjustSignature {
      $LINE = __LINE__+1;
      ADJUST ( $params ) { }
   }
   BEGIN { undef $SIG{__WARN__}; }

   like( $warnings, qr/^Use of ADJUST \(signature\) \{BLOCK\} is now deprecated at \S+ line $LINE\./,
      'ADJUST (signature) { BLOCK } raises a warning' );
}

# RT158048
{
   my @args;
   my $class_during_method;
   class WithCommonMethods {
      method cmeth :common ( @rest ) {
         $class_during_method = $class;
         @args = @rest;
      }
   }

   WithCommonMethods->cmeth( 1, 2, 3 );
   is( \@args, [ 1 .. 3 ], 'args to :common method' );
   is( $class_during_method, "WithCommonMethods", '$class during :common method' );
}

done_testing;