|
4 | 4 | use util\profiling\Timer; |
5 | 5 | use util\Comparator; |
6 | 6 |
|
7 | | - |
8 | 7 | /** |
9 | 8 | * Tests Timer class |
10 | 9 | * |
11 | 10 | * @see xp://util.profiling.Timer |
12 | 11 | */ |
13 | 12 | class TimerTest extends TestCase { |
14 | | - |
15 | | - /** |
16 | | - * Tests elapsed time is zero if not started yet |
17 | | - * |
18 | | - */ |
| 13 | + |
| 14 | + #[@test] |
| 15 | + public function can_create() { |
| 16 | + new Timer(); |
| 17 | + } |
| 18 | + |
19 | 19 | #[@test] |
20 | | - public function initiallyZero() { |
| 20 | + public function elapsed_time_is_zero_before_timer_is_started() { |
21 | 21 | $this->assertEquals(0.0, (new Timer())->elapsedTime()); |
22 | 22 | } |
23 | 23 |
|
24 | | - /** |
25 | | - * Tests elapsed time after 100 milliseconds |
26 | | - * |
27 | | - */ |
28 | 24 | #[@test] |
29 | | - public function elapsedTimeGreaterThanZeroUsingStartAndStop() { |
30 | | - $fixture= new Timer(); |
31 | | - $fixture->start(); |
32 | | - usleep(100 * 1000); |
33 | | - $fixture->stop(); |
| 25 | + public function start_returns_timer() { |
| 26 | + $t= new Timer(); |
| 27 | + $this->assertEquals($t, $t->start()); |
| 28 | + } |
| 29 | + |
| 30 | + #[@test] |
| 31 | + public function stop_returns_timer() { |
| 32 | + $t= new Timer(); |
| 33 | + $t->start(); |
| 34 | + $this->assertEquals($t, $t->stop()); |
| 35 | + } |
| 36 | + |
| 37 | + #[@test] |
| 38 | + public function calling_stop_before_start_does_not_raise_exception() { |
| 39 | + (new Timer())->stop(); |
| 40 | + } |
| 41 | + |
| 42 | + #[@test] |
| 43 | + public function elapsed_time_after_50_milliseconds() { |
| 44 | + $fixture= (new Timer())->start(); |
| 45 | + usleep(50 * 1000); |
| 46 | + $elapsed= $fixture->stop()->elapsedTime(); |
| 47 | + $this->assertTrue($elapsed > 0.0, 'Elapsed time '.$elapsed.' should be greater than zero'); |
| 48 | + } |
| 49 | + |
| 50 | + #[@test] |
| 51 | + public function elapsed_time_without_stop_after_50_milliseconds() { |
| 52 | + $fixture= (new Timer())->start(); |
| 53 | + usleep(50 * 1000); |
34 | 54 | $elapsed= $fixture->elapsedTime(); |
35 | | - $this->assertTrue($elapsed > 0.0, 'Elapsed time should be greater than zero'); |
| 55 | + $this->assertTrue($elapsed > 0.0, 'Elapsed time '.$elapsed.' should be greater than zero'); |
36 | 56 | } |
37 | 57 |
|
38 | | - /** |
39 | | - * Tests elapsed time after 100 milliseconds |
40 | | - * |
41 | | - */ |
42 | 58 | #[@test] |
43 | | - public function elapsedTimeGreaterThanZeroUsingMeasure() { |
| 59 | + public function elapsed_time_measured_after_50_milliseconds() { |
44 | 60 | $fixture= Timer::measure(function() { |
45 | 61 | usleep(100 * 1000); |
46 | 62 | }); |
47 | 63 | $elapsed= $fixture->elapsedTime(); |
48 | | - $this->assertTrue($elapsed > 0.0, 'Elapsed time should be greater than zero'); |
| 64 | + $this->assertTrue($elapsed > 0.0, 'Elapsed time '.$elapsed.' should be greater than zero'); |
49 | 65 | } |
50 | 66 |
|
51 | | - /** |
52 | | - * Tests measure() |
53 | | - * |
54 | | - */ |
55 | 67 | #[@test, @expect('lang.IllegalArgumentException')] |
56 | | - public function notCallable() { |
| 68 | + public function not_callable_argument_passed_to_measure() { |
57 | 69 | Timer::measure('@not-callable@'); |
58 | 70 | } |
59 | 71 | } |
0 commit comments