scope the classes
[gitmo/MooseX-Types-Path-Class.git] / t / 02.getopt.t
1
2 use warnings FATAL => 'all';
3 use strict;
4 use English qw(-no_match_vars);
5
6 eval { require MooseX::Getopt; };
7 if ($EVAL_ERROR) {
8     plan( skip_all => 'MooseX::Getopt required for this test' );
9 }
10
11 {
12
13     package Foo;
14     use Moose;
15     with 'MooseX::Getopt';
16     use MooseX::Types::Path::Class;
17
18     has 'dir' => (
19         is       => 'ro',
20         isa      => 'Path::Class::Dir',
21         required => 1,
22         coerce   => 1,
23     );
24
25     has 'file' => (
26         is       => 'ro',
27         isa      => 'Path::Class::File',
28         required => 1,
29         coerce   => 1,
30     );
31 }
32
33 {
34
35     package Bar;
36     use Moose;
37     with 'MooseX::Getopt';
38     use MooseX::Types::Path::Class qw( Dir File );
39
40     has 'dir' => (
41         is       => 'ro',
42         isa      => Dir,
43         required => 1,
44         coerce   => 1,
45     );
46
47     has 'file' => (
48         is       => 'ro',
49         isa      => File,
50         required => 1,
51         coerce   => 1,
52     );
53 }
54
55 package main;
56
57 use Test::More;
58 use Path::Class;
59 plan tests => 20;
60
61 my $dir = dir('', 'tmp');
62 my $file = file('', 'tmp', 'foo');
63
64 my $check = sub {
65     my $o = shift;
66     isa_ok( $o->dir, 'Path::Class::Dir' );
67     cmp_ok( $o->dir, 'eq', "$dir", "dir is $dir" );
68     isa_ok( $o->file, 'Path::Class::File' );
69     cmp_ok( $o->file, 'eq', "$file", "file is $file" );
70 };
71
72 for my $class (qw(Foo Bar)) {
73     my $o;
74
75     $o = $class->new( dir => "$dir", file => [ '', 'tmp', 'foo' ] );
76     isa_ok( $o, $class );
77     $check->($o);
78     @ARGV = qw(
79         --dir
80         /tmp
81         --file
82         /tmp/foo
83     );
84     $o = $class->new_with_options;
85     isa_ok( $o, $class );
86     $check->($o);
87 }
88