ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/t/utils.t
Revision: 1.1
Committed: Fri Feb 23 20:14:58 2007 UTC (19 years, 6 months ago) by elmex
Content type: application/x-troff
Branch: MAIN
Log Message:
added some tests and fixed quite some bugs and added missing
documentation.

File Contents

# User Rev Content
1 elmex 1.1 #!perl
2     use strict;
3     use Test::More;
4     use Net::IRC3::Util
5     qw/parse_irc_msg mk_msg split_prefix rfc_code_to_name
6     prefix_nick prefix_user prefix_host/;
7    
8     our @ircmsg_tests = (
9     ['full message' =>
10     ":nick!user\@host PRIVMSG #test :test message\015\012" => {
11     prefix => 'nick!user@host',
12     prefix_ar => ['nick', 'user', 'host'],
13     command => 'PRIVMSG',
14     params => ['#test', 'test message'],
15     trailing => 'test message'
16     }
17     ],
18     ['without prefix' =>
19     "PART #test :i'm gone\015\012" => {
20     prefix => undef,
21     command => 'PART',
22     params => ['#test', 'i\'m gone'],
23     trailing => 'i\'m gone'
24     }
25     ],
26     ['without params' =>
27     "QUIT\015\012" => {
28     prefix => undef,
29     command => 'QUIT',
30     params => [],
31     trailing => undef,
32     }
33     ],
34     );
35    
36     our @ircmodes = (
37     [qw/461 ERR_NEEDMOREPARAMS/],
38     [qw/491 ERR_NOOPERHOST/],
39     [qw/324 RPL_CHANNELMODEIS/],
40     [qw/209 RPL_TRACECLASS/],
41     [qw/001 RPL_WELCOME/],
42     [qw/502 ERR_USERSDONTMATCH/]
43     );
44    
45     plan tests =>
46     (5 * scalar @ircmsg_tests)
47     + (6 * scalar grep { $_->[2]->{prefix} } @ircmsg_tests)
48     + scalar @ircmodes;
49    
50     {
51     sub undef_or_eq {
52     my ($what, $it) = @_;
53     if (not defined $what) {
54     return not defined $it;
55     } else {
56     return 0 unless defined $it;
57     return $what eq $it;
58     }
59     }
60     sub cmp_msg {
61     my ($name, $msg, $cmp) = @_;
62     ok (undef_or_eq ($cmp->{prefix}, $msg->{prefix}), "$name: message prefix");
63     ok (undef_or_eq ($cmp->{command}, $msg->{command}), "$name: message command");
64    
65     my $params_ok = 1;
66    
67     if ($cmp->{params}) {
68     my @msgp = @{$msg->{params}};
69     for (@{$cmp->{params}}) {
70     my $p = shift @msgp;
71     unless (undef_or_eq ($_, $p)) {
72     $params_ok = 0;
73     last
74     }
75     }
76     }
77    
78     ok ($params_ok, "$name: message params");
79    
80     ok (undef_or_eq ($cmp->{trailing}, $msg->{trailing}), "$name: message trailing");
81     }
82    
83     for (@ircmsg_tests) {
84     my $msg = parse_irc_msg ($_->[1]);
85     cmp_msg ($_->[0], $msg, $_->[2]);
86     }
87     }
88    
89     {
90     for (@ircmsg_tests) {
91     my $name = $_->[0];
92     my $msg = $_->[1];
93     my $pmsg = parse_irc_msg ($msg);
94     my @params = @{$pmsg->{params}};
95     pop @params if $pmsg->{trailing};
96     my $omsg =
97     mk_msg ($pmsg->{prefix}, $pmsg->{command}, $pmsg->{trailing}, @params);
98    
99     is ($omsg, $msg, "$name: message parse and making succeed");
100     }
101     }
102    
103     {
104     for (@ircmsg_tests) {
105     my $name = $_->[0];
106     my $msg = $_->[1];
107     my $cmp = $_->[2];
108    
109     if ($cmp->{prefix}) {
110     $msg = parse_irc_msg ($msg);
111     my @prfx = split_prefix ($msg->{prefix});
112     for (0..2) {
113     is ($prfx[$_], $cmp->{prefix_ar}->[$_], "'$name': prefix ($_)")
114     }
115     is (prefix_nick ($msg), $cmp->{prefix_ar}->[0], "$name: nick prefix");
116     is (prefix_user ($msg), $cmp->{prefix_ar}->[1], "$name: user prefix");
117     is (prefix_host ($msg), $cmp->{prefix_ar}->[2], "$name: host prefix");
118     }
119     }
120     }
121    
122     for (@ircmodes) {
123     is (rfc_code_to_name ($_->[0]), $_->[1], "rfc_code_to_name: $_->[0]");
124     }