OSDN Git Service

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