OSDN Git Service

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