OSDN Git Service

Backported a5c94012656902e73e00f46e7a4c7004b24d4578: test/TC_logger.rb depeneded...
[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
67     now = Time.mktime(2010,7,25,23,30)
68     assert_equal(Time.mktime(2010,7,26,0,0), @next.call(now))
69     now = Time.mktime(2010,7,26,23,30)
70     assert_equal(Time.mktime(2010,7,27,0,0), @next.call(now))
71     now = Time.mktime(2010,7,27,23,30)
72     assert_equal(Time.mktime(2010,7,28,0,0), @next.call(now))
73     now = Time.mktime(2010,7,28,23,30)
74     assert_equal(Time.mktime(2010,7,29,0,0), @next.call(now))
75     now = Time.mktime(2010,7,29,23,30)
76     assert_equal(Time.mktime(2010,7,30,0,0), @next.call(now))
77   end
78
79   def test_50_min_next_month
80     now = Time.mktime(2009,11,30,23,50)
81     assert_equal(Time.mktime(2009,12,1,0,0), @next.call(now))
82   end
83
84   def test_50_min_next_year
85     now = Time.mktime(2009,12,31,23,50)
86     assert_equal(Time.mktime(2010,1,1,0,0), @next.call(now))
87   end
88 end
89
90 class TestNextTimeGenerator_3600_0 < Test::Unit::TestCase
91   def setup
92     @next = ShogiServer::League::Floodgate::NextTimeGenerator_Floodgate_3600_0.new
93   end
94
95   def test_22_00
96     now = Time.mktime(2009,12,25,22,0)
97     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
98   end
99
100   def test_22_30
101     now = Time.mktime(2009,12,25,22,0)
102     assert_equal(Time.mktime(2009,12,25,23,0), @next.call(now))
103   end
104
105   def test_23_00
106     now = Time.mktime(2009,12,25,23,0)
107     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
108   end
109
110   def test_23_30
111     now = Time.mktime(2009,12,25,23,30)
112     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
113   end
114
115   def test_00_00
116     now = Time.mktime(2009,12,26,0,0)
117     assert_equal(Time.mktime(2009,12,26,1,0), @next.call(now))
118   end
119
120   def test_23_30_next_month
121     now = Time.mktime(2009,11,30,23,30)
122     assert_equal(Time.mktime(2009,12,1,1,0), @next.call(now))
123   end
124
125   def test_23_30_next_year
126     now = Time.mktime(2009,12,31,23,30)
127     assert_equal(Time.mktime(2010,1,1,1,0), @next.call(now))
128   end
129 end
130
131 class TestNextTimeGeneratorConfig < Test::Unit::TestCase
132   def setup
133   end
134
135   def test_read
136     now = DateTime.new(2010, 6, 10, 21, 20, 15) # Thu
137     assert_equal DateTime.parse("10-06-2010 21:20:15"), now
138
139     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
140     assert_instance_of Time, ntc.call(now)
141     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
142     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:15"
143     assert_equal Time.parse("10-06-2010 22:15"), ntc.call(now)
144     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Fri 22:00"
145     assert_equal Time.parse("11-06-2010 22:00"), ntc.call(now)
146     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Sat 22:00"
147     assert_equal Time.parse("12-06-2010 22:00"), ntc.call(now)
148     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Sun 22:00"
149     assert_equal Time.parse("13-06-2010 22:00"), ntc.call(now)
150     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Mon 22:00"
151     assert_equal Time.parse("14-06-2010 22:00"), ntc.call(now)
152     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 20:00"
153     assert_equal Time.parse("17-06-2010 20:00"), ntc.call(now)
154   end
155
156   def test_read_time
157     now = Time.mktime(2010, 6, 10, 21, 20, 15)
158     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
159     assert_instance_of Time, ntc.call(now)
160   end
161
162   def test_read_change
163     now = DateTime.new(2010, 6, 10, 21, 59, 59) # Thu
164     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
165     assert_equal Time.parse("10-06-2010 22:00"), ntc.call(now)
166
167     now = DateTime.new(2010, 6, 10, 22, 0, 0) # Thu
168     ntc = ShogiServer::League::Floodgate::NextTimeGeneratorConfig.new "Thu 22:00"
169     assert_equal Time.parse("17-06-2010 22:00"), ntc.call(now)
170   end
171 end