| 1 | package MOP4Import::Util::CallTester; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Test::More; | ||
| 6 | use Capture::Tiny (); | ||
| 7 | use Test::Exit; | ||
| 8 | |||
| 9 | use MOP4Import::Base::Configure -as_base, [fields => qw/target_object/]; | ||
| 10 | use MOP4Import::Util qw/terse_dump shallow_copy/; | ||
| 11 | |||
| 12 | sub make_tester { | ||
| 13 | my ($pack, $app) = @_; | ||
| 14 | my MY $self = $pack->new; | ||
| 15 | $self->{target_object} = $app; | ||
| 16 | $self; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub returns_in_scalar { | ||
| 20 | (my MY $self, my ($call, $expect)) = @_; | ||
| 21 | my ($method, @args) = @$call; | ||
| 22 | my @savedArgs = map {shallow_copy($_)} @args; | ||
| 23 | is_deeply(scalar($self->{target_object}->$method(@args)), $expect | ||
| 24 | , sprintf("scalar call:%s(%s) expect:%s" | ||
| 25 | , $method | ||
| 26 | , join(", ", map(terse_dump($_), @savedArgs)) | ||
| 27 | , terse_dump($expect))); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub returns_in_list { | ||
| 31 | (my MY $self, my ($call, $expect)) = @_; | ||
| 32 | my ($method, @args) = @$call; | ||
| 33 | my @savedArgs = map {shallow_copy($_)} @args; | ||
| 34 | is_deeply([$self->{target_object}->$method(@args)], $expect | ||
| 35 | , sprintf("list call:%s(%s) expect:%s" | ||
| 36 | , $method | ||
| 37 | , join(", ", map(terse_dump($_), @savedArgs)) | ||
| 38 | , terse_dump($expect))); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub captures { | ||
| 42 | (my MY $self, my ($call, $expect)) = @_; | ||
| 43 | my ($method, @args) = @$call; | ||
| 44 | my @savedArgs = map {shallow_copy($_)} @args; | ||
| 45 | my ($got, $stderr, @res); | ||
| 46 | my $raised = do { | ||
| 47 | local $@; | ||
| 48 | eval { | ||
| 49 | ($got, $stderr, @res) = Capture::Tiny::capture { | ||
| 50 | $self->{target_object}->$method(@args); | ||
| 51 | }; | ||
| 52 | }; | ||
| 53 | $@; | ||
| 54 | }; | ||
| 55 | if ($raised) { | ||
| 56 | fail join(", ", map(terse_dump($_), @savedArgs)); | ||
| 57 | diag "Error: $raised"; | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | if ($stderr) { | ||
| 61 | diag "STDERR: $stderr"; | ||
| 62 | } | ||
| 63 | is($got, $expect | ||
| 64 | , sprintf("call:%s(%s) expect:%s" | ||
| 65 | , $method | ||
| 66 | , join(", ", map(terse_dump($_), @savedArgs)) | ||
| 67 | , terse_dump($expect))); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub exits { | ||
| 71 | (my MY $self, my ($call, $expect)) = @_; | ||
| 72 | my ($method, @args) = @$call; | ||
| 73 | my @savedArgs = map {shallow_copy($_)} @args; | ||
| 74 | my ($ignore); | ||
| 75 | my $exit = Test::Exit::exit_code { | ||
| 76 | $ignore = Capture::Tiny::capture { | ||
| 77 | $self->{target_object}->$method(@args); | ||
| 78 | }; | ||
| 79 | }; | ||
| 80 | |||
| 81 | is($exit, $expect | ||
| 82 | , sprintf("call:%s(%s) expect:%s" | ||
| 83 | , $method | ||
| 84 | , join(", ", map(terse_dump($_), @savedArgs)) | ||
| 85 | , $expect)); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; |