Skip to content

Commit

Permalink
Undo defaulting nanoseconds to 0; remove class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
docelic committed Dec 25, 2023
1 parent 21f2541 commit 8dcf1c0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
33 changes: 22 additions & 11 deletions spec/virtualtime_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe VirtualTime do
end

it "can match Crystal's Times" do
vt = VirtualTime.new nanosecond: nil
vt = VirtualTime.new

vt.matches?(Time.local).should be_true

Expand Down Expand Up @@ -128,7 +128,7 @@ describe VirtualTime do
vt.second = true
vt.location = Time::Location.load("Europe/Berlin")
# vt.millisecond = ->( val : Int32) { true }
vt.to_yaml.should eq "---\nmonth: 3\nday: 1,2\nhour: 10..20\nminute: 10,12,14,16,18,20\nsecond: true\nnanosecond: 0\nlocation: Europe/Berlin\n"
vt.to_yaml.should eq "---\nmonth: 3\nday: 1,2\nhour: 10..20\nminute: 10,12,14,16,18,20\nsecond: true\nlocation: Europe/Berlin\n"
end
it "converts from YAML" do
vt = VirtualTime.from_yaml "---\nmonth: 3\nday: 1,2\nhour: 10..20\nminute: 10,12,14,16,18,20\nsecond: true\nlocation: Europe/Berlin\n"
Expand All @@ -137,9 +137,11 @@ describe VirtualTime do
vt.hour.should eq 10..20
vt.second.should eq true
vt.location.should eq Time::Location.load("Europe/Berlin")
#vt.default_match?.should eq false
end

it "does range comparison properly" do
vt = VirtualTime.new
a = 6..10
b = 2..4
c = 4..6
Expand All @@ -150,14 +152,23 @@ describe VirtualTime do
h = 10..12
i = 9..11
j = 20..24
VirtualTime.matches?(a, b).should be_false
VirtualTime.matches?(a, c).should be_true
VirtualTime.matches?(a, d).should be_true
VirtualTime.matches?(a, e).should be_true
VirtualTime.matches?(a, f).should be_true
VirtualTime.matches?(a, g).should be_true
VirtualTime.matches?(a, h).should be_true
VirtualTime.matches?(a, i).should be_true
VirtualTime.matches?(a, j).should be_false
vt.matches?(a, b).should be_false
vt.matches?(a, c).should be_true
vt.matches?(a, d).should be_true
vt.matches?(a, e).should be_true
vt.matches?(a, f).should be_true
vt.matches?(a, g).should be_true
vt.matches?(a, h).should be_true
vt.matches?(a, i).should be_true
vt.matches?(a, j).should be_false
end

# Other

it "honors default_match?" do
vt = VirtualTime.new
vt.matches?(Time.local).should be_true
VirtualTime.default_match = false
vt.matches?(Time.local).should be_false
end
end
63 changes: 33 additions & 30 deletions src/virtualtime.cr
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ class VirtualTime
@[YAML::Field(converter: VirtualTime::TimeLocationConverter)]
property location : Time::Location?

# Default match result if one of field values matched is `nil`
# Class-default match result if one of field values matched is `nil`
class_property? default_match : Bool = true

def initialize(@year = nil, @month = nil, @day = nil, @hour = nil, @minute = nil, @second = nil, *, @millisecond = nil, @nanosecond = 0, @day_of_week = nil, @day_of_year = nil, @week = nil)
# Instance-default match result if one of field values matched is `nil`
#property? default_match : Bool = true

def initialize(@year = nil, @month = nil, @day = nil, @hour = nil, @minute = nil, @second = nil, *, @millisecond = nil, @nanosecond = nil, @day_of_week = nil, @day_of_year = nil, @week = nil) #, @default_match = @@default_match)
end

def initialize(*, @year, @week, @day_of_week = nil, @hour = nil, @minute = nil, @second = nil, @millisecond = nil, @nanosecond = 0)
def initialize(*, @year, @week, @day_of_week = nil, @hour = nil, @minute = nil, @second = nil, @millisecond = nil, @nanosecond = nil) #, @default_match = self.class.default_match?)
end

def initialize(@year, @month, @day, @week, @day_of_week, @day_of_year, @hour, @minute, @second, @millisecond, @nanosecond, @location)
Expand Down Expand Up @@ -80,38 +83,38 @@ class VirtualTime
# Returns whether `VirtualTime` matches the date part of specified time
def matches_date?(time : TimeOrVirtualTime = Time.local)
adjust_location
self.class.matches?(year, time.year, 9999) &&
self.class.matches?(month, time.month, 12) &&
self.class.matches?(day, time.day, TimeHelper.days_in_month(time)) &&
self.class.matches?(week, TimeHelper.week(time), TimeHelper.weeks_in_year(time)) &&
self.class.matches?(day_of_week, TimeHelper.day_of_week(time), 7) &&
self.class.matches?(day_of_year, TimeHelper.day_of_year(time), TimeHelper.days_in_year(time))
matches?(year, time.year, 9999) &&
matches?(month, time.month, 12) &&
matches?(day, time.day, TimeHelper.days_in_month(time)) &&
matches?(week, TimeHelper.week(time), TimeHelper.weeks_in_year(time)) &&
matches?(day_of_week, TimeHelper.day_of_week(time), 7) &&
matches?(day_of_year, TimeHelper.day_of_year(time), TimeHelper.days_in_year(time))
end

