Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add spec / Fix typo

  • Loading branch information...
commit 42280ae9e12899526eef8867df8d1c949d1a2929 1 parent 3137591
@labocho authored
View
2  lib/cronie.rb
@@ -30,7 +30,7 @@ def load(path)
@queue = :cronie
def run_async(time)
- Resque.enqueue(Cron, time)
+ Resque.enqueue(Cronie, time)
end
# ===== Resque 対応ここまで =====
end
View
31 lib/cronie/schedule.rb
@@ -37,6 +37,13 @@ def self.parse(text, type)
element
end
+ def self.parse_special_element(str)
+ str = str.downcase[0..2]
+ months = [nil] + %w(jan feb mar apr may jun jul aug sep oct nov dec) # index と月の数を合わせるため、最初に nil をいれておく
+ weekdays = %w(sun mon tue wed thu fri sat)
+ months.index(str) || weekdays.index(str)
+ end
+
def initialize(attributes = {})
attributes.each do |k, v|
send("#{k}=", v)
@@ -99,18 +106,13 @@ def expand_asterisk
private :elements, :elements=
# crontab 形式のスケジュール指定文字列から新しいインスタンスをつくる
- # 内部形式は list と step の 2 要素配列の、5 要素配列
- # 0 1,2-3,4 */10 * * なら
- # [ [[0], nil],
- # [[1,2..3,4], nil],
- # [[:*], 10],
- # [[:*], nil],
- # [[:*], nil]
- # ] になる
+ # 内部形式は Element の 5 要素配列
# パースに失敗すると ParseError を投げる
def self.parse!(str)
return parse_special(str) if str =~ /\A@.+\z/
- raise ParseError, "Unexpected schedule string #{str.inspect}" unless str =~ /\A.+ .+ .+ .+ .+\z/
+ unless str =~ %r(\A\s*\S+\s+\S+\s+\S+\s+\S+\s+\S+\s*\z)
+ raise ParseError, "Unexpected schedule string #{str.inspect}"
+ end
elements = str.split(" ").zip(Element::TYPES).map do |text, type|
Element.parse(text, type)
@@ -143,12 +145,12 @@ def to_s
end
def inspect
- "#<Cron::Schedule: #{to_s}>"
+ "#<Cronie::Schedule: #{to_s}>"
end
# 基底クラスに Schedule を持ち、to_s が同じなら true
def ==(other)
- other.is_a?(Cron::Schedule) && to_s == other.to_s
+ other.is_a?(Cronie::Schedule) && to_s == other.to_s
end
private
@@ -170,12 +172,5 @@ def self.parse_special(str)
raise ParseError, "Unexpected schedule string: #{str}"
end
end
-
- def self.parse_special_element(str)
- str = str.downcase[0..2]
- months = [nil] + %w(jan feb mar apr may jun jul aug sep oct nov dec) # index と月の数を合わせるため、最初に nil をいれておく
- weekdays = %w(sun mon tue wed thu fri sat)
- months.index(str) || weekdays.index(str)
- end
end
end
View
2  lib/cronie/version.rb
@@ -1,3 +1,3 @@
module Cronie
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
247 spec/cronie/schedule_spec.rb
@@ -1,71 +1,206 @@
+# encoding: UTF-8
require "spec_helper"
module Cronie
describe Cronie::Schedule do
- # RSpec::Matchers.define :match do |target|
- # match do |receiver|
- # receiver =~ target
- # end
- # end
-
- context "parse 0 1,2-3,4 */10 * *" do
- subject { Schedule.parse! "0 1,2-3,4 */10 * *" }
- its(:to_s) { should == "0 1,2-3,4 */10 * *" }
- end
- context "exactly match" do
- subject { Schedule.parse! "* 10 * * *" }
- it { should =~ Time.new(2011, 11, 11, 10, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 9, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 11, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 20, 12, 34) }
- end
- context "range include" do
- subject { Schedule.parse! "* 3-5 * * *" }
- it { should =~ Time.new(2011, 11, 11, 3, 12, 34) }
- it { should =~ Time.new(2011, 11, 11, 4, 12, 34) }
- it { should =~ Time.new(2011, 11, 11, 5, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 2, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 6, 12, 34) }
+ describe ".parse!" do
+ it "should parse * * * * *" do
+ Schedule.parse! "* * * * *"
+ end
+ it "should parse * 10 * 10 *" do
+ Schedule.parse! "* 10 * 10 *"
+ end
+ it "should parse * */10 * */10 *" do
+ Schedule.parse! "* */10 * */10 *"
+ end
+ it "should parse * 10 * */10 *" do
+ Schedule.parse! "* 10 * */10 *"
+ end
+ it "should parse string include extra spaces" do
+ Schedule.parse! " \t* \t10 * \t */10 * "
+ end
+ it "should raise parsing less elements" do
+ expect {
+ Schedule.parse! "* * * *"
+ }.to raise_error
+ end
+ it "should raise parsing extra elements" do
+ expect {
+ Schedule.parse! "* * * * * *"
+ }.to raise_error
+ end
end
- context "list " do
- subject { Schedule.parse! "* 1,5 * * *" }
- it { should =~ Time.new(2011, 11, 11, 1, 12, 34) }
- it { should =~ Time.new(2011, 11, 11, 5, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 0, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 2, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 4, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 6, 12, 34) }
- it { should_not =~ Time.new(2011, 11, 11, 10, 12, 34) }
+
+ describe "#to_s" do
+ it "should return parsable string" do
+ Schedule.parse("* 10 * */10 *").to_s.should == "* 10 * */10 *"
+ end
+ it "should remove spaces" do
+ Schedule.parse(" \t* \t10 * \t */10 * ").to_s.should == "* 10 * */10 *"
+ end
end
- context "step" do
- context "with asterisk" do
- subject { Schedule.parse! "*/12 * * * *" }
- ok = [0, 12, 24, 36, 48]
- ng = (0..59).to_a - ok
- ok.each do |i|
- it { should =~ Time.new(2011, 11, 11, 11, i, 34) }
+ describe "=~" do
+ def self.min(i)
+ Time.new(2011, 11, 7, 0, i)
+ end
+ def self.hour(i)
+ Time.new(2011, 11, 7, i, 12)
+ end
+ def self.day(i)
+ Time.new(2011, 11, i, 14, 12)
+ end
+ def self.month(i)
+ Time.new(2011, i, 7, 14, 12)
+ end
+ def self.wday(i)
+ # 2011-11-06 is Sunday
+ Time.new(2011, 11, 6 + i, 14, 12)
+ end
+ def self.it_should_match_only(options = {})
+ case
+ when options[:minutes]
+ variations = (0..59).map{|i| min(i) }
+ ok = options[:minutes].map{|i| min(i) }
+ when options[:hours]
+ variations = (0..23).map{|i| hour(i) }
+ ok = options[:hours].map{|i| hour(i) }
+ when options[:days]
+ variations = (1..30).map{|i| day(i) }
+ ok = options[:days].map{|i| day(i) }
+ when options[:months]
+ variations = (1..12).map{|i| month(i) }
+ ok = options[:months].map{|i| month(i) }
+ when options[:weekdays]
+ variations = (0..6).map{|i| wday(i) }
+ ok = options[:weekdays].map{|i| wday(i) }
end
-
- ng.each do |i|
- it {
- should_not =~ Time.new(2011, 11, 11, 11, i, 34)
- }
+ ok.each do |time|
+ it { should =~ time }
+ end
+ (variations - ok).each do |time|
+ it { should_not =~ time }
end
end
- context "with range" do
- subject { Schedule.parse! "1-15/3 * * * *" }
- ok = [1, 4, 7, 10, 13]
- ng = (0..59).to_a - ok
- ok.each do |i|
- it { should =~ Time.new(2011, 11, 11, 11, i, 34) }
+ context "minutes" do
+ context "asterisk: *" do
+ subject { Schedule.parse("* * * * *") }
+ it_should_match_only minutes: (0..59)
end
-
- ng.each do |i|
- it {
- should_not =~ Time.new(2011, 11, 11, 11, i, 34)
- }
+ context "number: 13" do
+ subject { Schedule.parse("13 * * * *") }
+ it_should_match_only minutes: [13]
+ end
+ context "list: 13,17" do
+ subject { Schedule.parse! "13,17 * * * *" }
+ it_should_match_only minutes: [13, 17]
+ end
+ context "range: 13-17" do
+ subject { Schedule.parse! "13-17 * * * *" }
+ it_should_match_only minutes: (13..17)
+ end
+ context "list and range: 11,13-17,19" do
+ subject { Schedule.parse! "7-9,11,13-17,19 * * * *" }
+ it_should_match_only minutes: [7, 8, 9, 11, 13, 14, 15, 16, 17, 19]
+ end
+ context "asterisk with step: */13" do
+ subject { Schedule.parse("*/13 * * * *") }
+ it_should_match_only minutes: [0, 13, 26, 39, 52]
end
+ context "list with step: 1,2,3,5,7,11/2" do
+ subject { Schedule.parse("1,2,3,5,7,11/2 * * * *") }
+ it_should_match_only minutes: [1, 3, 7]
+ end
+ context "range with step: 1-15/3" do
+ subject { Schedule.parse("1-15/3 * * * *") }
+ it_should_match_only minutes: [1, 4, 7, 10, 13]
+ end
+ context "list and range with step: 1-3,5,7,11,13/2" do
+ subject { Schedule.parse("1-3,5,7,11,13/2 * * * *") }
+ it_should_match_only minutes: [1, 3, 7, 13]
+ end
+ context "invalid number: 60" do
+ subject { Schedule.parse("60 * * * *") }
+ it_should_match_only minutes: []
+ end
+ end
+ context "hours" do
+ context "asterisk: *" do
+ subject { Schedule.parse("* * * * *") }
+ it_should_match_only hours: (0..23)
+ end
+ context "number: 13" do
+ subject { Schedule.parse("* 13 * * *") }
+ it_should_match_only hours: [13]
+ end
+ context "asterisk with step: */4" do
+ subject { Schedule.parse("* */4 * * *") }
+ it_should_match_only hours: [0, 4, 8, 12, 16, 20]
+ end
+ context "invalid number: 24" do
+ subject { Schedule.parse("* 24 * * *") }
+ it_should_match_only hours: []
+ end
+ end
+ context "day" do
+ context "asterisk: *" do
+ subject { Schedule.parse("* * * * *") }
+ it_should_match_only days: (1..30)
+ end
+ context "number: 13" do
+ subject { Schedule.parse("* * 13 * *") }
+ it_should_match_only days: [13]
+ end
+ context "asterisk with step: */4" do
+ subject { Schedule.parse("* * */4 * *") }
+ it_should_match_only days: [1, 5, 9, 13, 17, 21, 25, 29]
+ end
+ context "invalid number: 32" do
+ subject { Schedule.parse("* * 32 * *") }
+ it_should_match_only days: []
+ end
+ end
+ context "months" do
+ context "asterisk: *" do
+ subject { Schedule.parse("* * * * *") }
+ it_should_match_only months: (1..12)
+ end
+ context "number: 7" do
+ subject { Schedule.parse("* * * 7 *") }
+ it_should_match_only months: [7]
+ end
+ context "asterisk with step: */4" do
+ subject { Schedule.parse("* * * */4 *") }
+ it_should_match_only months: [1, 5, 9]
+ end
+ context "invalid number: 13" do
+ subject { Schedule.parse("* * * 13 *") }
+ it_should_match_only months: []
+ end
+ end
+ context "weekdays" do
+ context "asterisk: *" do
+ subject { Schedule.parse("* * * * *") }
+ it_should_match_only weekdays: (0..6)
+ end
+ context "number: 3" do
+ subject { Schedule.parse("* * * * 3") }
+ it_should_match_only weekdays: [3]
+ end
+ context "asterisk with step: */3" do
+ subject { Schedule.parse("* * * * */3") }
+ it_should_match_only weekdays: [0, 3, 6]
+ end
+ context "invalid number: 7" do
+ subject { Schedule.parse("* * * * 7") }
+ it_should_match_only weekdays: []
+ end
+ end
+ context "multiple constraints" do
+ subject { Schedule.parse("0 3 * * *") }
+ it { should_not =~ Time.new(2011, 11, 7, 2, 0) }
+ it { should_not =~ Time.new(2011, 11, 7, 3, 10) }
+ it { should =~ Time.new(2011, 11, 7, 3, 0) }
end
end
end
View
30 spec/cronie/task_spec.rb
@@ -0,0 +1,30 @@
+# encoding: UTF-8
+require 'spec_helper'
+
+module Cronie
+ describe Task do
+ describe "#do" do
+ it "@proc.call が呼ばれる" do
+ t = Time.now
+ called = false
+
+ task = Task.new :name, "* * * * *" do |time|
+ time.should == t
+ called = true
+ end
+ task.do(t)
+ called.should be_true
+ end
+ it "shedule が一致しないときは呼ばれない" do
+ t = Time.new(2011, 11, 7, 0, 0, 0)
+ called = false
+
+ task = Task.new :name, "1 * * * *" do |time|
+ called = true
+ end
+ task.do(t)
+ called.should be_false
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.