OSDN Git Service

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