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 INSTANCE: mb-writer output-stream
16 TUPLE: mb-reader lines disposed ;
17 INSTANCE: mb-reader input-stream
19 : <mb-writer> ( -- mb-writer ) V{ } clone V{ } clone f mb-writer boa ;
20 : <mb-reader> ( -- mb-reader ) <mailbox> f mb-reader boa ;
21 : push-line ( line test-reader-stream -- ) lines>> mailbox-put ;
22 : <test-stream> ( -- stream ) <mb-reader> <mb-writer> <duplex-stream> ;
23 M: mb-writer stream-write ( line mb-writer -- ) last-line>> push ;
24 M: mb-writer stream-flush ( mb-writer -- ) drop ;
25 M: mb-reader stream-readln ( mb-reader -- str/f ) lines>> mailbox-get ;
26 M: mb-writer stream-nl ( mb-writer -- )
27 [ [ last-line>> concat ] [ lines>> ] bi push ] keep
28 V{ } clone >>last-line drop ;
29 M: mb-reader dispose f swap push-line ;
30 M: mb-writer dispose drop ;
32 : spawn-client ( -- irc-client )
33 "someserver" irc-port "factorbot" f <irc-profile>
37 <test-stream> >>stream
38 dup [ spawn-irc yield ] with-irc ;
40 ! to be used inside with-irc quotations
41 : %add-named-chat ( chat -- ) (attach-chat) ;
42 : %push-line ( line -- ) irc> stream>> in>> push-line yield ;
43 : %push-lines ( lines -- ) [ %push-line ] each ;
44 : %join ( channel -- ) <irc-channel-chat> (attach-chat) ;
45 : %pop-output-line ( -- string ) irc> stream>> out>> lines>> pop ;
47 : read-matching-message ( chat quot: ( msg -- ? ) -- irc-message )
48 [ in-messages>> 0.1 seconds ] dip mailbox-get-timeout? ; inline
50 : spawning-irc ( quot: ( -- ) -- )
51 [ spawn-client ] dip [ (terminate-irc) ] compose with-irc ; inline
53 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
55 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
57 [ { t } [ irc> nick>> me? ] unit-test
59 { "factorbot" } [ irc> nick>> ] unit-test
61 { "#factortest" } [ ":someuser!n=user@some.where PRIVMSG #factortest :hi"
62 string>irc-message chat-name ] unit-test
64 { "someuser" } [ ":someuser!n=user@some.where PRIVMSG factorbot :hi"
65 string>irc-message chat-name ] unit-test
68 { privmsg "#channel" "hello" } [
69 "#channel" "hello" strings>privmsg
70 [ class-of ] [ target>> ] [ trailing>> ] tri
73 ! Test login and nickname set
75 ":some.where 001 factorbot2 :Welcome factorbot2" %push-line
81 { V{ "NICK factorbot" "USER factorbot hostname servername :irc.factor" } } [
82 "someserver" irc-port "factorbot" f <irc-profile> <irc-client>
83 [ 2drop <test-stream> ] >>connect
87 irc> stream>> out>> lines>>
92 ! Test connect with password
93 { V{ "PASS password" "NICK factorbot" "USER factorbot hostname servername :irc.factor" } } [
94 "someserver" irc-port "factorbot" "password" <irc-profile> <irc-client>
95 [ 2drop <test-stream> ] >>connect
99 irc> stream>> out>> lines>>
105 [ { "JOIN #factortest" } [
106 "#factortest" %join %pop-output-line
110 [ { "PART #factortest" } [
111 "#factortest" %join %pop-output-line drop
112 "#factortest" chat> remove-chat %pop-output-line
116 [ { join_ "#factortest" } [
117 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
118 { ":factorbot!n=factorbo@some.where JOIN :#factortest"
119 ":ircserver.net 353 factorbot @ #factortest :@factorbot "
120 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
121 ":ircserver.net 477 factorbot #factortest :[ircserver-info] blah blah"
123 [ join? ] read-matching-message
124 [ class-of ] [ channel>> ] bi
128 [ { privmsg "#factortest" "hello" } [
129 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
130 ":somebody!n=somebody@some.where PRIVMSG #factortest :hello" %push-line
131 [ privmsg? ] read-matching-message
132 [ class-of ] [ target>> ] [ trailing>> ] tri
136 [ { privmsg "factorbot" "hello" } [
137 "ircuser" <irc-nick-chat> [ %add-named-chat ] keep
138 ":ircuser!n=user@isp.net PRIVMSG factorbot :hello" %push-line
139 [ privmsg? ] read-matching-message
140 [ class-of ] [ target>> ] [ trailing>> ] tri
144 [ { mode "#factortest" "+ns" } [
145 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
146 ":ircserver.net MODE #factortest +ns" %push-line
147 [ mode? ] read-matching-message
148 [ class-of ] [ name>> ] [ mode>> ] tri
152 ! Participant lists tests
153 [ { { "ircuser" } } [
154 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
155 ":ircuser!n=user@isp.net JOIN :#factortest" %push-line
160 [ { { "ircuser2" } } [
161 "#factortest" <irc-channel-chat>
162 { "ircuser2" "ircuser" } [ over join-participant ] each
163 [ %add-named-chat ] keep
164 ":ircuser!n=user@isp.net PART #factortest" %push-line
169 [ { { "ircuser2" } } [
170 "#factortest" <irc-channel-chat>
171 { "ircuser2" "ircuser" } [ over join-participant ] each
172 [ %add-named-chat ] keep
173 ":ircuser!n=user@isp.net QUIT" %push-line
178 [ { { "ircuser2" } } [
179 "#factortest" <irc-channel-chat>
180 { "ircuser2" "ircuser" } [ over join-participant ] each
181 [ %add-named-chat ] keep
182 ":ircuser2!n=user2@isp.net KICK #factortest ircuser" %push-line
187 [ { H{ { "ircuser2" T{ participant { nick "ircuser2" } } } } } [
188 "#factortest" <irc-channel-chat>
189 "ircuser" over join-participant
190 [ %add-named-chat ] keep
191 ":ircuser!n=user2@isp.net NICK :ircuser2" %push-line
196 [ { H{ { "factorbot" T{ participant { nick "factorbot" } { operator t } } }
197 { "ircuser" T{ participant { nick "ircuser" } } }
198 { "voiced" T{ participant { nick "voiced" } { voice t } } } } } [
199 "#factortest" <irc-channel-chat>
200 "ircuser" over join-participant
201 [ %add-named-chat ] keep
202 { ":ircserver.net 353 factorbot @ #factortest :@factorbot "
203 ":ircserver.net 353 factorbot @ #factortest :ircuser2 "
204 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
205 ":ircserver.net 353 factorbot @ #factortest :@factorbot +voiced "
206 ":ircserver.net 353 factorbot @ #factortest :ircuser "
207 ":ircserver.net 366 factorbot #factortest :End of /NAMES list."
213 [ { mode "#factortest" "+o" "ircuser" } [
214 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
215 "ircuser" over join-participant
216 ":ircserver.net MODE #factortest +o ircuser" %push-line
217 [ mode? ] read-matching-message
218 { [ class-of ] [ name>> ] [ mode>> ] [ parameter>> ] } cleave
222 [ { T{ participant { nick "ircuser" } { operator t } } } [
223 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
224 "ircuser" over join-participant
225 ":ircserver.net MODE #factortest +o ircuser" %push-line
226 participants>> "ircuser" of
231 [ { "PRIVMSG #factortest :hello" } [
232 "#factortest" <irc-channel-chat> [ %add-named-chat ] keep
233 "hello" swap (speak) %pop-output-line