File: net-twitter-compatibility.t

package info (click to toggle)
libtwitter-api-perl 1.0006-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 424 kB
  • sloc: perl: 2,868; makefile: 7
file content (147 lines) | stat: -rw-r--r-- 4,690 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!perl
use 5.14.1;
use warnings;
use Ref::Util qw/is_hashref/;
use Test::Spec;
use HTTP::Response;
use Test::Fatal;

use Twitter::API;

BEGIN {
    eval { require Net::Twitter };
    plan skip_all => 'Net::Twitter >= 4.01041 not installed'
        if $@ || Net::Twitter->VERSION lt '4.01041';
}

my %skip = map +($_ => 1), (
    'contributees',           # deprecated
    'contributors',           # deprecated
    'create_media_metadata',  # described incorrectly in Net::Twitter
    'destroy_direct_message', # deprecated
    'direct_messages',        # deprecated
    'new_direct_message',     # deprecated
    'sent_direct_messages',   # deprecated
    'show_direct_message',    # deprecated
    'similar_places',         # no longer documented
    'update_delivery_device', # no longer documented
    'update_profile_colors',  # no longer documented
    'update_with_media',      # deprecated
    'upload_status',          # no longer documented
);

# These methods are either modified with an "around" or defined incorrectly in
# Net::Twitter, override what we expect for required parameters.
my %override_required = (
    show_user          => [ ':ID' ],
    create_friend      => [ ':ID' ],
    destroy_friend     => [ ':ID' ],
    friends_ids        => [ ':ID' ],
    followers_ids      => [ ':ID' ],
    create_block       => [ ':ID' ],
    destroy_block      => [ ':ID' ],
    report_spam        => [ ':ID' ],
    update_friendship  => [ ':ID' ],
    create_mute        => [ ':ID' ],
    destroy_mute       => [ ':ID' ],
);
# aliases
for ( \%override_required ) { # damned name is too long!
    $_->{follow} = $_->{follow_new} = $_->{create_friendship}
        = $_->{create_friend};
    $_->{destroy_friendship} = $_->{unfollow} = $_->{destroy_friend};
    $_->{following_ids} = $_->{friends_ids};
}

sub new_client {
    my $client = Twitter::API->new_with_traits(
        traits          => 'ApiMethods',
        consumer_key    => 'key',
        consumer_secret => 'secret',
    );
    $client->stubs(request => sub {
        my ( $self, $method, $path, $args ) = @_;
        die 'too many args' if @_ > 4;
        die 'too few args'  if @_ < 3;
        die 'final arg must be HASH' if @_ > 3 && !is_hashref($args);

        return ( uc $method, $args );
    });

    return $client;
}

sub http_response_ok {
    HTTP::Response->new(
        200, 'OK',
        [
            content_type   => 'application/json;charset=utf-8',
            contest_length => 4,
        ],
        '{}'
    );
}

my $nt = Net::Twitter->new(traits => [ qw/API::RESTv1_1/ ]);
my @nt_methods =
    # We'll test all methods through their aliases, too
    map {
        my $meta = $_;
        my @names = ($_->name, @{ $_->aliases });
        map [ $_, $meta ], @names;
    }
    sort { $a->name cmp $b->name }
    grep !$_->deprecated,
    grep $_->isa('Net::Twitter::Meta::Method'),
    map $_->original_method // $_, # may be wrapped
    $nt->meta->get_all_methods;

for my $pair ( @nt_methods ) {
    my ( $name, $nt_method ) = @$pair;
    next if $skip{$nt_method->name};

    describe $name => sub {
        my ( $client, @required );
        before each => sub {
            $client = new_client;
            @required = @{ $override_required{$name} // $nt_method->required };
        };

        it 'method exists' => sub {
            ok $client->can($name);
        };
        it 'has correct HTTP method' => sub {
            # path-part arguments must be passed
            my %must_have_args;
            @must_have_args{
                ( $nt_method->path =~ /:(\w+)/g ),
                map $_ eq ':ID' ? 'screen_name' : $_,
                @required
            } = 'a' .. 'z';
            my ( $http_method, undef ) = $client->$name(
                keys %must_have_args ? \%must_have_args : ()
            );
            is $http_method, $nt_method->method;
        };

        it "handles ${ \(0+@required) }  positional args" => sub {
            my @args; @args[0 .. $#required] = 'a' .. 'z';
            my %expected; @expected{
                map $_ eq ':ID' ? 'screen_name' : '$_', @required
            } = 'a' .. 'z';
            my ( undef, $args ) = $client->$name(@args);
            is_deeply $args, \%expected;
        } if @required > 0;

        it "handles mixed positional and named args" => sub {
            my %args; @args{@required[1..$#required]} = 'a' .. 'z';
            my %expected; @expected{
                map $_ eq ':ID' ? 'screen_name' : '$_', @required
            } = ( 'foo', 'a' .. 'z' );
            my ( undef, $args ) = $client->$name('foo', \%args);
            is_deeply $args, \%expected;
        } if @required > 1;
    };
}

runtests;