OSDN Git Service

- shogi_server/command.rb:
[shogi-server/shogi-server.git] / test / TC_command.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 $topdir = File.expand_path File.dirname(__FILE__)
3 require 'test/unit'
4 require 'tempfile'
5 require 'mock_game'
6 require 'mock_log_message'
7 require 'test/mock_player'
8 require 'shogi_server/login'
9 require 'shogi_server/player'
10 require 'shogi_server/command'
11
12
13 class MockLeague
14   def initialize
15     @games = {}
16     @games["dummy_game_id"] = MockGame.new
17   end
18
19   def games
20     return @games
21   end
22
23   def rated_players
24     return []
25   end
26
27   def players
28     return [MockPlayer.new]
29   end
30
31   def event
32     return "test"
33   end
34
35   def dir
36     return $topdir
37   end
38
39   def get_player(status, game_id, sente, searcher)
40     if sente == true
41       $p1 = MockPlayer.new
42       $p1.name = "p1"
43       return $p1
44     elsif sente == false
45       $p2 = MockPlayer.new
46       $p2.name = "p2"
47       return $p2
48     elsif sente == nil
49       return nil
50     else
51       return nil
52     end
53   end
54 end
55
56
57 class TestFactoryMethod < Test::Unit::TestCase 
58
59   def setup
60     @p = MockPlayer.new
61     @p.name = "test_factory_method_player"
62     $league = MockLeague.new
63   end
64
65   def test_keep_alive_command
66     cmd = ShogiServer::Command.factory("", @p)
67     assert_instance_of(ShogiServer::KeepAliveCommand, cmd)
68   end
69
70   def test_move_command
71     cmd = ShogiServer::Command.factory("+7776FU", @p)
72     assert_instance_of(ShogiServer::MoveCommand, cmd)
73   end
74
75   def test_special_command
76     cmd = ShogiServer::Command.factory("%TORYO", @p)
77     assert_instance_of(ShogiServer::SpecialCommand, cmd)
78   end
79
80   def test_special_command_timeout
81     cmd = ShogiServer::Command.factory(:timeout, @p)
82     assert_instance_of(ShogiServer::SpecialCommand, cmd)
83   end
84
85   def test_execption_command
86     cmd = ShogiServer::Command.factory(:exception, @p)
87     assert_instance_of(ShogiServer::ExceptionCommand, cmd)
88   end
89
90   def test_reject_command
91     cmd = ShogiServer::Command.factory("REJECT", @p)
92     assert_instance_of(ShogiServer::RejectCommand, cmd)
93   end
94
95   def test_agree_command
96     cmd = ShogiServer::Command.factory("AGREE", @p)
97     assert_instance_of(ShogiServer::AgreeCommand, cmd)
98   end
99
100   def test_show_command
101     cmd = ShogiServer::Command.factory("%%SHOW game_id", @p)
102     assert_instance_of(ShogiServer::ShowCommand, cmd)
103   end
104
105   def test_monitoron_command
106     cmd = ShogiServer::Command.factory("%%MONITORON game_id", @p)
107     assert_instance_of(ShogiServer::MonitorOnCommand, cmd)
108   end
109
110   def test_monitor2on_command
111     cmd = ShogiServer::Command.factory("%%MONITOR2ON game_id", @p)
112     assert_instance_of(ShogiServer::Monitor2OnCommand, cmd)
113   end
114
115   def test_monitoroff_command
116     cmd = ShogiServer::Command.factory("%%MONITOROFF game_id", @p)
117     assert_instance_of(ShogiServer::MonitorOffCommand, cmd)
118   end
119
120   def test_help_command
121     cmd = ShogiServer::Command.factory("%%HELP", @p)
122     assert_instance_of(ShogiServer::HelpCommand, cmd)
123   end
124
125   def test_rating_command
126     cmd = ShogiServer::Command.factory("%%RATING", @p)
127     assert_instance_of(ShogiServer::RatingCommand, cmd)
128   end
129
130   def test_version_command
131     cmd = ShogiServer::Command.factory("%%VERSION", @p)
132     assert_instance_of(ShogiServer::VersionCommand, cmd)
133   end
134
135   def test_game_command
136     cmd = ShogiServer::Command.factory("%%GAME", @p)
137     assert_instance_of(ShogiServer::GameCommand, cmd)
138   end
139
140   def test_game_challenge_command_game
141     cmd = ShogiServer::Command.factory("%%GAME default-1500-0 +", @p)
142     assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
143   end
144
145   def test_game_challenge_command_challenge
146     cmd = ShogiServer::Command.factory("%%CHALLENGE default-1500-0 -", @p)
147     assert_instance_of(ShogiServer::GameChallengeCommand, cmd)
148   end
149
150   def test_chat_command
151     cmd = ShogiServer::Command.factory("%%CHAT hello", @p)
152     assert_instance_of(ShogiServer::ChatCommand, cmd)
153   end
154
155   def test_list_command
156     cmd = ShogiServer::Command.factory("%%LIST", @p)
157     assert_instance_of(ShogiServer::ListCommand, cmd)
158   end
159
160   def test_who_command
161     cmd = ShogiServer::Command.factory("%%WHO", @p)
162     assert_instance_of(ShogiServer::WhoCommand, cmd)
163   end
164
165   def test_logout_command
166     cmd = ShogiServer::Command.factory("LOGOUT", @p)
167     assert_instance_of(ShogiServer::LogoutCommand, cmd)
168   end
169
170   def test_challenge_command
171     cmd = ShogiServer::Command.factory("CHALLENGE", @p)
172     assert_instance_of(ShogiServer::ChallengeCommand, cmd)
173   end
174
175   def test_space_command
176     cmd = ShogiServer::Command.factory(" ", @p)
177     assert_instance_of(ShogiServer::SpaceCommand, cmd)
178   end
179
180   def test_setbuoy_command
181     cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU", @p)
182     assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
183   end
184
185   def test_setbuoy_command_with_counter
186     cmd = ShogiServer::Command.factory("%%SETBUOY buoy_test-1500-0 +7776FU 3", @p)
187     assert_instance_of(ShogiServer::SetBuoyCommand, cmd)
188   end
189
190   def test_deletebuoy_command
191     cmd = ShogiServer::Command.factory("%%DELETEBUOY buoy_test-1500-0", @p)
192     assert_instance_of(ShogiServer::DeleteBuoyCommand, cmd)
193   end
194
195   def test_getbuoycount_command
196     cmd = ShogiServer::Command.factory("%%GETBUOYCOUNT buoy_test-1500-0", @p)
197     assert_instance_of(ShogiServer::GetBuoyCountCommand, cmd)
198   end
199
200   def test_error
201     cmd = ShogiServer::Command.factory("should_be_error", @p)
202     assert_instance_of(ShogiServer::ErrorCommand, cmd)
203   end
204 end
205
206 #
207 #
208 class TestKeepAliveCommand < Test::Unit::TestCase 
209   def setup
210     @p = MockPlayer.new
211   end
212
213   def test_call
214     cmd = ShogiServer::KeepAliveCommand.new("", @p)
215     rc = cmd.call
216     assert_equal(:continue, rc)
217   end
218 end
219
220 #
221 #
222 class TestMoveCommand < Test::Unit::TestCase
223   def setup
224     @p = MockPlayer.new
225     @game = MockGame.new
226     @p.game = @game
227     @p.status = "game"
228   end
229
230   def test_call
231     cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
232     rc = cmd.call
233     assert_equal(:continue, rc)
234   end
235
236   def test_comment
237     cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
238     rc = cmd.call
239     assert_equal(:continue, rc)
240     assert_equal("'*comment", @game.log.first)
241   end
242
243   def test_x1_return
244     @game.finish_flag = true
245     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
246     cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
247     rc = cmd.call
248     assert_equal(:return, rc)
249   end
250 end
251
252 #
253 #
254 class TestSpecialComand < Test::Unit::TestCase
255   def setup
256     @p = MockPlayer.new
257     @game = MockGame.new
258     @p.game = @game
259     @p.status = "game"
260   end
261
262   def test_toryo
263     @game.finish_flag = true
264     cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
265     rc = cmd.call
266     assert_equal(:continue, rc)
267   end
268
269   def test_toryo_csa_protocol
270     @game.finish_flag = true
271     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
272     cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
273     rc = cmd.call
274     assert_equal(:return, rc)
275   end
276
277   def test_timeout
278     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
279     rc = cmd.call
280     assert_equal(:continue, rc)
281   end
282
283   def test_expired_game
284     @p.status = "agree_waiting"
285     @game.prepared_expire = true
286     assert(!@game.rejected)
287     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
288     rc = cmd.call
289     assert_equal(:continue, rc)
290     assert(@game.rejected)
291   end
292
293   def test_expired_game_csa_protocol
294     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
295     @p.status = "agree_waiting"
296     @game.prepared_expire = true
297     assert(!@game.rejected)
298     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
299     rc = cmd.call
300     assert_equal(:return, rc)
301     assert(@game.rejected)
302   end
303
304   def test_error
305     @p.status = "should_be_ignored"
306     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
307     rc = cmd.call
308     assert_equal(:continue, rc)
309   end
310 end
311
312 #
313 #
314 class TestExceptionCommand < Test::Unit::TestCase 
315   def setup
316     @p = MockPlayer.new
317   end
318
319   def test_call
320     cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
321     rc = cmd.call
322     assert_equal(:return, rc)
323   end
324 end
325
326 #
327 #
328 class TestRejectCommand < Test::Unit::TestCase 
329   def setup
330     @p = MockPlayer.new
331     @game = MockGame.new
332     @p.game = @game
333     @p.status = "game"
334   end
335
336   def test_call
337     @p.status = "agree_waiting"
338     assert(!@game.rejected)
339     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
340     rc = cmd.call
341
342     assert_equal(:continue, rc)
343     assert(@game.rejected)
344   end
345
346   def test_call_csa_protocol
347     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
348     @p.status = "agree_waiting"
349     assert(!@game.rejected)
350     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
351     rc = cmd.call
352
353     assert_equal(:return, rc)
354     assert(@game.rejected)
355   end
356
357   def test_error
358     @p.status = "should_be_ignored"
359     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
360     rc = cmd.call
361
362     assert_equal(:continue, rc)
363     assert(!@game.rejected)
364   end
365 end
366
367 #
368 #
369 class TestAgreeCommand < Test::Unit::TestCase 
370   def setup
371     @p = MockPlayer.new
372     @game = MockGame.new
373     @p.game = @game
374     @p.status = "agree_waiting"
375   end
376
377   def test_not_start_yet
378     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
379     rc = cmd.call
380     assert_equal(:continue, rc)
381     assert(!@game.started)
382   end
383
384   def test_start
385     @game.is_startable_status = true
386     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
387     rc = cmd.call
388     assert_equal(:continue, rc)
389     assert(@game.started)
390   end
391
392   def test_error
393     @p.status = "should_be_ignored"
394     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
395     rc = cmd.call
396     assert_equal(:continue, rc)
397     assert(!@game.started)
398   end
399 end
400
401 #
402 #
403 class TestShowCommand < Test::Unit::TestCase 
404   def setup
405     @p = MockPlayer.new
406     @game = MockGame.new
407     @p.game = @game
408   end
409
410   def test_call
411     cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
412     rc = cmd.call
413
414     assert_equal(:continue, rc)
415   end
416
417   def test_call_nil_game
418     cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
419     rc = cmd.call
420
421     assert_equal(:continue, rc)
422   end
423 end
424
425 #
426 #
427 class TestMonitorOnCommand < Test::Unit::TestCase 
428   def setup
429     @p = MockPlayer.new
430     @game = MockGame.new
431     @p.game = @game
432   end
433
434   def test_call
435     cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
436     rc = cmd.call
437
438     assert_equal(:continue, rc)
439   end
440
441   def test_call_read_logfile
442     game = MockGame.new
443     cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
444     rc = cmd.call
445     assert_equal("##[MONITOR][dummy_game_id] dummy_game_show\n##[MONITOR][dummy_game_id] line1\n##[MONITOR][dummy_game_id] line2\n##[MONITOR][dummy_game_id] +OK\n", @p.out.join)
446     assert_equal(:continue, rc)
447   end
448 end
449
450 #
451 #
452 class TestMonitor2OnCommand < Test::Unit::TestCase 
453   def setup
454     @p = MockPlayer.new
455     @game = MockGame.new
456     @p.game = @game
457   end
458
459   def test_call
460     cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
461     rc = cmd.call
462
463     assert_equal(:continue, rc)
464   end
465
466   def test_call_read_logfile
467     $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
468     $tempfile.write "hoge\nfoo\n"
469     $tempfile.close
470     game = MockGame.new
471     def game.logfile
472       $tempfile.path
473     end
474     cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
475     rc = cmd.call
476     assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
477     assert_equal(:continue, rc)
478     $tempfile = nil
479   end
480 end
481
482 #
483 #
484 class TestMonitorOffCommand < Test::Unit::TestCase 
485   def setup
486     @p = MockPlayer.new
487     @game = MockGame.new
488     @p.game = @game
489   end
490
491   def test_call
492     cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
493     rc = cmd.call
494
495     assert_equal(:continue, rc)
496   end
497 end
498
499 #
500 #
501 class TestHelpCommand < Test::Unit::TestCase 
502   def setup
503     @p = MockPlayer.new
504     @game = MockGame.new
505     @p.game = @game
506   end
507
508   def test_call
509     cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
510     rc = cmd.call
511
512     assert_equal(:continue, rc)
513   end
514 end
515
516 #
517 #
518 class TestRatingCommand < Test::Unit::TestCase 
519   def setup
520     @p = MockPlayer.new
521     @game = MockGame.new
522     @p.game = @game
523   end
524
525   def test_call
526     players = [MockPlayer.new]
527     cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
528     rc = cmd.call
529
530     assert_equal(:continue, rc)
531   end
532 end
533
534 #
535 #
536 class TestVersionCommand < Test::Unit::TestCase 
537   def setup
538     @p = MockPlayer.new
539     @game = MockGame.new
540     @p.game = @game
541   end
542
543   def test_call
544     cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
545     rc = cmd.call
546
547     assert_equal(:continue, rc)
548   end
549 end
550
551 #
552 #
553 class TestGameCommand < Test::Unit::TestCase 
554   def setup
555     @p = MockPlayer.new
556     @game = MockGame.new
557     @p.game = @game
558   end
559
560   def test_call_connected
561     @p.status = "connected"
562     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
563     rc = cmd.call
564
565     assert_equal(:continue, rc)
566     assert_equal("connected", @p.status)
567   end
568
569   def test_call_game_waiting
570     @p.status = "game_waiting"
571     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
572     rc = cmd.call
573
574     assert_equal(:continue, rc)
575     assert_equal("connected", @p.status)
576   end
577
578   def test_call_agree_waiting
579     @p.status = "agree_waiting"
580     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
581     rc = cmd.call
582
583     assert_equal(:continue, rc)
584     assert_equal("agree_waiting", @p.status)
585   end
586 end
587
588 #
589 #
590 class TestChatCommand < Test::Unit::TestCase 
591   def setup
592     @p = MockPlayer.new
593     @game = MockGame.new
594     @p.game = @game
595   end
596
597   def test_call
598     players = [["dummy_name", MockPlayer.new]]
599     cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
600     rc = cmd.call
601
602     assert_equal(:continue, rc)
603   end
604
605   def test_call_csa_protocol
606     players = [["dummy_name", MockPlayer.new]]
607     players.each do |name, p|
608       p.protocol = ShogiServer::LoginCSA::PROTOCOL
609     end
610     cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
611     rc = cmd.call
612
613     assert_equal(:continue, rc)
614   end
615 end
616
617 #
618 #
619 class TestListCommand < Test::Unit::TestCase 
620   def setup
621     @p = MockPlayer.new
622     @game = MockGame.new
623     @p.game = @game
624   end
625
626   def test_call
627     games = [["dummy_game_id", MockGame.new]]
628     cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
629     rc = cmd.call
630
631     assert_equal(:continue, rc)
632   end
633
634 end
635
636 #
637 #
638 class TestWhoCommand < Test::Unit::TestCase 
639   def setup
640     @p = MockPlayer.new
641     @game = MockGame.new
642     @p.game = @game
643   end
644
645   def test_call
646     players = [["dummy_name", MockPlayer.new]]
647     cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
648     rc = cmd.call
649
650     assert_equal(:continue, rc)
651   end
652
653 end
654
655 #
656 #
657 class TestLogoutCommand < Test::Unit::TestCase 
658   def setup
659     @p = MockPlayer.new
660     @game = MockGame.new
661     @p.game = @game
662   end
663
664   def test_call
665     cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
666     rc = cmd.call
667
668     assert_equal(:return, rc)
669   end
670
671 end
672
673 #
674 #
675 class TestChallengeCommand < Test::Unit::TestCase 
676   def setup
677     @p = MockPlayer.new
678     @game = MockGame.new
679   end
680
681   def test_call
682     cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
683     rc = cmd.call
684
685     assert_equal(:continue, rc)
686   end
687 end
688
689 #
690 #
691 class TestSpaceCommand < Test::Unit::TestCase 
692   def setup
693     @p = MockPlayer.new
694     @game = MockGame.new
695   end
696
697   def test_call
698     cmd = ShogiServer::SpaceCommand.new("", @p)
699     rc = cmd.call
700
701     assert_equal(:continue, rc)
702   end
703 end
704
705 #
706 #
707 class TestErrorCommand < Test::Unit::TestCase 
708   def setup
709     @p = MockPlayer.new
710     @game = MockGame.new
711   end
712
713   def test_call
714     cmd = ShogiServer::ErrorCommand.new("", @p)
715     rc = cmd.call
716
717     assert_equal(:continue, rc)
718   end
719 end
720
721 class BaseTestBuoyCommand < Test::Unit::TestCase
722   def setup
723     @p = MockPlayer.new
724     $p1 = nil
725     $p2 = nil
726
727     delete_buoy_yaml
728     @buoy = ShogiServer::Buoy.new
729   end
730
731   def teadown
732     delete_buoy_yaml
733   end
734
735   def delete_buoy_yaml
736     file = File.join($topdir, "buoy.yaml")
737     File.delete file if File.exist? file
738   end
739
740   def test_dummy
741     assert true
742   end
743 end
744
745
746 #
747 #
748 class TestSetBuoyCommand < BaseTestBuoyCommand
749   
750   def setup
751     super
752     @p.name = "set_buoy_player"
753   end
754
755   def test_call_2
756     assert @buoy.is_new_game?("buoy_hoge-1500-0")
757     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
758     rt = cmd.call
759     assert :continue, rt
760     assert !@buoy.is_new_game?("buoy_hoge-1500-0")
761     assert !$p1.out.empty?
762     assert !$p2.out.empty?
763     buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
764     assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1), buoy_game2
765   end
766
767   def test_call_1
768     assert @buoy.is_new_game?("buoy_hoge-1500-0")
769     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
770     rt = cmd.call
771     assert :continue, rt
772     assert @buoy.is_new_game?("buoy_hoge-1500-0")
773     assert !$p1.out.empty?
774     assert !$p2.out.empty?
775   end
776
777   def test_call_error_not_buoy_game_name
778     assert @buoy.is_new_game?("buoy_hoge-1500-0")
779     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
780     rt = cmd.call
781     assert :continue, rt
782     assert !$p1
783     assert !$p2
784     assert @buoy.is_new_game?("buoy_hoge-1500-0")
785   end
786
787   def test_call_error_duplicated_game_name
788     assert @buoy.is_new_game?("buoy_duplicated-1500-0")
789     bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1)
790     @buoy.add_game bg
791     assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
792     
793     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
794     rt = cmd.call
795     assert :continue, rt
796     assert !$p1
797     assert !$p2
798     assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
799   end
800
801   def test_call_error_bad_moves
802     assert @buoy.is_new_game?("buoy_badmoves-1500-0")
803     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
804     rt = cmd.call
805     assert :continue, rt
806     assert !$p1
807     assert !$p2
808     assert @buoy.is_new_game?("buoy_badmoves-1500-0")
809   end
810
811   def test_call_error_bad_counter
812     assert @buoy.is_new_game?("buoy_badcounter-1500-0")
813     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
814     rt = cmd.call
815     assert :continue, rt
816     assert !$p1
817     assert !$p2
818     assert @buoy.is_new_game?("buoy_badcounter-1500-0")
819   end
820 end
821
822
823 #
824 #
825 class TestDeleteBuoyCommand < BaseTestBuoyCommand
826   def test_call
827     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
828     assert @buoy.is_new_game?(buoy_game.game_name)
829     @buoy.add_game buoy_game
830     assert !@buoy.is_new_game?(buoy_game.game_name)
831     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
832     rt = cmd.call
833     assert :continue, rt
834     assert !$p1
835     assert !$p2
836     assert @buoy.is_new_game?(buoy_game.game_name)
837   end
838
839   def test_call_not_exist
840     buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
841     assert @buoy.is_new_game?(buoy_game.game_name)
842     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
843     rt = cmd.call
844     assert :continue, rt
845     assert !$p1
846     assert !$p2
847     assert @buoy.is_new_game?(buoy_game.game_name)
848   end
849
850   def test_call_another_player
851     buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1)
852     assert @buoy.is_new_game?(buoy_game.game_name)
853     @buoy.add_game(buoy_game)
854     assert !@buoy.is_new_game?(buoy_game.game_name)
855
856     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
857     rt = cmd.call
858     assert :continue, rt
859     assert_equal "##[ERROR] you are not allowed to delete a buoy game that you did not set: buoy_anotherplayer-1500-0\n", @p.out.first
860     assert !@buoy.is_new_game?(buoy_game.game_name)
861   end
862 end
863
864 #
865 #
866 class TestGetBuoyCountCommand < BaseTestBuoyCommand
867   def test_call
868     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1)
869     assert @buoy.is_new_game?(buoy_game.game_name)
870     @buoy.add_game buoy_game
871     assert !@buoy.is_new_game?(buoy_game.game_name)
872     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
873     rt = cmd.call
874     assert :continue, rt
875     assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
876   end
877
878   def test_call_not_exist
879     buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1)
880     assert @buoy.is_new_game?(buoy_game.game_name)
881     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
882     rt = cmd.call
883     assert :continue, rt
884     assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
885   end
886 end
887
888 #
889 #
890 class TestMonitorHandler < Test::Unit::TestCase
891   def test_not_equal
892     @player1 = MockPlayer.new
893     @handler1 = ShogiServer::MonitorHandler1.new @player1
894     @player2 = MockPlayer.new
895     @handler2 = ShogiServer::MonitorHandler1.new @player2
896
897     assert_not_equal(@handler1, @handler2)
898   end
899
900   def test_equal
901     @player1 = MockPlayer.new
902     @handler1 = ShogiServer::MonitorHandler1.new @player1
903     @handler2 = ShogiServer::MonitorHandler1.new @player1
904
905     assert_equal(@handler1, @handler2)
906   end
907 end
908
909 #
910 #
911 class TestMonitorHandler1 < Test::Unit::TestCase
912   def setup
913     @player = MockPlayer.new
914     @handler = ShogiServer::MonitorHandler1.new @player
915   end
916
917   def test_type
918     assert_equal(1, @handler.type)
919   end
920
921   def test_header
922     assert_equal("MONITOR", @handler.header)
923   end
924
925   def test_write_safe
926     @handler.write_safe("game_id", "hoge")
927     assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n", 
928                  @player.out.join)
929   end
930 end
931
932 #
933 #
934 class TestMonitorHandler2 < Test::Unit::TestCase
935   def setup
936     @player = MockPlayer.new
937     @handler = ShogiServer::MonitorHandler2.new @player
938   end
939
940   def test_type
941     assert_equal(2, @handler.type)
942   end
943
944   def test_header
945     assert_equal("MONITOR2", @handler.header)
946   end
947
948   def test_write_safe
949     @handler.write_safe("game_id", "hoge")
950     assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n", 
951                  @player.out.join)
952   end
953
954   def test_write_safe2
955     @handler.write_safe("game_id", "hoge\nfoo")
956     assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n", 
957                  @player.out.join)
958   end
959 end
960