Changeset 5595

Show
Ignore:
Timestamp:
01/27/08 07:34:35 (5 years ago)
Author:
cho45
Message:

lang/ruby/net-irc/trunk/test/net-irc_test.rb,
lang/ruby/net-irc/trunk/lib/net/irc.rb:

Append tests

Location:
lang/ruby/net-irc/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/net-irc/trunk/lib/net/irc.rb

    r5594 r5595  
    297297        class Prefix < String 
    298298                def nick 
     299                        extract[0] 
     300                end 
     301 
     302                def user 
    299303                        extract[1] 
    300304                end 
    301305 
    302                 def user 
     306                def host 
    303307                        extract[2] 
    304                 end 
    305  
    306                 def host 
    307                         extract[3] 
    308308                end 
    309309 
     
    402402        include Constants 
    403403 
     404        attr_reader :prefix 
     405 
    404406        def initialize(host, port, opts={}) 
    405407                @host = host 
     
    428430                                name = "on_#{(COMMANDS[m.command.upcase] || m.command).downcase}" 
    429431                                send(name, m) if respond_to?(name) 
     432                        rescue Exception => e 
     433                                warn e 
     434                                warn e.backtrace.join("\r\t") 
     435                                raise 
    430436                        rescue Message::InvalidMessage 
    431437                                @log.error "MessageParse: " + l.inspect 
  • lang/ruby/net-irc/trunk/test/net-irc_test.rb

    r5555 r5595  
    3333        end 
    3434 
    35         def test_server 
     35        def test_server_client 
    3636                port = rand(0xffff) + 1000 
    3737 
     
    4545 
    4646                Thread.start do 
    47                         client = Net::IRC::Client.new("localhost", port, { 
    48                                 :nick => "chokan", 
    49                                 :user => "chokan", 
    50                                 :real => "chokan", 
     47                        client = TestClient.new("localhost", port, { 
     48                                :nick => "foonick", 
     49                                :user => "foouser", 
     50                                :real => "foo real name", 
     51                                :pass => "foopass", 
    5152                                :out  => StringIO.new, 
    5253                        }) 
     
    5455                end 
    5556 
    56                 assert_equal "chokan!chokan@localhost", TestServerSession.testq.pop 
     57                assert_equal "PASS foopass\r\n", TestServerSession.testq.pop.to_s 
     58                assert_equal "NICK foonick\r\n", TestServerSession.testq.pop.to_s 
     59                assert_equal "USER foouser 0 * :foo real name\r\n", TestServerSession.testq.pop.to_s 
     60                assert_equal "WHOIS foonick\r\n", TestServerSession.testq.pop.to_s 
     61 
     62                assert_equal "001 foonick :Welcome to the Internet Relay Network foonick!foouser@localhost\r\n", TestClient.testq.pop.to_s 
     63                assert_equal "002 foonick :Your host is Net::IRC::Server::Session, running version 0.0.0\r\n", TestClient.testq.pop.to_s 
     64 
    5765                client.instance_eval do 
    5866                        post PRIVMSG, "#channel", "message a b c" 
     
    6270                assert_instance_of Net::IRC::Message, message 
    6371                assert_equal "PRIVMSG #channel :message a b c\r\n", message.to_s 
     72 
     73                client.instance_variable_set(:@prefix, Prefix.new("foonick!foouser@localhost")) 
     74 
     75                # test channel management 
     76                TestServerSession.instance.instance_eval do 
     77                        Thread.exclusive do 
     78                                post client.prefix, JOIN, "#test" 
     79                                post nil, NOTICE, "#test", "sep1" 
     80 
     81                                post "test1!test@localhost", JOIN, "#test" 
     82                                post "test2!test@localhost", JOIN, "#test" 
     83                                post nil, NOTICE, "#test", "sep2" 
     84 
     85                                post nil, RPL_NAMREPLY, client.prefix.nick, "@", "#test", "foo1 foo2 foo3 @foo4 +foo5" 
     86                                post nil, NOTICE, "#test", "sep3" 
     87                        end 
     88                end 
     89 
     90                while m = TestClient.testq.pop.to_s 
     91                        break if m == "NOTICE #test sep1\r\n" 
     92                end 
     93 
     94                c = client.instance_variable_get(:@channels) 
     95                assert_instance_of Hash,  c 
     96                assert_instance_of Hash,  c["#test"] 
     97                assert_instance_of Array, c["#test"][:modes] 
     98                assert_instance_of Array, c["#test"][:users] 
     99                assert_equal ["foonick"], c["#test"][:users] 
     100 
     101                while m = TestClient.testq.pop.to_s 
     102                        break if m == "NOTICE #test sep2\r\n" 
     103                end 
     104 
     105                assert_equal ["foonick", "test1", "test2"], c["#test"][:users] 
     106 
     107                while m = TestClient.testq.pop.to_s 
     108                        break if m == "NOTICE #test sep3\r\n" 
     109                end 
     110                assert_equal ["foonick", "test1", "test2", "foo1", "foo2", "foo3", "foo4", "foo5"], c["#test"][:users] 
     111                assert c["#test"][:modes].include?(["s", nil]) 
     112                assert c["#test"][:modes].include?(["o", "foo4"]) 
     113                assert c["#test"][:modes].include?(["v", "foo5"]) 
    64114        end 
    65115 
     
    81131                end 
    82132 
    83                 def on_user(m) 
    84                         super 
    85                         @@testq << @mask 
     133                def on_message(m) 
     134                        @@testq << m 
     135                end 
     136        end 
     137 
     138        class TestClient < Net::IRC::Client 
     139                @@testq = SizedQueue.new(1) 
     140                @@instance = nil 
     141 
     142                def self.testq 
     143                        @@testq 
    86144                end 
    87145 
    88                 def on_privmsg(m) 
     146                def self.instance 
     147                        @@instance 
     148                end 
     149 
     150                def initialize(*args) 
     151                        super 
     152                        @@instance = self 
     153                end 
     154 
     155                def on_message(m) 
    89156                        @@testq << m 
    90157                end