Implement ActionList::Entry#clear.
[kaya.git] / test / test_clock.rb
blob80a3ee76508b6c4b1afb93234a5088860ceca556
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'test/unit'
9 require 'clock'
11 class TestClock < Test::Unit::TestCase
12   class FakeTimer
13     def on(*args)
14     end
15     
16     def single_shot=(val)
17     end
18     
19     def start(ms)
20     end
21     
22     def stop
23     end
24   end
26   def test_main
27     timer = nil
28     
29     # 2 seconds main time
30     clock = Clock.new(2000, 0, FakeTimer)
31     clock.on(:timer) {|timer|}
32     clock.start
33     
34     clock.tick
35     assert_nil timer
36     
37     8.times { clock.tick }
38     assert_nil timer
39     
40     clock.tick
41     assert_equal(1000, timer)
42     timer = nil
43     
44     9.times { clock.tick }
45     assert_nil timer
46     
47     clock.tick
48     assert_equal(0, timer)
49   end
50   
51   def test_increment
52     timer = nil
53     
54     # 10 seconds main time, 1 second increment
55     clock = Clock.new(10000, 1000, FakeTimer)
56     clock.on(:timer) {|timer|}
57     clock.start
58     
59     80.times { clock.tick }
60     assert_equal(2000, timer)
61     timer = nil
62     
63     clock.stop
64     assert_equal(3000, timer)
65     clock.start
66     
67     15.times { clock.tick }
68     assert_equal(2000, timer)
69     timer = nil
70     
71     14.times { clock.tick }
72     assert_equal(1000, timer)
73     timer = nil
74     
75     clock.tick
76     assert_equal(0, timer)
77     timer = nil
78     
79     7.times { clock.tick }
80     assert_nil timer
81   end
82   
83   def test_resolution
84     timer = nil
85     
86     clock = Clock.new(5000, 0, FakeTimer)
87     clock.on(:timer) {|timer|}
88     clock.resolution = 100
89     clock.start
90     
91     24.times { clock.tick }
92     assert_equal(2600, timer)
93     timer = nil
94     
95     clock.tick
96     assert_equal(2500, timer)
97     timer = nil
98     
99     30.times { clock.tick }
100     assert_equal(-500, timer)
101   end
102   
103   def test_invalid_resolution
104     clock = Clock.new(10, 0, FakeTimer)
105     assert_raise(RuntimeError) do
106       clock.resolution = 50
107     end
108   end