| 1 | package Mason::t::Interp; | ||
| 2 | $Mason::t::Interp::VERSION = '2.23'; | ||
| 3 | 1 | ▶ | use Test::Class::Most parent => 'Mason::Test::Class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | use Capture::Tiny qw(capture); | ||
| 5 | |||
| 6 | { package MyInterp; use Moose; extends 'Mason::Interp'; __PACKAGE__->meta->make_immutable() } | ||
| 7 | $MyInterp::VERSION = '2.23'; | ||
| 8 | sub test_base_interp_class : Tests { | ||
| 9 | my $self = shift; | ||
| 10 | my $interp = $self->create_interp( base_interp_class => 'MyInterp' ); | ||
| 11 | is( ref($interp), 'MyInterp' ); | ||
| 12 | } | ||
| 13 | |||
| 14 | sub test_find_paths : Tests { | ||
| 15 | my $self = shift; | ||
| 16 | my $r1 = $self->temp_dir . "/r1"; | ||
| 17 | my $r2 = $self->temp_dir . "/r2"; | ||
| 18 | my $interp = $self->create_interp( comp_root => [ $r1, $r2 ] ); | ||
| 19 | my @files = | ||
| 20 | ( "$r1/foo.mc", "$r1/foo/bar.mc", "$r2/foo/baz.mc", "$r1/foo/blarg.mc", "$r2/foo/blarg.mc" ); | ||
| 21 | foreach my $file (@files) { | ||
| 22 | $self->mkpath_and_write_file( $file, " " ); | ||
| 23 | } | ||
| 24 | cmp_set( | ||
| 25 | [ $interp->all_paths("/") ], | ||
| 26 | [qw(/foo.mc /foo/bar.mc /foo/baz.mc /foo/blarg.mc)], | ||
| 27 | "all_paths(/)" | ||
| 28 | ); | ||
| 29 | cmp_set( | ||
| 30 | [ $interp->all_paths() ], | ||
| 31 | [qw(/foo.mc /foo/bar.mc /foo/baz.mc /foo/blarg.mc)], | ||
| 32 | "all_paths(/)" | ||
| 33 | ); | ||
| 34 | cmp_set( | ||
| 35 | [ $interp->all_paths("/foo") ], | ||
| 36 | [qw(/foo/bar.mc /foo/baz.mc /foo/blarg.mc)], | ||
| 37 | "all_paths(/foo)" | ||
| 38 | ); | ||
| 39 | cmp_set( [ $interp->all_paths("/bar") ], [], "all_paths(/bar)" ); | ||
| 40 | |||
| 41 | cmp_set( | ||
| 42 | [ $interp->glob_paths("/foo/ba*.mc") ], | ||
| 43 | [qw(/foo/bar.mc /foo/baz.mc)], | ||
| 44 | "glob_paths(/foo/ba*.mc)" | ||
| 45 | ); | ||
| 46 | cmp_set( [ $interp->glob_paths("/foo/bl*.mc") ], | ||
| 47 | [qw(/foo/blarg.mc)], "glob_paths(/foo/bl*.mc)" ); | ||
| 48 | cmp_set( [ $interp->glob_paths("/foo/d*") ], [], "glob_paths(/foo/d*)" ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub test_component_class_prefix : Tests { | ||
| 52 | my $self = shift; | ||
| 53 | |||
| 54 | my $check_prefix = sub { | ||
| 55 | my $interp = shift; | ||
| 56 | my $regex = "^" . $interp->component_class_prefix . "::"; | ||
| 57 | like( $interp->load('/foo.mc'), qr/$regex/, "prefix at beginning of path" ); | ||
| 58 | }; | ||
| 59 | |||
| 60 | $self->add_comp( path => '/foo.mc', src => 'foo' ); | ||
| 61 | |||
| 62 | my @interp = | ||
| 63 | map { $self->create_interp() } ( 0 .. 1 ); | ||
| 64 | ok( $interp[0]->component_class_prefix ne $interp[1]->component_class_prefix, | ||
| 65 | "different prefixes" ); | ||
| 66 | ok( $interp[0]->load('/foo.mc') ne $interp[1]->load('/foo.mc'), "different classnames" ); | ||
| 67 | |||
| 68 | $check_prefix->( $interp[0] ); | ||
| 69 | $check_prefix->( $interp[1] ); | ||
| 70 | |||
| 71 | $interp[2] = $self->create_interp( component_class_prefix => 'Blah' ); | ||
| 72 | is( $interp[2]->component_class_prefix, 'Blah', 'specified prefix' ); | ||
| 73 | $check_prefix->( $interp[2] ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub test_no_data_dir : Tests { | ||
| 77 | my $self = shift; | ||
| 78 | my $interp = Mason->new( comp_root => $self->comp_root ); | ||
| 79 | ok( -d $interp->data_dir ); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub test_bad_param : Tests { | ||
| 83 | my $self = shift; | ||
| 84 | throws_ok { $self->create_interp( foo => 5 ) } qr/Found unknown attribute/; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub test_comp_exists : Tests { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | $self->add_comp( path => '/comp_exists/one.mc', src => 'hi' ); | ||
| 91 | my $interp = $self->interp; | ||
| 92 | ok( $interp->comp_exists('/comp_exists/one.mc') ); | ||
| 93 | ok( !$interp->comp_exists('/comp_exists/two.mc') ); | ||
| 94 | throws_ok { $interp->comp_exists('one.mc') } qr/not an absolute/; | ||
| 95 | } | ||
| 96 | |||
| 97 | sub test_out_method : Tests { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | $self->add_comp( path => '/out_method/hi.mc', src => 'hi' ); | ||
| 101 | |||
| 102 | my $buffer = ''; | ||
| 103 | my $try = sub { | ||
| 104 | my ( $out_method, $expect_result, $expect_buffer, $expect_stdout, $desc ) = @_; | ||
| 105 | my ( $result, $stdout ); | ||
| 106 | my @params = ( $out_method ? ( { out_method => $out_method } ) : () ); | ||
| 107 | ($stdout) = capture { | ||
| 108 | $result = $self->interp->run( @params, '/out_method/hi' ); | ||
| 109 | }; | ||
| 110 | is( $stdout, $expect_stdout, "stdout - $desc" ); | ||
| 111 | is( $buffer, $expect_buffer, "buffer - $desc" ); | ||
| 112 | is( $result->output, $expect_result, "result->output - $desc" ); | ||
| 113 | }; | ||
| 114 | |||
| 115 | $try->( undef, 'hi', '', '', 'undef' ); | ||
| 116 | $try->( sub { print $_[0] }, '', '', 'hi', 'sub print' ); | ||
| 117 | $try->( sub { $buffer .= uc( $_[0] ) }, '', 'HI', '', 'sub buffer .=' ); | ||
| 118 | $try->( \$buffer, '', 'HIhi', '', '\$buffer' ); | ||
| 119 | |||
| 120 | $buffer = ''; | ||
| 121 | $self->setup_interp( out_method => sub { print scalar( reverse( $_[0] ) ) } ); | ||
| 122 | $try->( undef, '', '', 'ih', 'print reverse' ); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub test_no_source_line_numbers : Tests { | ||
| 126 | my $self = shift; | ||
| 127 | |||
| 128 | $self->test_parse( src => "hi\n<%init>my \$d = 0</%init>", expect => [qr/\#line/] ); | ||
| 129 | $self->setup_interp( no_source_line_numbers => 1 ); | ||
| 130 | $self->test_parse( src => "hi\n<%init>my \$d = 0</%init>", expect => [qr/^(?!(?s:.*)\#line)/] ); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub test_class_header : Tests { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | $self->setup_interp( class_header => '# header' ); | ||
| 137 | $self->test_parse( src => "hi", expect => [qr/\# header/] ); | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; | ||