OSDN Git Service

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