# Returns whether `VirtualTime` matches the time part of specified time
def matches_time?(time : TimeOrVirtualTime = Time.local)
adjust_location
self.class.matches?(hour, time.hour, 23) &&
self.class.matches?(minute, time.minute, 59) &&
self.class.matches?(second, time.second, 59) &&
self.class.matches?(millisecond, time.millisecond, 999) &&
self.class.matches?(nanosecond, time.nanosecond, 999_999_999)
matches?(hour, time.hour, 23) &&
matches?(minute, time.minute, 59) &&
matches?(second, time.second, 59) &&
matches?(millisecond, time.millisecond, 999) &&
matches?(nanosecond, time.nanosecond, 999_999_999)
end

# Performs matching between VirtualTime and other supported types
def self.matches?(a : Nil, b, max = nil)
def matches?(a : Nil, b, max = nil)
return false if b == false
default_match?
self.class.default_match?
end

# :ditto:
def self.matches?(a : Bool, b, max = nil)
def matches?(a : Bool, b, max = nil)
return false if b == false
a
end

# :ditto:
def self.matches?(a : Int, b : Int, max = nil)
def matches?(a : Int, b : Int, max = nil)
if max
a = max + a if a < 0
b = max + b if b < 0
Expand All @@ -121,15 +124,15 @@ class VirtualTime

# # ###### Possibly enable
# # :ditto:
# def self.matches?(a : Array(Int), b : Int, max = nil)
# def matches?(a : Array(Int), b : Int, max = nil)
# a.each do |aa|
# return true if matches? aa, b, max
# end
# false
# end

# # :ditto:
# def self.matches?(a : Range(Int, Int), b : Int, max = nil)
# def matches?(a : Range(Int, Int), b : Int, max = nil)
# if max && (a.begin < 0 || a.end < 0)
# ab = a.begin < 0 ? max + a.begin : a.begin
# ae = a.end < 0 ? max + a.end : a.end
Expand All @@ -142,7 +145,7 @@ class VirtualTime
# end

# # :ditto:
# def self.matches?(a : Steppable::StepIterator(Int, Int, Int), b : Int, max = nil)
# def matches?(a : Steppable::StepIterator(Int, Int, Int), b : Int, max = nil)
# if max && (a.current < 0 || a.limit < 0)
# ab = a.current < 0 ? max + a.current : a.current
# ae = a.limit < 0 ? max + a.limit : a.limit
Expand All @@ -159,7 +162,7 @@ class VirtualTime
# # ###### Possibly enable

# :ditto:
def self.matches?(a : Enumerable(Int), b : Int, max = nil)
def matches?(a : Enumerable(Int), b : Int, max = nil)
a.dup.each do |aa|
return true if matches? aa, b, max
end
Expand All @@ -168,7 +171,7 @@ class VirtualTime

# # ###### Possibly enable
# # :ditto:
# def self.matches?(a : Array(Int), b : Array(Int), max = nil)
# def matches?(a : Array(Int), b : Array(Int), max = nil)
# a.each do |aa|
# b.each do |bb|
# return true if matches? aa, bb, max
Expand All @@ -178,7 +181,7 @@ class VirtualTime
# end

# # :ditto:
# def self.matches?(a : Range(Int, Int), b : Range(Int, Int), max = nil)
# def matches?(a : Range(Int, Int), b : Range(Int, Int), max = nil)
# if max
# if (a.begin < 0 || a.end < 0)
# ab = a.begin < 0 ? max + a.begin : a.begin
Expand All @@ -200,7 +203,7 @@ class VirtualTime
# end

# # :ditto:
# def self.matches?(a : Steppable::StepIterator(Int, Int, Int), b : Steppable::StepIterator(Int, Int, Int), max = nil)
# def matches?(a : Steppable::StepIterator(Int, Int, Int), b : Steppable::StepIterator(Int, Int, Int), max = nil)
# if max
# if a.current < 0 || a.limit < 0
# ab = a.current < 0 ? max + a.current : a.current
Expand Down Expand Up @@ -228,7 +231,7 @@ class VirtualTime
# # ###### Possibly enable

# :ditto:
def self.matches?(a : Enumerable(Int), b : Enumerable(Int), max = nil)
def matches?(a : Enumerable(Int), b : Enumerable(Int), max = nil)
a.dup.each do |aa|
b.dup.each do |bb|
return true if matches? aa, bb, max
Expand All @@ -238,7 +241,7 @@ class VirtualTime
end

# :ditto:
def self.matches?(a : Enumerable(Int), b : VirtualProc, max = nil)
def matches?(a : Enumerable(Int), b : VirtualProc, max = nil)
a.dup.each do |aa|
aa = max + aa if max && (aa < 0)
return true if b.call aa
Expand All @@ -247,17 +250,17 @@ class VirtualTime
end

# :ditto:
def self.matches?(a : VirtualProc, b : Int, max = nil)
def matches?(a : VirtualProc, b : Int, max = nil)
b = max + b if max && (b < 0)
a.call b
end

# :ditto:
def self.matches?(a : VirtualProc, b : VirtualProc, max = nil)
def matches?(a : VirtualProc, b : VirtualProc, max = nil)
raise ArgumentError.new "Proc to Proc comparison not supported (yet?)"
end

def self.matches?(a, b, max = nil)
def matches?(a, b, max = nil)
matches? b, a, max
end

Expand Down

0 comments on commit 8dcf1c0

Please sign in to comment.