OSDN Git Service

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