Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ def now_with_mock_time

alias_method :new_without_mock_time, :new

def new_with_mock_time(*args)
args.size <= 0 ? now : new_without_mock_time(*args)
def new_with_mock_time(*args, **kwargs)
if args.empty? && kwargs.empty?
now
elsif kwargs.any?
new_without_mock_time(*args, **kwargs)
else
new_without_mock_time(*args)
end
end

ruby2_keywords :new_with_mock_time if Module.private_method_defined?(:ruby2_keywords)

alias_method :new, :new_with_mock_time
end
end
Expand Down
26 changes: 26 additions & 0 deletions test/timecop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,32 @@ def test_mock_time_new_same_as_now
assert_equal date, Time.new
end

def test_time_new_with_keyword_arguments
skip "Time.new with in: keyword requires Ruby 3.1+" if RUBY_VERSION < "3.1"
Timecop.freeze(2011, 1, 2) do
t = Time.new(2020, 1, 1, 0, 0, 0, in: "+05:00")
assert_equal 2020, t.year
assert_equal 18000, t.utc_offset
end
end

def test_time_new_with_only_keyword_arguments
skip "Time.new with in: keyword requires Ruby 3.1+" if RUBY_VERSION < "3.1"
Timecop.freeze(2011, 1, 2) do
t = Time.new(in: "+05:00")
assert_equal 18000, t.utc_offset
end
end

def test_time_new_with_positional_args_still_works
Timecop.freeze(2011, 1, 2) do
t = Time.new(2020, 6, 15, 12, 30, 0)
assert_equal 2020, t.year
assert_equal 6, t.month
assert_equal 15, t.day
end
end

def test_not_callable_send_travel
assert_raises NoMethodError do
Timecop.send_travel(:travel, Time.now - 100)
Expand Down