OSDN Git Service

Merge branch 'logger'
[shogi-server/shogi-server.git] / test / TC_floodgate_next_time_generator.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'shogi_server'
4 require 'shogi_server/league/floodgate'
5 require 'ftools'
6
7 $topdir = File.expand_path File.dirname(__FILE__)
8
9 class TestNextTimeGenerator < Test::Unit::TestCase
10   def setup
11     @game_name = "floodgate-3600-0"
12     @config_path = File.join($topdir, "#{@game_name}.conf")
13   end
14
15   def teardown
16     if File.exist? @config_path
17       FileUtils.rm @config_path
18     end
19   end
20
21   def test_assure_file_does_not_exist
22     assert !File.exist?(@config_path)
23   end
24
25   def test_factory_from_config_file
26     # no config file
27     assert !File.exist?(@config_path)
28     assert_instance_of ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0, 
29                        ShogiServer::League::Floodgate::NextTimeGenerator.factory(@game_name)
30
31     # there is a config file
32     FileUtils.touch(@config_path)
33     assert_instance_of ShogiServer::League::Floodgate::NextTimeGeneratorConfig,
34                        ShogiServer::League::Floodgate::NextTimeGenerator.factory(@game_name)
35   end
36 end
37
38 class TestNextTimeGenerator_900_0 < Test::Unit::TestCase
39   def setup
40     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_900_0.new
41   end
42
43   def test_0_min
44     now = Time.mktime(2009,12,25,22,0)
45     assert_equal(Time.mktime(2009,12,25,22,30), @next.call(now))
46   end
47
48   def test_20_min
49     now = Time.mktime(2009,12,25,22,20)
50     assert_equal(Time.mktime(2009,12,25,22,30), @next.call(now))
51   end
52
53   def test_30_min
54     now = Time.mktime(2009,12,25,22,30)
55     assert_equal(Time.mktime(2009,12,25,23,00), @next.call(now))
56   end
57
58   def test_50_min
59     now = Time.mktime(2009,12,25,22,50)
60     assert_equal(Time.mktime(2009,12,25,23,00), @next.call(now))
61   end
62
63   def test_50_min_next_day
64     now = Time.mktime(2009,12,25,23,50)
65     assert_equal(Time.mktime(2009,12,26,0,0), @next.call(now))
66   end
67
68   def test_50_min_next_month
69     now = Time.mktime(2009,11,30,23,50)
70     assert_equal(Time.mktime(2009,12,1,0,0), @next.call(now))
71   end
72
73   def test_50_min_next_year
74     now = Time.mktime(2009,12,31,23,50)
75     assert_equal(Time.mktime(2010,1,1,0,0), @next.call(now))
76   end
77 end
78
79 class TestNextTimeGenerator_3600_0 < Test::Unit::TestCase
80   def setup
81     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0.new
82   end
83
84   def test_22_00
85     now = Time.mktime(2009,12,25,22,0)
86     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
87   end
88
89   def test_22_30
90     now = Time.mktime(2009,12,25,22,0)
91     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
92   end
93
94   def test_23_00
95     now = Time.mktime(2009,12,25,23,0)
96     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
97   end
98
99   def test_23_30
100     now = Time.mktime(2009,12,25,23,30)
101     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
102   end
103
104   def test_00_00
105     now = Time.mktime(2009,12,26,0,0)
106     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
107   end
108
109   def test_23_30_next_month
110     now = Time.mktime(2009,11,30,23,30)
111     assert_equal(Time.mktime(2009,12,1,1,0), @next.call(now))
112   end
113
114   def test_23_30_next_year
115     now = Time.mktime(2009,12,31,23,30)
116     assert_equal(Time.mktime(2010,1,1,1,0), @next.call(now))
117   end
118 end
119
120 class TestNextTimeGeneratorConfig < Test::Unit::TestCase
121   def setup
122   end
123
124   def test_read
125     now = DateTime.new(2010, 6, 10, 21, 20, 15) # Thu
126     assert_equal DateTime.parse("10-06-2010 21:20:15"), now
127
128     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
129     assert_instance_of Time, ntc.call(now)
130     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
131     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:15"
132     assert_equal Time.parse("10-06-2010 22:15"), ntc.call(now)
133     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Fri 22:00"
134     assert_equal Time.parse("11-06-2010 22:00"), ntc.call(now)
135     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Sat 22:00"
136     assert_equal Time.parse("12-06-2010 22:00"), ntc.call(now)
137     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Sun 22:00"
138     assert_equal Time.parse("13-06-2010 22:00"), ntc.call(now)
139     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Mon 22:00"
140     assert_equal Time.parse("14-06-2010 22:00"), ntc.call(now)
141     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 20:00"
142     assert_equal Time.parse("17-06-2010 20:00"), ntc.call(now)
143   end
144
145   def test_read_time
146     now = Time.mktime(2010, 6, 10, 21, 20, 15)
147     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
148     assert_instance_of Time, ntc.call(now)
149   end
150
151   def test_read_change
152     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
153     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
154     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
155
156     now = DateTime.new(2010, 6, 10, 22, 0, 0) # Thu
157     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
158     assert_equal Time.parse("17-06-2010 22:00"), ntc.call(now)
159   end
160 end