OSDN Git Service

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