1 ! Copyright (C) 2009 Bruno Deferrari
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel tools.test accessors arrays sequences
4 io io.streams.duplex namespaces threads destructors
5 calendar concurrency.mailboxes classes assocs combinators
6 irc.messages.parser irc.client.base irc.client.chats
7 irc.client.participants irc.client.internals ;
8 EXCLUDE: irc.messages => join ;
9 RENAME: join irc.messages => join_
10 IN: irc.client.internals.tests
13 TUPLE: mb-writer lines last-line disposed ;
14 TUPLE: mb-reader lines disposed ;
15 : <mb-writer> ( -- mb-writer ) V{ } clone V{ } clone f mb-writer boa ;
16 : <mb-reader> ( -- mb-reader ) <mailbox> f mb-reader boa ;
17 : push-line ( line test-reader-stream -- ) lines>> mailbox-put ;
18 : <test-stream> ( -- stream ) <mb-reader> <mb-writer> <duplex-stream> ;
19 M: mb-writer stream-write ( line mb-writer -- ) last-line>> push ;
20 M: mb-writer stream-flush ( mb-writer -- ) drop ;
21 M: mb-reader stream-readln ( mb-reader -- str/f ) lines>> mailbox-get ;
22 M: mb-writer stream-nl ( mb-writer -- )
23 [ [ last-line>> concat ] [ lines>> ] bi push ] keep
24 V{ } clone >>last-line drop ;
25 M: mb-reader dispose f swap push-line ;
26 M: mb-writer dispose drop ;
28 : spawn-client ( -- irc-client )
29 "someserver" irc-port "factorbot" f <irc-profile>
33 <test-stream> >>stream
34 dup [ spawn-irc yield ] with-irc ;
36 ! to be used inside with-irc quotations
37 : %add-named-chat ( chat -- ) (attach-chat) ;
38 : %push-line ( line -- ) irc> stream>> in>> push-line yield ;
39 : %push-lines ( lines -- ) [ %push-line ] each ;
40 : %join ( channel -- ) <irc-channel-chat> (attach-chat) ;
41 : %pop-output-line ( -- string ) irc> stream>> out>> lines>> pop ;
43 : read-matching-message ( chat quot: ( msg -- ? ) -- irc-message )
44 [ in-messages>> 0.1 seconds ] dip mailbox-get-timeout? ; inline
46 : spawning-irc ( quot: ( -- ) -- )
47 [ spawn-client ] dip [ (terminate-irc) ] compose with-irc ; inline
49 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
51 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
53 [ { t } [ irc> nick>> me? ] unit-test
55 { "factorbot" } [ irc> nick>> ] unit-test
57 { "#factortest" } [ ":someuser!n=user@some.where PRIVMSG #factortest :hi"
58 string>irc-message chat-name ] unit-test
60 { "someuser" } [ ":someuser!n=user@some.where PRIVMSG factorbot :hi"
61 string>irc-message chat-name ] unit-test
64 { privmsg "#channel" "hello" } [
65 "#channel" "hello" strings>privmsg
66 [ class ] [ target>> ] [ trailing>> ] tri
69 ! Test login and nickname set
71 ":some.where 001 factorbot2 :Welcome factorbot2" %push-line
77 { V{ "NICK factorbot" "USER factorbot hostname servername :irc.factor" } } [
78 "someserver" irc-port "factorbot" f <irc-profile> <irc-client>
79 [ 2drop <test-stream> ] >>connect
83 irc> stream>> out>> lines>>
88 ! Test connect with password
89 { V{ "PASS password" "NICK factorbot" "USER factorbot hostname servername :irc.factor" } } [
90 "someserver" irc-port "factorbot" "password" <irc-profile> <irc-client>
91 [ 2drop <test-stream> ] >>connect
95 irc> stream>> out>> lines>>
101 [ { "JOIN #factortest" } [
102 "#factortest" %join %pop-output-line
106 [ { join_ "#factortest"} [
107 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
108 { ":factorbot!n=factorbo@some.where JOIN :#factortest"
109 ":ircserver.net 353 factorbot @ #factortest :@factorbot "
110 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
111 ":ircserver.net 477 factorbot #factortest :[ircserver-info] blah blah"
113 [ join? ] read-matching-message
114 [ class ] [ channel>> ] bi
118 [ { privmsg "#factortest" "hello" } [
119 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
120 ":somebody!n=somebody@some.where PRIVMSG #factortest :hello" %push-line
121 [ privmsg? ] read-matching-message
122 [ class ] [ target>> ] [ trailing>> ] tri
126 [ { privmsg "factorbot" "hello" } [
127 "ircuser" <irc-nick-chat> [ %add-named-chat ] keep
128 ":ircuser!n=user@isp.net PRIVMSG factorbot :hello" %push-line
129 [ privmsg? ] read-matching-message
130 [ class ] [ target>> ] [ trailing>> ] tri
134 [ { mode "#factortest" "+ns" } [
135 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
136 ":ircserver.net MODE #factortest +ns" %push-line
137 [ mode? ] read-matching-message
138 [ class ] [ name>> ] [ mode>> ] tri
142 ! Participant lists tests
143 [ { { "ircuser" } } [
144 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
145 ":ircuser!n=user@isp.net JOIN :#factortest" %push-line
150 [ { { "ircuser2" } } [
151 "#factortest" <irc-channel-chat>
152 { "ircuser2" "ircuser" } [ over join-participant ] each
153 [ %add-named-chat ] keep
154 ":ircuser!n=user@isp.net PART #factortest" %push-line
159 [ { { "ircuser2" } } [
160 "#factortest" <irc-channel-chat>
161 { "ircuser2" "ircuser" } [ over join-participant ] each
162 [ %add-named-chat ] keep
163 ":ircuser!n=user@isp.net QUIT" %push-line
168 [ { { "ircuser2" } } [
169 "#factortest" <irc-channel-chat>
170 { "ircuser2" "ircuser" } [ over join-participant ] each
171 [ %add-named-chat ] keep
172 ":ircuser2!n=user2@isp.net KICK #factortest ircuser" %push-line
177 [ { H{ { "ircuser2" T{ participant { nick "ircuser2" } } } } } [
178 "#factortest" <irc-channel-chat>
179 "ircuser" over join-participant
180 [ %add-named-chat ] keep
181 ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
186 [ { H{ { "factorbot" T{ participant { nick "factorbot" } { operator t } } }
187 { "ircuser" T{ participant { nick "ircuser" } } }
188 { "voiced" T{ participant { nick "voiced" } { voice t } } } } } [
189 "#factortest" <irc-channel-chat>
190 "ircuser" over join-participant
191 [ %add-named-chat ] keep
192 { ":ircserver.net 353 factorbot @ #factortest :@factorbot "
193 ":ircserver.net 353 factorbot @ #factortest :ircuser2 "
194 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
195 ":ircserver.net 353 factorbot @ #factortest :@factorbot +voiced "
196 ":ircserver.net 353 factorbot @ #factortest :ircuser "
197 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
203 [ { mode "#factortest" "+o" "ircuser" } [
204 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
205 "ircuser" over join-participant
206 ":ircserver.net MODE #factortest +o ircuser" %push-line
207 [ mode? ] read-matching-message
208 { [ class ] [ name>> ] [ mode>> ] [ parameter>> ] } cleave
212 [ { T{ participant { nick "ircuser" } { operator t } } } [
213 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
214 "ircuser" over join-participant
215 ":ircserver.net MODE #factortest +o ircuser" %push-line
216 participants>> "ircuser" swap at
221 [ { "PRIVMSG #factortest :hello" } [
222 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
223 "hello" swap (speak) %pop-output-line