OSDN Git Service

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