1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | ################################################################################ |
---|
7 | # A mock Fcm::ConfigSystem object |
---|
8 | { |
---|
9 | package MockConfigSystem; |
---|
10 | use base qw{Fcm::ConfigSystem}; |
---|
11 | |
---|
12 | our $LATEST_INVOKED_INSTANCE; |
---|
13 | |
---|
14 | ############################################################################ |
---|
15 | # Returns the arguments to the last invoke() call |
---|
16 | sub get_invoke_args { |
---|
17 | my ($self) = @_; |
---|
18 | return $self->{invoke_args}; |
---|
19 | } |
---|
20 | |
---|
21 | ############################################################################ |
---|
22 | # Does nothing but captures the arguments |
---|
23 | sub invoke { |
---|
24 | my ($self, %args) = @_; |
---|
25 | $LATEST_INVOKED_INSTANCE = $self; |
---|
26 | $self->{invoke_args} = \%args; |
---|
27 | return 1; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | use Cwd; |
---|
32 | use Test::More qw{no_plan}; |
---|
33 | |
---|
34 | main(); |
---|
35 | |
---|
36 | sub main { |
---|
37 | my $class = 'Fcm::CLI::Invoker::ConfigSystem'; |
---|
38 | use_ok($class); |
---|
39 | test_invoke($class); |
---|
40 | } |
---|
41 | |
---|
42 | ################################################################################ |
---|
43 | # Tests normal usage of invoke() |
---|
44 | sub test_invoke { |
---|
45 | my ($class) = @_; |
---|
46 | my $prefix = "invoke"; |
---|
47 | my %TEST = ( |
---|
48 | test1 => { |
---|
49 | command => 'pig', |
---|
50 | options => {'egg' => 1}, |
---|
51 | arguments => ['bacon'], |
---|
52 | expected_options => {FOO => undef, BAR_BAZ => undef, EGGS => 1}, |
---|
53 | expected_arguments => 'bacon', |
---|
54 | }, |
---|
55 | test2 => { |
---|
56 | command => 'pig', |
---|
57 | options => {'foo' => 1, 'bar-baz' => 1}, |
---|
58 | arguments => [], |
---|
59 | expected_options => {FOO => 1, BAR_BAZ => 1, EGGS => undef}, |
---|
60 | expected_arguments => cwd(), |
---|
61 | } |
---|
62 | ); |
---|
63 | for my $key (keys(%TEST)) { |
---|
64 | my $invoker = $class->new({ |
---|
65 | command => $TEST{$key}{command}, |
---|
66 | options => $TEST{$key}{options}, |
---|
67 | arguments => $TEST{$key}{arguments}, |
---|
68 | impl_class => 'MockConfigSystem', |
---|
69 | cli2invoke_key_map => { |
---|
70 | 'foo' => 'FOO', 'bar-baz' => 'BAR_BAZ', 'egg' => 'EGGS', |
---|
71 | }, |
---|
72 | }); |
---|
73 | isa_ok($invoker, 'Fcm::CLI::Invoker::ConfigSystem', "$prefix: $key"); |
---|
74 | $invoker->invoke(); |
---|
75 | my $config_system_instance = $MockConfigSystem::LATEST_INVOKED_INSTANCE; |
---|
76 | isa_ok( |
---|
77 | $config_system_instance, |
---|
78 | 'Fcm::ConfigSystem', |
---|
79 | "$prefix: $key: Fcm::ConfigSystem", |
---|
80 | ); |
---|
81 | is( |
---|
82 | $config_system_instance->cfg()->src(), |
---|
83 | $TEST{$key}{expected_arguments}, |
---|
84 | "$prefix: $key: cfg()->src()", |
---|
85 | ); |
---|
86 | is_deeply( |
---|
87 | $config_system_instance->get_invoke_args(), |
---|
88 | $TEST{$key}{expected_options}, |
---|
89 | "$prefix: $key: invoke args", |
---|
90 | ); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | __END__ |
---|