File: 14protocol-encoding.t

package info (click to toggle)
libprotocol-irc-perl 0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 2,122; makefile: 2
file content (95 lines) | stat: -rw-r--r-- 2,893 bytes parent folder | download
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
#!/usr/bin/perl

use v5.14;
use warnings;
use utf8;

use Test::More;

use Encode qw( encode_utf8 );

my $CRLF = "\x0d\x0a"; # because \r\n isn't portable

my @textmessages;
my @quitmessages;
my $serverstream;

package TestIRC {
   use base qw( Protocol::IRC );

   sub new { return bless [], shift }

   sub write { $serverstream .= $_[1] }

   use constant encoder => Encode::find_encoding("UTF-8");

   my %isupport = (
      CHANTYPES   => "#&",
         channame_re => qr/^[#&]/,
      PREFIX      => "(ohv)@%+",
         prefix_modes => 'ohv',
         prefix_flags => '@%+',
         prefixflag_re => qr/^[@%+]/,
         prefix_map_m2f => { 'o' => '@', 'h' => '%', 'v' => '+' },
         prefix_map_f2m => { '@' => 'o', '%' => 'h', '+' => 'v' },
   );

   sub isupport { return $isupport{$_[1]} }

   sub nick { return "MyNick" }

   sub on_message_text { push @textmessages, [ $_[1], $_[2] ] }
   sub on_message_QUIT { push @quitmessages, [ $_[1], $_[2] ] }
}

my $irc = TestIRC->new;
sub write_irc
{
   my $line = $_[0];
   $irc->on_read( $line );
   length $line == 0 or die '$irc failed to read all of the line';
}

my $helloworld = "مرحبا العالم"; # Hello World in Arabic, according to Google translate
my $octets = encode_utf8( $helloworld );

write_irc( ':Someone!theiruser@their.host PRIVMSG #arabic :' . $octets . $CRLF );

my ( $msg, $hints ) = @{ shift @textmessages };

is( $msg->command, "PRIVMSG",                      '$msg->command for PRIVMSG with encoding' );
is( $msg->prefix,  'Someone!theiruser@their.host', '$msg->prefix for PRIVMSG with encoding' );

is_deeply( $hints,
           { synthesized        => 1,
             prefix_nick        => "Someone",
             prefix_nick_folded => "someone",
             prefix_user        => "theiruser",
             prefix_host        => "their.host",
             prefix_name        => "Someone",
             prefix_name_folded => "someone",
             prefix_is_me       => '',
             target_name        => "#arabic",
             target_name_folded => "#arabic",
             target_is_me       => '',
             target_type        => "channel",
             is_notice          => 0,
             restriction        => '',
             text               => "مرحبا العالم",
             handled            => 1 },
           '$hints for PRIVMSG with encoding' );

$serverstream = "";
$irc->send_message( "PRIVMSG", undef, "#arabic", "مرحبا العالم" );

is( $serverstream, "PRIVMSG #arabic :$octets$CRLF",
                   "Server stream after sending PRIVMSG with encoding" );

write_irc( ':Someone!theiruser@their.host QUIT :' . $octets . $CRLF );

( $msg, $hints ) = @{ shift @quitmessages };

is( $msg->command, "QUIT", '$msg->command for QUIT with encoding' );
is( $hints->{text}, "مرحبا العالم", '$hints->{text} for QUIT with encoding' );

done_testing;