File: 65mop-create-method.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 (62 lines) | stat: -rw-r--r-- 1,638 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
#!/usr/bin/perl

use v5.18;
use warnings;

use Test2::V0;

use Object::Pad 0.800 ':experimental(mop)';

class AClass {
   use Test2::V0;

   BEGIN {
      # Most of this test has to happen at BEGIN time before AClass gets
      # sealed
      my $classmeta = Object::Pad::MOP::Class->for_caller;

      my $methodmeta = $classmeta->add_method( 'method', sub {
         return "result";
      } );

      is( $methodmeta->name, "method", '$methodmeta->name' );

      like( dies { $classmeta->add_method( undef, sub {} ) },
         qr/^methodname must not be undefined or empty /,
         'Failure from ->add_method undef' );
      like( dies { $classmeta->add_method( "", sub {} ) },
         qr/^methodname must not be undefined or empty /,
         'Failure from ->add_method on empty string' );

      like( dies { $classmeta->add_method( 'method', sub {} ) },
         qr/^Cannot add another method named method /,
         'Failure from ->add_method duplicate' );

      {
         'magic' =~ m/^(.*)$/;
         my $methodmeta = $classmeta->add_method( $1, sub {} );
         'different' =~ m/^(.*)$/;
         is( $methodmeta->name, 'magic', '->add_method captures FETCH magic' );
      }

      $classmeta->add_method( 'cmethod', common => 1, sub {
         return "Classy result";
      } );
   }
}

{
   my $obj = AClass->new;
   is( $obj->method, "result", '->method works' );

   my $can = $obj->can('method');
   is( ref($can), 'CODE', '->can("method") returns coderef' );
   is( $obj->$can, 'result', '... which works' );
}

# common method
{
   is( AClass->cmethod, "Classy result", '->cmethod works' );
}

done_testing;