OSDN Git Service

Implemented a new command: %%FORK
[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_fork_command
232     cmd = ShogiServer::Command.factory("%%FORK server-denou-14400-60+p1+p2+20130223185013 buoy_denou-14400-60", @p)
233     assert_instance_of(ShogiServer::ForkCommand, cmd)
234   end
235
236   def test_void_command
237     cmd = ShogiServer::Command.factory("%%%HOGE", @p)
238     assert_instance_of(ShogiServer::VoidCommand, cmd)
239   end
240
241   def test_error
242     cmd = ShogiServer::Command.factory("should_be_error", @p)
243     assert_instance_of(ShogiServer::ErrorCommand, cmd)
244     cmd.call
245     assert_match /unknown command should_be_error/, cmd.msg
246   end
247
248   def test_error_login
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("LOGIN  hoge foo", @p)
260     assert_instance_of(ShogiServer::ErrorCommand, cmd)
261     cmd.call
262     assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
263
264     cmd = ShogiServer::Command.factory("LOGINhoge foo", @p)
265     assert_instance_of(ShogiServer::ErrorCommand, cmd)
266     cmd.call
267     assert_no_match /unknown command LOGIN hoge foo/, cmd.msg
268   end
269 end
270
271 #
272 #
273 class TestKeepAliveCommand < Test::Unit::TestCase 
274   def setup
275     @p = MockPlayer.new
276   end
277
278   def test_call
279     cmd = ShogiServer::KeepAliveCommand.new("", @p)
280     rc = cmd.call
281     assert_equal(:continue, rc)
282   end
283 end
284
285 #
286 #
287 class TestMoveCommand < Test::Unit::TestCase
288   def setup
289     @p = MockPlayer.new
290     @game = MockGame.new
291     @p.game = @game
292     @p.status = "game"
293   end
294
295   def test_call
296     cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
297     rc = cmd.call
298     assert_equal(:continue, rc)
299   end
300
301   def test_comment
302     cmd = ShogiServer::MoveCommand.new("+7776FU,'comment", @p)
303     rc = cmd.call
304     assert_equal(:continue, rc)
305     assert_equal("'*comment", @game.log.first)
306   end
307
308   def test_x1_return
309     @game.finish_flag = true
310     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
311     cmd = ShogiServer::MoveCommand.new("+7776FU", @p)
312     rc = cmd.call
313     assert_equal(:return, rc)
314   end
315 end
316
317 #
318 #
319 class TestSpecialComand < Test::Unit::TestCase
320   def setup
321     @p = MockPlayer.new
322     @game = MockGame.new
323     @p.game = @game
324     @p.status = "game"
325   end
326
327   def test_toryo
328     @game.finish_flag = true
329     cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
330     rc = cmd.call
331     assert_equal(:continue, rc)
332   end
333
334   def test_toryo_csa_protocol
335     @game.finish_flag = true
336     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
337     cmd = ShogiServer::SpecialCommand.new("%TORYO", @p)
338     rc = cmd.call
339     assert_equal(:return, rc)
340   end
341
342   def test_timeout
343     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
344     rc = cmd.call
345     assert_equal(:continue, rc)
346   end
347
348   def test_expired_game
349     @p.status = "agree_waiting"
350     @game.prepared_expire = true
351     assert(!@game.rejected)
352     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
353     rc = cmd.call
354     assert_equal(:continue, rc)
355     assert(@game.rejected)
356   end
357
358   def test_expired_game_csa_protocol
359     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
360     @p.status = "agree_waiting"
361     @game.prepared_expire = true
362     assert(!@game.rejected)
363     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
364     rc = cmd.call
365     assert_equal(:return, rc)
366     assert(@game.rejected)
367   end
368
369   def test_error
370     @p.status = "should_be_ignored"
371     cmd = ShogiServer::SpecialCommand.new(:timeout, @p)
372     rc = cmd.call
373     assert_equal(:continue, rc)
374   end
375 end
376
377 #
378 #
379 class TestExceptionCommand < Test::Unit::TestCase 
380   def setup
381     @p = MockPlayer.new
382   end
383
384   def test_call
385     cmd = ShogiServer::ExceptionCommand.new(:exception, @p)
386     rc = cmd.call
387     assert_equal(:return, rc)
388   end
389 end
390
391 #
392 #
393 class TestRejectCommand < Test::Unit::TestCase 
394   def setup
395     @p = MockPlayer.new
396     @game = MockGame.new
397     @p.game = @game
398     @p.status = "game"
399   end
400
401   def test_call
402     @p.status = "agree_waiting"
403     assert(!@game.rejected)
404     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
405     rc = cmd.call
406
407     assert_equal(:continue, rc)
408     assert(@game.rejected)
409   end
410
411   def test_call_csa_protocol
412     @p.protocol = ShogiServer::LoginCSA::PROTOCOL
413     @p.status = "agree_waiting"
414     assert(!@game.rejected)
415     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
416     rc = cmd.call
417
418     assert_equal(:return, rc)
419     assert(@game.rejected)
420   end
421
422   def test_error
423     @p.status = "should_be_ignored"
424     cmd = ShogiServer::RejectCommand.new("REJECT", @p)
425     rc = cmd.call
426
427     assert_equal(:continue, rc)
428     assert(!@game.rejected)
429   end
430 end
431
432 #
433 #
434 class TestAgreeCommand < Test::Unit::TestCase 
435   def setup
436     @p = MockPlayer.new
437     @game = MockGame.new
438     @p.game = @game
439     @p.status = "agree_waiting"
440   end
441
442   def test_not_start_yet
443     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
444     rc = cmd.call
445     assert_equal(:continue, rc)
446     assert(!@game.started)
447   end
448
449   def test_start
450     @game.is_startable_status = true
451     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
452     rc = cmd.call
453     assert_equal(:continue, rc)
454     assert(@game.started)
455   end
456
457   def test_error
458     @p.status = "should_be_ignored"
459     cmd = ShogiServer::AgreeCommand.new("AGREE", @p)
460     rc = cmd.call
461     assert_equal(:continue, rc)
462     assert(!@game.started)
463   end
464 end
465
466 #
467 #
468 class TestShowCommand < Test::Unit::TestCase 
469   def setup
470     @p = MockPlayer.new
471     @game = MockGame.new
472     @p.game = @game
473   end
474
475   def test_call
476     cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, @game)
477     rc = cmd.call
478
479     assert_equal(:continue, rc)
480   end
481
482   def test_call_nil_game
483     cmd = ShogiServer::ShowCommand.new("%%SHOW hoge", @p, nil)
484     rc = cmd.call
485
486     assert_equal(:continue, rc)
487   end
488 end
489
490 #
491 #
492 class TestMonitorOnCommand < Test::Unit::TestCase 
493   def setup
494     @p = MockPlayer.new
495     @game = MockGame.new
496     @p.game = @game
497   end
498
499   def test_call
500     cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, nil)
501     rc = cmd.call
502
503     assert_equal(:continue, rc)
504   end
505
506   def test_call_read_logfile
507     game = MockGame.new
508     cmd = ShogiServer::MonitorOnCommand.new("%%MONITORON hoge", @p, game)
509     rc = cmd.call
510     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)
511     assert_equal(:continue, rc)
512   end
513 end
514
515 #
516 #
517 class TestMonitor2OnCommand < Test::Unit::TestCase 
518   def setup
519     @p = MockPlayer.new
520     @game = MockGame.new
521     @p.game = @game
522   end
523
524   def test_call
525     cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, nil)
526     rc = cmd.call
527
528     assert_equal(:continue, rc)
529   end
530
531   def test_call_read_logfile
532     $tempfile = Tempfile.new("TC_command_test_call_read_logfile")
533     $tempfile.write "hoge\nfoo\n"
534     $tempfile.close
535     game = MockGame.new
536     def game.logfile
537       $tempfile.path
538     end
539     cmd = ShogiServer::Monitor2OnCommand.new("%%MONITOR2ON hoge", @p, game)
540     rc = cmd.call
541     assert_equal("##[MONITOR2][dummy_game_id] hoge\n##[MONITOR2][dummy_game_id] foo\n##[MONITOR2][dummy_game_id] +OK\n", @p.out.join)
542     assert_equal(:continue, rc)
543     $tempfile = nil
544   end
545 end
546
547 #
548 #
549 class TestMonitorOffCommand < Test::Unit::TestCase 
550   def setup
551     @p = MockPlayer.new
552     @game = MockGame.new
553     @p.game = @game
554   end
555
556   def test_call
557     cmd = ShogiServer::MonitorOffCommand.new("%%MONITOROFF hoge", @p, nil)
558     rc = cmd.call
559
560     assert_equal(:continue, rc)
561   end
562 end
563
564 #
565 #
566 class TestMonitor2OffCommand < Test::Unit::TestCase 
567   def setup
568     @p = MockPlayer.new
569     @game = MockGame.new
570     @p.game = @game
571   end
572
573   def test_call
574     cmd = ShogiServer::Monitor2OffCommand.new("%%MONITOR2OFF hoge", @p, nil)
575     rc = cmd.call
576
577     assert_equal(:continue, rc)
578   end
579 end
580
581 #
582 #
583 class TestHelpCommand < Test::Unit::TestCase 
584   def setup
585     @p = MockPlayer.new
586     @game = MockGame.new
587     @p.game = @game
588   end
589
590   def test_call
591     cmd = ShogiServer::HelpCommand.new("%%HELP", @p)
592     rc = cmd.call
593
594     assert_equal(:continue, rc)
595   end
596 end
597
598 #
599 #
600 class TestRatingCommand < Test::Unit::TestCase 
601   def setup
602     @p = MockPlayer.new
603     @game = MockGame.new
604     @p.game = @game
605   end
606
607   def test_call
608     players = [MockPlayer.new]
609     cmd = ShogiServer::RatingCommand.new("%%RATING", @p, players)
610     rc = cmd.call
611
612     assert_equal(:continue, rc)
613   end
614 end
615
616 #
617 #
618 class TestVersionCommand < Test::Unit::TestCase 
619   def setup
620     @p = MockPlayer.new
621     @game = MockGame.new
622     @p.game = @game
623   end
624
625   def test_call
626     cmd = ShogiServer::VersionCommand.new("%%VERSION", @p)
627     rc = cmd.call
628
629     assert_equal(:continue, rc)
630   end
631 end
632
633 #
634 #
635 class TestGameCommand < Test::Unit::TestCase 
636   def setup
637     @p = MockPlayer.new
638     @game = MockGame.new
639     @p.game = @game
640   end
641
642   def test_call_connected
643     @p.status = "connected"
644     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
645     rc = cmd.call
646
647     assert_equal(:continue, rc)
648     assert_equal("connected", @p.status)
649   end
650
651   def test_call_game_waiting
652     @p.status = "game_waiting"
653     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
654     rc = cmd.call
655
656     assert_equal(:continue, rc)
657     assert_equal("connected", @p.status)
658   end
659
660   def test_call_agree_waiting
661     @p.status = "agree_waiting"
662     cmd = ShogiServer::GameCommand.new("%%GAME", @p)
663     rc = cmd.call
664
665     assert_equal(:continue, rc)
666     assert_equal("agree_waiting", @p.status)
667   end
668 end
669
670 #
671 #
672 class TestChatCommand < Test::Unit::TestCase 
673   def setup
674     @p = MockPlayer.new
675     @game = MockGame.new
676     @p.game = @game
677   end
678
679   def test_call
680     players = [["dummy_name", MockPlayer.new]]
681     cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
682     rc = cmd.call
683
684     assert_equal(:continue, rc)
685   end
686
687   def test_call_csa_protocol
688     players = [["dummy_name", MockPlayer.new]]
689     players.each do |name, p|
690       p.protocol = ShogiServer::LoginCSA::PROTOCOL
691     end
692     cmd = ShogiServer::ChatCommand.new("%%CHAT hoge", @p, "dummy message", players)
693     rc = cmd.call
694
695     assert_equal(:continue, rc)
696   end
697 end
698
699 #
700 #
701 class TestListCommand < Test::Unit::TestCase 
702   def setup
703     @p = MockPlayer.new
704     @game = MockGame.new
705     @p.game = @game
706   end
707
708   def test_call
709     games = [["dummy_game_id", MockGame.new]]
710     cmd = ShogiServer::ListCommand.new("%%LIST", @p, games)
711     rc = cmd.call
712
713     assert_equal(:continue, rc)
714   end
715
716 end
717
718 #
719 #
720 class TestWhoCommand < Test::Unit::TestCase 
721   def setup
722     @p = MockPlayer.new
723     @game = MockGame.new
724     @p.game = @game
725   end
726
727   def test_call
728     players = [["dummy_name", MockPlayer.new]]
729     cmd = ShogiServer::WhoCommand.new("%%LIST", @p, players)
730     rc = cmd.call
731
732     assert_equal(:continue, rc)
733   end
734
735 end
736
737 #
738 #
739 class TestLogoutCommand < Test::Unit::TestCase 
740   def setup
741     @p = MockPlayer.new
742     @game = MockGame.new
743     @p.game = @game
744   end
745
746   def test_call
747     cmd = ShogiServer::LogoutCommand.new("LOGOUT", @p)
748     rc = cmd.call
749
750     assert_equal(:return, rc)
751   end
752
753 end
754
755 #
756 #
757 class TestChallengeCommand < Test::Unit::TestCase 
758   def setup
759     @p = MockPlayer.new
760     @game = MockGame.new
761   end
762
763   def test_call
764     cmd = ShogiServer::ChallengeCommand.new("CHALLENGE", @p)
765     rc = cmd.call
766
767     assert_equal(:continue, rc)
768   end
769 end
770
771 #
772 #
773 class TestSpaceCommand < Test::Unit::TestCase 
774   def setup
775     @p = MockPlayer.new
776     @game = MockGame.new
777   end
778
779   def test_call
780     cmd = ShogiServer::SpaceCommand.new("", @p)
781     rc = cmd.call
782
783     assert_equal(:continue, rc)
784   end
785 end
786
787 #
788 #
789 class TestErrorCommand < Test::Unit::TestCase 
790   def setup
791     @p = MockPlayer.new
792     @game = MockGame.new
793   end
794
795   def test_call
796     cmd = ShogiServer::ErrorCommand.new("", @p)
797     rc = cmd.call
798
799     assert_equal(:continue, rc)
800   end
801 end
802
803 class BaseTestBuoyCommand < Test::Unit::TestCase
804   def setup
805     @p = MockPlayer.new
806     $league = MockLeague.new
807
808     delete_buoy_yaml
809     @buoy = ShogiServer::Buoy.new
810   end
811
812   def teadown
813     delete_buoy_yaml
814   end
815
816   def delete_buoy_yaml
817     file = File.join($topdir, "buoy.yaml")
818     File.delete file if File.exist? file
819   end
820
821   def test_dummy
822     assert true
823   end
824 end
825
826
827 #
828 #
829 class TestSetBuoyCommand < BaseTestBuoyCommand
830   
831   def setup
832     super
833     @p.name = "set_buoy_player"
834   end
835
836   def test_call_2
837     assert @buoy.is_new_game?("buoy_hoge-1500-0")
838     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 2
839     rt = cmd.call
840     assert :continue, rt
841     assert !@buoy.is_new_game?("buoy_hoge-1500-0")
842     assert !$p1.out.empty?
843     assert !$p2.out.empty?
844     buoy_game2 = @buoy.get_game("buoy_hoge-1500-0")
845     assert_equal ShogiServer::BuoyGame.new("buoy_hoge-1500-0", "+7776FU", @p.name, 1, nil, nil), buoy_game2
846   end
847
848   def test_call_1
849     assert @buoy.is_new_game?("buoy_hoge-1500-0")
850     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_hoge-1500-0", "+7776FU", 1
851     rt = cmd.call
852     assert :continue, rt
853     assert @buoy.is_new_game?("buoy_hoge-1500-0")
854     assert !$p1.out.empty?
855     assert !$p2.out.empty?
856   end
857
858   def test_call_error_not_buoy_game_name
859     assert @buoy.is_new_game?("buoy_hoge-1500-0")
860     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
861     rt = cmd.call
862     assert :continue, rt
863     assert $p1.out.empty?
864     assert $p2.out.empty?
865     assert @buoy.is_new_game?("buoy_hoge-1500-0")
866   end
867
868   def test_call_error_duplicated_game_name
869     assert @buoy.is_new_game?("buoy_duplicated-1500-0")
870     bg = ShogiServer::BuoyGame.new("buoy_duplicated-1500-0", ["+7776FU"], @p.name, 1, nil, nil)
871     @buoy.add_game bg
872     assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
873     
874     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
875     rt = cmd.call
876     assert :continue, rt
877     assert $p1.out.empty?
878     assert $p2.out.empty?
879     assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
880   end
881
882   def test_call_error_bad_moves
883     assert @buoy.is_new_game?("buoy_badmoves-1500-0")
884     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
885     rt = cmd.call
886     assert :continue, rt
887     assert $p1.out.empty?
888     assert $p2.out.empty?
889     assert @buoy.is_new_game?("buoy_badmoves-1500-0")
890   end
891
892   def test_call_error_bad_counter
893     assert @buoy.is_new_game?("buoy_badcounter-1500-0")
894     cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
895     rt = cmd.call
896     assert :continue, rt
897     assert $p1.out.empty?
898     assert $p2.out.empty?
899     assert @buoy.is_new_game?("buoy_badcounter-1500-0")
900   end
901 end
902
903
904 #
905 #
906 class TestDeleteBuoyCommand < BaseTestBuoyCommand
907   def test_call
908     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1, nil, nil)
909     assert @buoy.is_new_game?(buoy_game.game_name)
910     @buoy.add_game buoy_game
911     assert !@buoy.is_new_game?(buoy_game.game_name)
912     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
913     rt = cmd.call
914     assert :continue, rt
915     assert $p1.out.empty?
916     assert $p2.out.empty?
917     assert @buoy.is_new_game?(buoy_game.game_name)
918   end
919
920   def test_call_not_exist
921     buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1, nil, nil)
922     assert @buoy.is_new_game?(buoy_game.game_name)
923     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
924     rt = cmd.call
925     assert :continue, rt
926     assert $p1.out.empty?
927     assert $p2.out.empty?
928     assert @buoy.is_new_game?(buoy_game.game_name)
929   end
930
931   def test_call_another_player
932     buoy_game = ShogiServer::BuoyGame.new("buoy_anotherplayer-1500-0", "+7776FU", "another_player", 1, nil, nil)
933     assert @buoy.is_new_game?(buoy_game.game_name)
934     @buoy.add_game(buoy_game)
935     assert !@buoy.is_new_game?(buoy_game.game_name)
936
937     cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
938     rt = cmd.call
939     assert :continue, rt
940     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
941     assert !@buoy.is_new_game?(buoy_game.game_name)
942   end
943 end
944
945 #
946 #
947 class TestGetBuoyCountCommand < BaseTestBuoyCommand
948   def test_call
949     buoy_game = ShogiServer::BuoyGame.new("buoy_testdeletebuoy-1500-0", "+7776FU", @p.name, 1, nil, nil)
950     assert @buoy.is_new_game?(buoy_game.game_name)
951     @buoy.add_game buoy_game
952     assert !@buoy.is_new_game?(buoy_game.game_name)
953     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
954     rt = cmd.call
955     assert :continue, rt
956     assert_equal ["##[GETBUOYCOUNT] 1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
957   end
958
959   def test_call_not_exist
960     buoy_game = ShogiServer::BuoyGame.new("buoy_notexist-1500-0", "+7776FU", @p.name, 1, nil, nil)
961     assert @buoy.is_new_game?(buoy_game.game_name)
962     cmd = ShogiServer::GetBuoyCountCommand.new "%%GETBUOYCOUNT", @p, buoy_game.game_name
963     rt = cmd.call
964     assert :continue, rt
965     assert_equal ["##[GETBUOYCOUNT] -1\n", "##[GETBUOYCOUNT] +OK\n"], @p.out
966   end
967 end
968
969 #
970 #
971 class TestMonitorHandler < Test::Unit::TestCase
972   def test_not_equal_players
973     @player1 = MockPlayer.new
974     @handler1 = ShogiServer::MonitorHandler1.new @player1
975     @player2 = MockPlayer.new
976     @handler2 = ShogiServer::MonitorHandler1.new @player2
977
978     assert_not_equal(@handler1, @handler2)
979   end
980
981   def test_equal
982     @player1 = MockPlayer.new
983     @handler1 = ShogiServer::MonitorHandler1.new @player1
984     @handler2 = ShogiServer::MonitorHandler1.new @player1
985
986     assert_equal(@handler1, @handler2)
987   end
988 end
989
990 #
991 #
992 class TestMonitorHandler1 < Test::Unit::TestCase
993   def setup
994     @player = MockPlayer.new
995     @handler = ShogiServer::MonitorHandler1.new @player
996   end
997
998   def test_type
999     assert_equal(1, @handler.type)
1000   end
1001
1002   def test_header
1003     assert_equal("MONITOR", @handler.header)
1004   end
1005   
1006   def test_equal
1007     assert_equal @handler, @handler
1008     assert_not_equal @handler, nil
1009   end
1010
1011   def test_not_equal
1012     assert_not_equal(@handler, ShogiServer::MonitorHandler2.new(@player))
1013   end
1014
1015   def test_write_safe
1016     @handler.write_safe("game_id", "hoge")
1017     assert_equal("##[MONITOR][game_id] hoge\n##[MONITOR][game_id] +OK\n", 
1018                  @player.out.join)
1019   end
1020 end
1021
1022 #
1023 #
1024 class TestMonitorHandler2 < Test::Unit::TestCase
1025   def setup
1026     @player = MockPlayer.new
1027     @handler = ShogiServer::MonitorHandler2.new @player
1028   end
1029
1030   def test_type
1031     assert_equal(2, @handler.type)
1032   end
1033
1034   def test_header
1035     assert_equal("MONITOR2", @handler.header)
1036   end
1037
1038   def test_equal
1039     assert_equal @handler, @handler
1040     assert_not_equal @handler, nil
1041   end
1042
1043   def test_not_equal
1044     assert_not_equal(@handler, ShogiServer::MonitorHandler1.new(@player))
1045   end
1046
1047   def test_write_safe
1048     @handler.write_safe("game_id", "hoge")
1049     assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] +OK\n", 
1050                  @player.out.join)
1051   end
1052
1053   def test_write_safe2
1054     @handler.write_safe("game_id", "hoge\nfoo")
1055     assert_equal("##[MONITOR2][game_id] hoge\n##[MONITOR2][game_id] foo\n##[MONITOR2][game_id] +OK\n", 
1056                  @player.out.join)
1057   end
1058 end
1059