ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-IRC3/t/utils.t
Revision: 1.3
Committed: Sat Aug 18 12:39:02 2007 UTC (19 years, 1 month ago) by elmex
Content type: application/x-troff
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +13 -2 lines
Log Message:
implemented color filtering for irc

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 elmex 1.3 prefix_nick prefix_user prefix_host filter_colors/;
7 elmex 1.1
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 elmex 1.3 + scalar @ircmodes
49     + 3;
50 elmex 1.1
51     {
52     sub undef_or_eq {
53     my ($what, $it) = @_;
54     if (not defined $what) {
55     return not defined $it;
56     } else {
57     return 0 unless defined $it;
58     return $what eq $it;
59     }
60     }
61     sub cmp_msg {
62     my ($name, $msg, $cmp) = @_;
63     ok (undef_or_eq ($cmp->{prefix}, $msg->{prefix}), "$name: message prefix");
64     ok (undef_or_eq ($cmp->{command}, $msg->{command}), "$name: message command");
65    
66     my $params_ok = 1;
67    
68     if ($cmp->{params}) {
69     my @msgp = @{$msg->{params}};
70     for (@{$cmp->{params}}) {
71     my $p = shift @msgp;
72     unless (undef_or_eq ($_, $p)) {
73     $params_ok = 0;
74     last
75     }
76     }
77     }
78    
79     ok ($params_ok, "$name: message params");
80    
81     ok (undef_or_eq ($cmp->{trailing}, $msg->{trailing}), "$name: message trailing");
82     }
83    
84     for (@ircmsg_tests) {
85     my $msg = parse_irc_msg ($_->[1]);
86     cmp_msg ($_->[0], $msg, $_->[2]);
87     }
88     }
89    
90     {
91     for (@ircmsg_tests) {
92     my $name = $_->[0];
93     my $msg = $_->[1];
94     my $pmsg = parse_irc_msg ($msg);
95     my @params = @{$pmsg->{params}};
96     pop @params if $pmsg->{trailing};
97     my $omsg =
98     mk_msg ($pmsg->{prefix}, $pmsg->{command}, $pmsg->{trailing}, @params);
99    
100     is ($omsg, $msg, "$name: message parse and making succeed");
101     }
102     }
103    
104     {
105     for (@ircmsg_tests) {
106     my $name = $_->[0];
107     my $msg = $_->[1];
108     my $cmp = $_->[2];
109    
110     if ($cmp->{prefix}) {
111     $msg = parse_irc_msg ($msg);
112     my @prfx = split_prefix ($msg->{prefix});
113     for (0..2) {
114     is ($prfx[$_], $cmp->{prefix_ar}->[$_], "'$name': prefix ($_)")
115     }
116     is (prefix_nick ($msg), $cmp->{prefix_ar}->[0], "$name: nick prefix");
117     is (prefix_user ($msg), $cmp->{prefix_ar}->[1], "$name: user prefix");
118     is (prefix_host ($msg), $cmp->{prefix_ar}->[2], "$name: host prefix");
119     }
120     }
121     }
122    
123     for (@ircmodes) {
124     is (rfc_code_to_name ($_->[0]), $_->[1], "rfc_code_to_name: $_->[0]");
125     }
126 elmex 1.3
127     is (filter_colors ('2007-06-30 12:14:36 +0200 | IRC RECV{cmd: 332, params: elmex, #Jav-Fans, 8,1::7[ 0JAVFANS 7]8:: 8:: 7( 8Recruiting 7)0'),
128     '2007-06-30 12:14:36 +0200 | IRC RECV{cmd: 332, params: elmex, #Jav-Fans, ::[ JAVFANS ]:: :: ( Recruiting )',
129     'mirc color filter ok');
130    
131     is (filter_colors ('2007-08-04 22:01:04 +0200 | IRC RECV{cmd: PRIVMSG, params: #welcome, cocommlymeca: what is the biggest contemporan brake to the evolution of humankind towards Communism?, prefix: anonymous!anonymous@psyced.org}'),
132     '2007-08-04 22:01:04 +0200 | IRC RECV{cmd: PRIVMSG, params: #welcome, cocommlymeca: what is the biggest contemporan brake to the evolution of humankind towards Communism?, prefix: anonymous!anonymous@psyced.org}', 'filter ansi sequences');
133     is (filter_colors ('2007-08-07 19:15:27 +0200 | IRC RECV{cmd: PRIVMSG, params: #ccc, ~[5~[5~[5~[6~[6~[6~[5~[6~, prefix: schneider!~schneider@blinkenlichts.net}'),
134     '2007-08-07 19:15:27 +0200 | IRC RECV{cmd: PRIVMSG, params: #ccc, ~, prefix: schneider!~schneider@blinkenlichts.net}',
135     'filter ansi sequences 2');