File: at_test.rb

package info (click to toggle)
ruby-clockwork 1.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 316 kB
  • ctags: 159
  • sloc: ruby: 1,365; makefile: 3
file content (117 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require File.expand_path('../../lib/clockwork', __FILE__)
require 'rubygems'
require 'mocha/setup'
require 'time'
require 'active_support/time'
require "minitest/autorun"

describe 'Clockwork::At' do
  def time_in_day(hour, minute)
    Time.new(2013, 1, 1, hour, minute, 0)
  end

  it '16:20' do
    at = Clockwork::At.parse('16:20')
    assert !at.ready?(time_in_day(16, 19))
    assert  at.ready?(time_in_day(16, 20))
    assert !at.ready?(time_in_day(16, 21))
  end

  it '8:20' do
    at = Clockwork::At.parse('8:20')
    assert !at.ready?(time_in_day(8, 19))
    assert  at.ready?(time_in_day(8, 20))
    assert !at.ready?(time_in_day(8, 21))
  end

  it '**:20 with two stars' do
    at = Clockwork::At.parse('**:20')

    assert !at.ready?(time_in_day(15, 19))
    assert  at.ready?(time_in_day(15, 20))
    assert !at.ready?(time_in_day(15, 21))

    assert !at.ready?(time_in_day(16, 19))
    assert  at.ready?(time_in_day(16, 20))
    assert !at.ready?(time_in_day(16, 21))
  end

  it '*:20 with one star' do
    at = Clockwork::At.parse('*:20')

    assert !at.ready?(time_in_day(15, 19))
    assert  at.ready?(time_in_day(15, 20))
    assert !at.ready?(time_in_day(15, 21))

    assert !at.ready?(time_in_day(16, 19))
    assert  at.ready?(time_in_day(16, 20))
    assert !at.ready?(time_in_day(16, 21))
  end

  it '16:**' do
    at = Clockwork::At.parse('16:**')

    assert !at.ready?(time_in_day(15, 59))
    assert  at.ready?(time_in_day(16, 00))
    assert  at.ready?(time_in_day(16, 30))
    assert  at.ready?(time_in_day(16, 59))
    assert !at.ready?(time_in_day(17, 00))
  end

  it '8:**' do
    at = Clockwork::At.parse('8:**')

    assert !at.ready?(time_in_day(7, 59))
    assert  at.ready?(time_in_day(8, 00))
    assert  at.ready?(time_in_day(8, 30))
    assert  at.ready?(time_in_day(8, 59))
    assert !at.ready?(time_in_day(9, 00))
  end

  it 'Saturday 12:00' do
    at = Clockwork::At.parse('Saturday 12:00')

    assert !at.ready?(Time.new(2010, 1, 1, 12, 00))
    assert  at.ready?(Time.new(2010, 1, 2, 12, 00)) # Saturday
    assert !at.ready?(Time.new(2010, 1, 3, 12, 00))
    assert  at.ready?(Time.new(2010, 1, 9, 12, 00))
  end

  it 'sat 12:00' do
    at = Clockwork::At.parse('sat 12:00')

    assert !at.ready?(Time.new(2010, 1, 1, 12, 00))
    assert  at.ready?(Time.new(2010, 1, 2, 12, 00))
    assert !at.ready?(Time.new(2010, 1, 3, 12, 00))
  end

  it 'invalid time 32:00' do
    assert_raises Clockwork::At::FailedToParse do
      Clockwork::At.parse('32:00')
    end
  end

  it 'invalid multi-line with Sat 12:00' do
    assert_raises Clockwork::At::FailedToParse do
      Clockwork::At.parse("sat 12:00\nreally invalid time")
    end
  end

  it 'invalid multi-line with 8:30' do
    assert_raises Clockwork::At::FailedToParse do
      Clockwork::At.parse("8:30\nreally invalid time")
    end
  end

  it 'invalid multi-line with *:10' do
    assert_raises Clockwork::At::FailedToParse do
      Clockwork::At.parse("*:10\nreally invalid time")
    end
  end

  it 'invalid multi-line with 12:**' do
    assert_raises Clockwork::At::FailedToParse do
      Clockwork::At.parse("12:**\nreally invalid time")
    end
  end
end