Projects
openEuler:Mainline
rubygem-timecop
Sign Up
Log In
Username
Password
We truncated the diff of some files because they were too big. If you want to see the full diff for every file,
click here
.
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
Expand all
Collapse all
Changes of Revision 2
View file
_service:tar_scm:rubygem-timecop.spec
Changed
@@ -1,12 +1,11 @@ %global gem_name timecop Summary: Provides a unified method to mock Time.now, Date.today in a single call Name: rubygem-%{gem_name} -Version: 0.7.1 +Version: 0.9.5 Release: 1 License: MIT URL: https://github.com/travisjeffery/timecop Source0: http://rubygems.org/downloads/timecop-%{version}.gem -Patch0: rubygem-timecop-0.7.1-Use-minitest.patch BuildRequires: rubygems-devel rubygem(activesupport) rubygem(minitest) rubygem(mocha) BuildArch: noarch %description @@ -21,30 +20,36 @@ Documentation for %{name}. %prep -%setup -q -c -T -%gem_install -n %{SOURCE0} -pushd .%{gem_instdir} -%patch0 -p1 -popd +%setup -q -n %{gem_name}-%{version} %build +gem build ../%{gem_name}-%{version}.gemspec +%gem_install %install rm -rf %{buildroot} mkdir -p %{buildroot}%{gem_dir} cp -va ./%{gem_dir}/* %{buildroot}%{gem_dir} -chmod a+x %{buildroot}%{gem_instdir}/test/run_tests.sh %check -pushd .%{gem_instdir}/test -sed -i '/bundler\/setup/ s/^/#/' test_helper.rb -./run_tests.sh +pushd .%{gem_instdir}/ +# Drop Bundler and minitest-rg dependency. +sed -i \ + -e '/require..bundler\/setup./ s/^/#/' \ + -e '/require..minitest\/rg./ s/^/#/' \ + -e '/require .pry./ s/^/#/g' \ + test/test_helper.rb + +# The test cases must be executed independently. +for i in test/*_test.rb; do + ruby -I.:lib ${i} +done popd %files %dir %{gem_instdir} +%license %{gem_instdir}/LICENSE %{gem_libdir} -%doc %{gem_instdir}/LICENSE %doc %{gem_instdir}/README.markdown %exclude %{gem_cache} %{gem_spec} @@ -55,5 +60,8 @@ %{gem_docdir} %changelog +* Wed Jul 20 2022 wangkai <wangkai385@h-partners.com> - 0.9.5-1 +- Update to 0.9.5 + * Fri Aug 7 2020 yanan li <liyanan032@huawei.com> - 0.7.1-1 - Package init
View file
_service:tar_scm:rubygem-timecop-0.7.1-Use-minitest.patch
Deleted
@@ -1,253 +0,0 @@ -From c30897f67ad90f0582c0ed0d7b78f46a7142f113 Mon Sep 17 00:00:00 2001 -From: Travis Jeffery <tj@travisjeffery.com> -Date: Sat, 10 May 2014 01:16:30 -0500 -Subject: PATCH Use minitest - ---- - test/test_helper.rb | 6 ++-- - test/time_stack_item_test.rb | 10 ++----- - test/timecop_test.rb | 40 +++++++++++-------------- - test/timecop_without_date_but_with_time_test.rb | 10 ++--- - test/timecop_without_date_test.rb | 8 ++--- - 5 files changed, 32 insertions(+), 42 deletions(-) - -diff --git a/test/test_helper.rb b/test/test_helper.rb -index 2d7852e..e2b059a 100644 ---- a/test/test_helper.rb -+++ b/test/test_helper.rb -@@ -1,14 +1,14 @@ - require 'rubygems' - require 'bundler/setup' --require 'test/unit' -+require 'minitest/autorun' -+ - begin - require 'mocha/setup' - rescue LoadError - require 'mocha' - end - --class Test::Unit::TestCase -- -+class MiniTest::Unit::TestCase - private - # Tests to see that two times are within the given distance, - # in seconds, from each other. -diff --git a/test/time_stack_item_test.rb b/test/time_stack_item_test.rb -index f654fa1..3658127 100644 ---- a/test/time_stack_item_test.rb -+++ b/test/time_stack_item_test.rb -@@ -4,7 +4,7 @@ - - require 'active_support/all' - --class TestTimeStackItem < Test::Unit::TestCase -+class TestTimeStackItem < MiniTest::Unit::TestCase - def teardown - Timecop.return - Time.zone = nil -@@ -204,16 +204,12 @@ def test_parse_only_string_with_active_support - end - - def test_parse_date -- assert_nothing_raised do -- Timecop.freeze(Date.new(2012, 6, 9)) -- end -+ Timecop.freeze(Date.new(2012, 6, 9)) - end - - def test_time_zone_returns_nil - Time.zone = nil -- assert_nothing_raised do -- Timecop.freeze -- end -+ Timecop.freeze - end - - def test_nsecs_are_set -diff --git a/test/timecop_test.rb b/test/timecop_test.rb -index e954329..e5f7d79 100644 ---- a/test/timecop_test.rb -+++ b/test/timecop_test.rb -@@ -2,7 +2,7 @@ - require File.join(File.dirname(__FILE__), "test_helper") - require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop') - --class TestTimecop < Test::Unit::TestCase -+class TestTimecop < MiniTest::Unit::TestCase - def teardown - Timecop.return - end -@@ -60,7 +60,7 @@ def test_travel_does_not_reduce_precision_of_datetime - # requires to_r on Float (>= 1.9) - if Float.method_defined?(:to_r) - Timecop.travel(1) -- assert_not_equal DateTime.now, DateTime.now -+ assert DateTime.now != DateTime.now - end - end - -@@ -113,7 +113,7 @@ def test_recursive_freeze - end - assert_equal t, Time.now - end -- assert_not_equal t, Time.now -+ assert t != Time.now - end - - def test_freeze_with_time_instance_works_as_expected -@@ -124,9 +124,9 @@ def test_freeze_with_time_instance_works_as_expected - assert_equal Date.new(2008, 10, 10), Date.today - end - -- assert_not_equal t, Time.now -- assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now -- assert_not_equal Date.new(2008, 10, 10), Date.today -+ assert t != Time.now -+ assert DateTime.new(2008, 10, 10, 10, 10, 10, local_offset) != DateTime.now -+ assert Date.new(2008, 10, 10) != Date.today - end - - def test_freeze_with_datetime_on_specific_timezone_during_dst -@@ -185,9 +185,9 @@ def test_freeze_with_date_instance_works_as_expected - assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now - assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now - end -- assert_not_equal d, Date.today -- assert_not_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now -- assert_not_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now -+ assert d != Date.today -+ assert Time.local(2008, 10, 10, 0, 0, 0) != Time.now -+ assert DateTime.new(2008, 10, 10, 0, 0, 0, local_offset) != DateTime.now - end - - def test_freeze_with_integer_instance_works_as_expected -@@ -202,9 +202,9 @@ def test_freeze_with_integer_instance_works_as_expected - assert_equal Date.new(2008, 10, 10), Date.today - end - end -- assert_not_equal t, Time.now -- assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10), DateTime.now -- assert_not_equal Date.new(2008, 10, 10), Date.today -+ assert t != Time.now -+ assert DateTime.new(2008, 10, 10, 10, 10, 10) != DateTime.now -+ assert Date.new(2008, 10, 10) != Date.today - end - - def test_exception_thrown_in_freeze_block_properly_resets_time -@@ -215,7 +215,7 @@ def test_exception_thrown_in_freeze_block_properly_resets_time - raise "blah exception" - end - rescue -- assert_not_equal t, Time.now -+ assert t != Time.now - assert_nil Time.send(:mock_time) - end - end -@@ -252,7 +252,7 @@ def test_mocked_date_time_now_is_local - if ENV'TZ' == 'UTC' - assert_equal(local_offset, 0, "Local offset not be zero for #{ENV'TZ'}") - else -- assert_not_equal(local_offset, 0, "Local offset should not be zero for #{ENV'TZ'}") -+ assert(local_offset, 0 != "Local offset should not be zero for #{ENV'TZ'}") - end - assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV'TZ'}" - end -@@ -442,7 +442,7 @@ def test_mock_time_new_same_as_now - end - - def test_not_callable_send_travel -- assert_raise NoMethodError do -+ assert_raises NoMethodError do - Timecop.send_travel(:travel, Time.now - 100) - end - end -@@ -466,7 +466,7 @@ def test_datetime_to_time_for_dst_to_non_dst - - def test_raises_when_safe_mode_and_no_block - with_safe_mode do -- assert_raise Timecop::SafeModeException do -+ assert_raises Timecop::SafeModeException do - Timecop.freeze - end - end -@@ -474,17 +474,13 @@ def test_raises_when_safe_mode_and_no_block - - def test_no_raise_when_safe_mode_and_block_used - with_safe_mode do -- assert_nothing_raised do -- Timecop.freeze {} -- end -+ Timecop.freeze {} - end - end - - def test_no_raise_when_not_safe_mode_and_no_block - with_safe_mode(false) do -- assert_nothing_raised do -- Timecop.freeze -- end -+ Timecop.freeze - end - end - -diff --git a/test/timecop_without_date_but_with_time_test.rb b/test/timecop_without_date_but_with_time_test.rb -index 9531afd..5f47a31 100644 ---- a/test/timecop_without_date_but_with_time_test.rb -+++ b/test/timecop_without_date_but_with_time_test.rb -@@ -1,12 +1,10 @@ --require "test/unit"
View file
_service
Changed
@@ -2,7 +2,7 @@ <service name="tar_scm"> <param name="scm">git</param> <param name="url">git@gitee.com:src-openeuler/rubygem-timecop.git</param> - <param name="revision">1d9444d3f84cfea9cdc8b5f68e39cb469dd0e7f2</param> + <param name="revision">deac7dbf2e708f89e2c8424279d224b376b1ebf3</param> <param name="exclude">*</param> <param name="extract">*</param> </service>
View file
_service:tar_scm:timecop-0.7.1.gem/data/test/run_tests.sh
Deleted
@@ -1,10 +0,0 @@ -#!/bin/sh - -FAILEDCASES=0 -for f in *_test.rb; do - if ! ${RUBY:-ruby} -I../lib:. $f; then - FAILEDCASES=`expr "$FAILEDCASES" + 1` - fi -done -echo "$FAILEDCASES test cases had failures" -exit $FAILEDCASES
View file
_service:tar_scm:timecop-0.9.5.gem/checksums.yaml.gz
Added
@@ -0,0 +1,7 @@ +--- +SHA256: + metadata.gz: 681f52d846b808b06ff833f3fbfc21e3c7c4c1bf426baa03f101c0a2c3b79ce8 + data.tar.gz: b95b6d4a3c357e7f11f046d82dad3badd81d4903c96ef2b8e77762f241eeb66e +SHA512: + metadata.gz: e62e48939fb924a78d59199222688ae7692a2f6649c16daac296375ac1e537fa1c85e6a34d1a5cbd64b209e643ffa08d180ce3d933905f91284ce4c819873c1e + data.tar.gz: f6f5ef32ee1bfadfd43a5a8899bb521873d61eafd2f242942df6e80a3ed11f80a170d074ada23a61eb3b92009eeaf3a1046b126c802dc9a1c014c77d32acb3ab
View file
_service:tar_scm:timecop-0.7.1.gem/data/LICENSE -> _service:tar_scm:timecop-0.9.5.gem/data/LICENSE
Changed
@@ -1,6 +1,6 @@ (The MIT License) -Copyright (c) 2012 — Travis Jeffery, John Trupiano +Copyright (c) 2019 — Travis Jeffery, John Trupiano Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the
View file
_service:tar_scm:timecop-0.7.1.gem/data/README.markdown -> _service:tar_scm:timecop-0.9.5.gem/data/README.markdown
Changed
@@ -1,9 +1,7 @@ # timecop -!Build Status(https://secure.travis-ci.org/travisjeffery/timecop.png)(http://travis-ci.org/travisjeffery/timecop) - -- Source(http://github.com/travisjeffery/timecop) -- Documentation(http://johntrupiano.rubyforge.org/timecop) +!Gem Version(https://badge.fury.io/rb/timecop.svg)(https://rubygems.org/gems/timecop) +!Build Status(https://github.com/travisjeffery/timecop/workflows/CI/badge.svg)(https://github.com/travisjeffery/timecop/actions?query=workflow%3ACI) ## DESCRIPTION @@ -54,7 +52,8 @@ Timecop.return end - it "should do blah blah blah" {} + it "should do blah blah blah" do + end end ``` @@ -106,11 +105,13 @@ Timecop.scale(3600) Time.now # => 2012-09-20 21:23:25 -0500 -# seconds later, hours have past it's gone from 9pm at night to 6am in the morning +# seconds later, hours have passed and it's gone from 9pm at night to 6am in the morning Time.now # => 2012-09-21 06:22:59 -0500 ``` +See #42(https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs. + ### Timecop.safe_mode Safe mode forces you to use Timecop with the block syntax since it always puts time back the way it was. If you are running in safe mode and use Timecop without the block syntax `Timecop::SafeModeException` will be raised to tell the user they are not being safe. @@ -128,7 +129,11 @@ # => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed. ``` -See #42(https://github.com/travisjeffery/timecop/pull/42) for more information, thanks to Ken Mayer, David Holcomb, and Pivotal Labs. +### Rails v Ruby Date/Time libraries + +Sometimes Rails Date/Time methods don't play nicely with Ruby Date/Time methods.(https://rails.lighthouseapp.com/projects/8994/tickets/6410-dateyesterday-datetoday) + +Be careful mixing Ruby `Date.today` with Rails `Date.tomorrow` / `Date.yesterday` as things might break. ## Contribute
View file
_service:tar_scm:timecop-0.7.1.gem/data/Rakefile -> _service:tar_scm:timecop-0.9.5.gem/data/Rakefile
Changed
@@ -3,8 +3,6 @@ require 'rake/testtask' require 'rdoc/task' -$LOAD_PATH.unshift("lib") - Rake::RDocTask.new do |rdoc| if File.exist?('VERSION') version = File.read('VERSION') @@ -21,7 +19,15 @@ end task :test do - system "cd test && ./run_tests.sh" or fail + failed = Dir"test/*_test.rb".map do |test| + command = "ruby #{test}" + puts + puts command + command unless system(command) + end.compact + if failed.any? + abort "#{failed.count} Tests failed\n#{failed.join("\n")}" + end end desc 'Default: run tests'
View file
_service:tar_scm:timecop-0.7.1.gem/data/lib/timecop/time_extensions.rb -> _service:tar_scm:timecop-0.9.5.gem/data/lib/timecop/time_extensions.rb
Changed
@@ -1,5 +1,5 @@ -require 'date' require 'time' +require 'date' class Time #:nodoc: class << self @@ -22,6 +22,8 @@ args.size <= 0 ? now : new_without_mock_time(*args) end + ruby2_keywords :new_with_mock_time if Module.private_method_defined?(:ruby2_keywords) + alias_method :new, :new_with_mock_time end end @@ -29,7 +31,6 @@ class Date #:nodoc: class << self def mock_date - mocked_time_stack_item = Timecop.top_stack_item mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.date(self) end @@ -46,31 +47,113 @@ def strptime_with_mock_date(str = '-4712-01-01', fmt = '%F', start = Date::ITALY) unless start == Date::ITALY raise ArgumentError, "Timecop's #{self}::#{__method__} only " + - "supports Date::ITALY for the start argument." + "supports Date::ITALY for the start argument." end - Time.strptime(str, fmt).to_date + #If date is not valid the following line raises + Date.strptime_without_mock_date(str, fmt) + + d = Date._strptime(str, fmt) + now = Time.now.to_date + year = d:year || now.year + mon = d:mon || now.mon + if d.keys == :year + Date.new(year) + elsif d:mday + Date.new(year, mon, d:mday) + elsif d:wday + Date.new(year, mon, now.mday) + (d:wday - now.wday) + elsif d:yday + Date.new(year).next_day(d:yday - 1) + elsif d:cwyear && d:cweek + if d:cwday + Date.commercial(d:cwyear, d:cweek, d:cwday) + else + Date.commercial(d:cwyear, d:cweek) + end + elsif d:seconds + Time.at(d:seconds).to_date + else + Date.new(year, mon) + end end alias_method :strptime, :strptime_with_mock_date + + def parse_with_mock_date(*args) + parsed_date = parse_without_mock_date(*args) + return parsed_date unless mocked_time_stack_item + date_hash = Date._parse(*args) + + case + when date_hash:year && date_hash:mon + parsed_date + when date_hash:mon && date_hash:mday + Date.new(mocked_time_stack_item.year, date_hash:mon, date_hash:mday) + when date_hash:mday + Date.new(mocked_time_stack_item.year, mocked_time_stack_item.month, date_hash:mday) + when date_hash:wday + closest_wday(date_hash:wday) + else + parsed_date + mocked_time_stack_item.travel_offset_days + end + end + + alias_method :parse_without_mock_date, :parse + alias_method :parse, :parse_with_mock_date + + def mocked_time_stack_item + Timecop.top_stack_item + end + + def closest_wday(wday) + today = Date.today + result = today - today.wday + result += 1 until wday == result.wday + result + end end end class DateTime #:nodoc: class << self def mock_time - mocked_time_stack_item = Timecop.top_stack_item mocked_time_stack_item.nil? ? nil : mocked_time_stack_item.datetime(self) end - def now_without_mock_time - Time.now_without_mock_time.to_datetime - end - def now_with_mock_time mock_time || now_without_mock_time end + alias_method :now_without_mock_time, :now + alias_method :now, :now_with_mock_time + + def parse_with_mock_date(*args) + date_hash = Date._parse(*args) + parsed_date = parse_without_mock_date(*args) + return parsed_date unless mocked_time_stack_item + date_hash = DateTime._parse(*args) + + case + when date_hash:year && date_hash:mon + parsed_date + when date_hash:mon && date_hash:mday + DateTime.new(mocked_time_stack_item.year, date_hash:mon, date_hash:mday) + when date_hash:mday + DateTime.new(mocked_time_stack_item.year, mocked_time_stack_item.month, date_hash:mday) + when date_hash:wday + Date.closest_wday(date_hash:wday).to_datetime + else + parsed_date + mocked_time_stack_item.travel_offset_days + end + end + + alias_method :parse_without_mock_date, :parse + alias_method :parse, :parse_with_mock_date + + def mocked_time_stack_item + Timecop.top_stack_item + end end end
View file
_service:tar_scm:timecop-0.7.1.gem/data/lib/timecop/time_stack_item.rb -> _service:tar_scm:timecop-0.9.5.gem/data/lib/timecop/time_stack_item.rb
Changed
@@ -1,140 +1,140 @@ class Timecop - # A data class for carrying around "time movement" objects. Makes it easy to keep track of the time - # movements on a simple stack. - class TimeStackItem #:nodoc: - attr_reader :mock_type - - def initialize(mock_type, *args) - raise "Unknown mock_type #{mock_type}" unless :freeze, :travel, :scale.include?(mock_type) - @scaling_factor = args.shift if mock_type == :scale - @mock_type = mock_type - @time = parse_time(*args) - @time_was = Time.now_without_mock_time - @travel_offset = compute_travel_offset - end + # A data class for carrying around "time movement" objects. Makes it easy to keep track of the time + # movements on a simple stack. + class TimeStackItem #:nodoc: + attr_reader :mock_type + + def initialize(mock_type, *args) + raise "Unknown mock_type #{mock_type}" unless :freeze, :travel, :scale.include?(mock_type) + @travel_offset = @scaling_factor = nil + @scaling_factor = args.shift if mock_type == :scale + @mock_type = mock_type + @time = parse_time(*args) + @time_was = Time.now_without_mock_time + @travel_offset = compute_travel_offset + end - def year - time.year - end + def year + time.year + end - def month - time.month - end + def month + time.month + end - def day - time.day - end + def day + time.day + end - def hour - time.hour - end + def hour + time.hour + end - def min - time.min - end + def min + time.min + end - def sec - time.sec - end + def sec + time.sec + end - def utc_offset - time.utc_offset - end + def utc_offset + time.utc_offset + end - def travel_offset - @travel_offset - end + def travel_offset + @travel_offset unless mock_type == :freeze + end - def scaling_factor - @scaling_factor - end + def travel_offset_days + (@travel_offset / 60 / 60 / 24).round + end - def time(time_klass = Time) #:nodoc: - if @time.respond_to?(:in_time_zone) - time = time_klass.at(@time.dup.utc.to_r) - else - time = time_klass.at(@time) - end + def scaling_factor + @scaling_factor + end - if travel_offset.nil? - time - elsif scaling_factor.nil? - time_klass.at((Time.now_without_mock_time + travel_offset).to_f) - else - time_klass.at(scaled_time) - end + def time(time_klass = Time) #:nodoc: + if @time.respond_to?(:in_time_zone) + time = time_klass.at(@time.dup.localtime) + else + time = time_klass.at(@time) end - def scaled_time - (@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f + if travel_offset.nil? + time + elsif scaling_factor.nil? + time_klass.at(Time.now_without_mock_time + travel_offset) + else + time_klass.at(scaled_time) end + end - def date(date_klass = Date) - date_klass.jd(time.__send__(:to_date).jd) - end + def scaled_time + (@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f + end - def datetime(datetime_klass = DateTime) - if Float.method_defined?(:to_r) - if !sec.zero? - fractions_of_a_second = time.to_f % 1 - datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset)) - else - datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset)) - end - else - datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset)) - end + def date(date_klass = Date) + date_klass.jd(time.__send__(:to_date).jd) + end + + def datetime(datetime_klass = DateTime) + if Float.method_defined?(:to_r) + fractions_of_a_second = time.to_f % 1 + datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset)) + else + datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset)) end + end - private + private - def rational_to_utc_offset(rational) - ((24.0 / rational.denominator) * rational.numerator) * (60 * 60) - end + def rational_to_utc_offset(rational) + ((24.0 / rational.denominator) * rational.numerator) * (60 * 60) + end - def utc_offset_to_rational(utc_offset) - Rational(utc_offset, 24 * 60 * 60) - end + def utc_offset_to_rational(utc_offset) + Rational(utc_offset, 24 * 60 * 60) + end - def parse_time(*args) - arg = args.shift - if arg.is_a?(Time) - arg - elsif Object.const_defined?(:DateTime) && arg.is_a?(DateTime) - time_klass.at(arg.to_time.to_f).getlocal - elsif Object.const_defined?(:Date) && arg.is_a?(Date) - time_klass.local(arg.year, arg.month, arg.day, 0, 0, 0) - elsif args.empty? && arg.kind_of?(Integer) - Time.now + arg - elsif arg.nil? - Time.now + def parse_time(*args) + arg = args.shift + if arg.is_a?(Time) + arg + elsif Object.const_defined?(:DateTime) && arg.is_a?(DateTime) + time_klass.at(arg.to_time.to_f).getlocal + elsif Object.const_defined?(:Date) && arg.is_a?(Date) + time_klass.local(arg.year, arg.month, arg.day, 0, 0, 0) + elsif args.empty? && (arg.kind_of?(Integer) || arg.kind_of?(Float)) + time_klass.now + arg + elsif arg.nil? + time_klass.now
View file
_service:tar_scm:timecop-0.7.1.gem/data/lib/timecop/timecop.rb -> _service:tar_scm:timecop-0.9.5.gem/data/lib/timecop/timecop.rb
Changed
@@ -1,5 +1,4 @@ require 'singleton' -require File.join(File.dirname(__FILE__), "time_extensions") require File.join(File.dirname(__FILE__), "time_stack_item") # Timecop @@ -14,6 +13,8 @@ include Singleton class << self + private :instance + # Allows you to run a block of code and "fake" a time throughout the execution of that block. # This is particularly useful for writing test methods where the passage of time is critical to the business # logic being tested. For example: @@ -31,6 +32,7 @@ # 3. Timecop.freeze(date_inst) # 4. Timecop.freeze(offset_in_seconds) # 5. Timecop.freeze(year, month, day, hour=0, minute=0, second=0) + # 6. Timecop.freeze() # Defaults to Time.now # # When a block is also passed, Time.now, DateTime.now and Date.today are all reset to their # previous values after the block has finished executing. This allows us to nest multiple @@ -74,11 +76,11 @@ end def baseline - instance.send(:baseline) + instance.baseline end def baseline=(baseline) - instance.send(:baseline=, baseline) + instance.baseline = baseline end # Reverts back to system's Time.now, Date.today and DateTime.now (if it exists) permamently when @@ -86,20 +88,21 @@ # the given block. def return(&block) if block_given? - instance.send(:return, &block) + instance.return(&block) else - instance.send(:unmock!) + instance.unmock! nil end end + alias :unfreeze :return def return_to_baseline - instance.send(:return_to_baseline) + instance.return_to_baseline Time.now end def top_stack_item #:nodoc: - instance.instance_variable_get(:@_stack).last + instance.stack.last end def safe_mode=(safe) @@ -107,61 +110,120 @@ end def safe_mode? - false || @safe_mode + @safe_mode ||= false + end + + def thread_safe=(t) + instance.thread_safe = t + end + + def thread_safe + instance.thread_safe + end + + # Returns whether or not Timecop is currently frozen/travelled + def frozen? + !instance.stack.empty? end private def send_travel(mock_type, *args, &block) - val = instance.send(:travel, mock_type, *args, &block) + val = instance.travel(mock_type, *args, &block) block_given? ? val : Time.now end end - private + def baseline=(b) + set_baseline(b) + stack << TimeStackItem.new(:travel, b) + end - def baseline=(baseline) - @baseline = baseline - @_stack << TimeStackItem.new(:travel, baseline) + def baseline + if @thread_safe + Thread.current:timecop_baseline + else + @baseline + end + end + + def set_baseline(b) + if @thread_safe + Thread.current:timecop_baseline = b + else + @baseline = b + end + end + + def stack + if @thread_safe + Thread.current:timecop_stack ||= + Thread.current:timecop_stack + else + @stack + end + end + + def set_stack(s) + if @thread_safe + Thread.current:timecop_stack = s + else + @stack = s + end end def initialize #:nodoc: - @_stack = + @stack = + @safe = nil + @thread_safe = false + end + + def thread_safe=(t) + initialize + @thread_safe = t + end + + def thread_safe + @thread_safe end def travel(mock_type, *args, &block) #:nodoc: - raise SafeModeException if Timecop.safe_mode? && !block_given? + raise SafeModeException if Timecop.safe_mode? && !block_given? && !@safe stack_item = TimeStackItem.new(mock_type, *args) - stack_backup = @_stack.dup - @_stack << stack_item + stack_backup = stack.dup + stack << stack_item if block_given? + safe_backup = @safe + @safe = true begin yield stack_item.time ensure - @_stack.replace stack_backup + stack.replace stack_backup + @safe = safe_backup end end end def return(&block) - current_stack = @_stack - current_baseline = @baseline + current_stack = stack + current_baseline = baseline unmock! yield - @_stack = current_stack - @baseline = current_baseline + ensure + set_stack current_stack + set_baseline current_baseline end def unmock! #:nodoc: - @baseline = nil - @_stack = + set_baseline nil + set_stack end def return_to_baseline - if @baseline - @_stack = @_stack.shift + if baseline + set_stack stack.shift
View file
_service:tar_scm:timecop-0.7.1.gem/data/lib/timecop/version.rb -> _service:tar_scm:timecop-0.9.5.gem/data/lib/timecop/version.rb
Changed
@@ -1,3 +1,3 @@ class Timecop - VERSION = "0.7.1" + VERSION = "0.9.5" end
View file
_service:tar_scm:timecop-0.7.1.gem/data/test/test_helper.rb -> _service:tar_scm:timecop-0.9.5.gem/data/test/test_helper.rb
Changed
@@ -1,40 +1,39 @@ -require 'rubygems' require 'bundler/setup' -require 'test/unit' -begin - require 'mocha/setup' -rescue LoadError - require 'mocha' -end +require 'minitest/autorun' +require 'minitest/rg' +require 'pry' + +$VERBOSE = true # enable ruby warnings -class Test::Unit::TestCase +require 'mocha/minitest' +class Minitest::Test private # Tests to see that two times are within the given distance, # in seconds, from each other. def times_effectively_equal(time1, time2, seconds_interval = 1) (time1 - time2).abs <= seconds_interval end - + def assert_times_effectively_equal(time1, time2, seconds_interval = 1, msg = nil) assert times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}" end - + def assert_times_effectively_not_equal(time1, time2, seconds_interval = 1, msg = nil) assert !times_effectively_equal(time1, time2, seconds_interval), "#{msg}: time1 = #{time1.to_s}, time2 = #{time2.to_s}" end - + # Gets the local offset (supplied by ENV'TZ' or your computer's clock) # At the given timestamp, or Time.now if not time is given. def local_offset(time = Time.now) Time.at(time.to_i).to_datetime.offset end - - TIMEZONES = "Europe/Paris", "UTC", "America/Chicago" - + + TIMEZONES = "Pacific/Midway", "Europe/Paris", "UTC", "America/Chicago" + def each_timezone old_tz = ENV"TZ" - + begin TIMEZONES.each do |timezone| ENV"TZ" = timezone @@ -44,13 +43,17 @@ ENV"TZ" = old_tz end end - + def a_time_stack_item Timecop::TimeStackItem.new(:freeze, 2008, 1, 1, 0, 0, 0) end - + def assert_date_times_equal(dt1, dt2) assert_in_delta dt1.to_time.to_f, dt2.to_time.to_f, 0.01, "Failed for timezone: #{ENV'TZ'}: #{dt1.to_s} not equal to #{dt2.to_s}" end - + + def jruby? + RUBY_PLATFORM == "java" + end + end
View file
_service:tar_scm:timecop-0.7.1.gem/data/test/time_stack_item_test.rb -> _service:tar_scm:timecop-0.9.5.gem/data/test/time_stack_item_test.rb
Changed
@@ -1,10 +1,10 @@ require 'date' -require File.join(File.dirname(__FILE__), "test_helper") -require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop') +require_relative "test_helper" +require 'timecop' require 'active_support/all' -class TestTimeStackItem < Test::Unit::TestCase +class TestTimeStackItem < Minitest::Test def teardown Timecop.return Time.zone = nil @@ -87,6 +87,19 @@ assert_equal s, stack_item.sec end + def test_new_with_float + t = Time.now + y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec + stack_item = Timecop::TimeStackItem.new(:freeze, 0.0) + + assert_equal y, stack_item.year + assert_equal m, stack_item.month + assert_equal d, stack_item.day + assert_equal h, stack_item.hour + assert_equal min, stack_item.min + assert_equal s, stack_item.sec + end + def test_new_with_individual_arguments y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10 stack_item = Timecop::TimeStackItem.new(:freeze, y, m, d, h, min, s) @@ -168,7 +181,7 @@ t = Time.local(2009, 10, 1, 0, 0, 30) tsi = Timecop::TimeStackItem.new(:freeze, t) - assert_equal nil, tsi.send(:travel_offset) + assert_nil tsi.send(:travel_offset) end def test_timezones @@ -179,12 +192,21 @@ end end + def test_timezones_with_parsed_string + Time.zone = "Europe/Zurich" + time_string = "2012-12-27 12:12" + expected_time = Time.zone.parse(time_string) + Timecop.freeze(time_string) do |frozen_time| + assert_equal expected_time, frozen_time + end + end + def test_timezones_apply_dates Time.zone = "Central Time (US & Canada)" time = Time.zone.local(2013,1,3) Timecop.freeze(time) do - assert_equal time.to_date, Time.now.to_date + assert_equal time.to_date, Time.zone.now.to_date end end @@ -204,16 +226,12 @@ end def test_parse_date - assert_nothing_raised do - Timecop.freeze(Date.new(2012, 6, 9)) - end + Timecop.freeze(Date.new(2012, 6, 9)) end def test_time_zone_returns_nil Time.zone = nil - assert_nothing_raised do - Timecop.freeze - end + Timecop.freeze end def test_nsecs_are_set @@ -273,7 +291,7 @@ def test_datetime_timezones dt = DateTime.new(2011,1,3,15,25,0,"-6") - Timecop.travel(dt) do + Timecop.freeze(dt) do now = DateTime.now assert_equal dt, now, "#{dt.to_f}, #{now.to_f}" end
View file
_service:tar_scm:timecop-0.7.1.gem/data/test/timecop_test.rb -> _service:tar_scm:timecop-0.9.5.gem/data/test/timecop_test.rb
Changed
@@ -1,8 +1,7 @@ -require 'date' -require File.join(File.dirname(__FILE__), "test_helper") -require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop') +require_relative "test_helper" +require 'timecop' -class TestTimecop < Test::Unit::TestCase +class TestTimecop < Minitest::Test def teardown Timecop.return end @@ -31,13 +30,19 @@ assert_equal frozen_time, Time.now end end - + def test_freeze_then_return_unsets_mock_time Timecop.freeze(1) Timecop.return assert_nil Time.send(:mock_time) end + def test_freeze_then_unfreeze_unsets_mock_time + Timecop.freeze(1) + Timecop.unfreeze + assert_nil Time.send(:mock_time) + end + def test_travel_then_return_unsets_mock_time Timecop.travel(1) Timecop.return @@ -59,8 +64,11 @@ def test_travel_does_not_reduce_precision_of_datetime # requires to_r on Float (>= 1.9) if Float.method_defined?(:to_r) - Timecop.travel(1) - assert_not_equal DateTime.now, DateTime.now + Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0)) + assert DateTime.now != DateTime.now + + Timecop.travel(Time.new(2014, 1, 1, 0, 0, 59)) + assert DateTime.now != DateTime.now end end @@ -113,7 +121,7 @@ end assert_equal t, Time.now end - assert_not_equal t, Time.now + assert t != Time.now end def test_freeze_with_time_instance_works_as_expected @@ -124,9 +132,9 @@ assert_equal Date.new(2008, 10, 10), Date.today end - assert_not_equal t, Time.now - assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10, local_offset), DateTime.now - assert_not_equal Date.new(2008, 10, 10), Date.today + assert t != Time.now + assert DateTime.new(2008, 10, 10, 10, 10, 10, local_offset) != DateTime.now + assert Date.new(2008, 10, 10) != Date.today end def test_freeze_with_datetime_on_specific_timezone_during_dst @@ -185,9 +193,9 @@ assert_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now assert_date_times_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now end - assert_not_equal d, Date.today - assert_not_equal Time.local(2008, 10, 10, 0, 0, 0), Time.now - assert_not_equal DateTime.new(2008, 10, 10, 0, 0, 0, local_offset), DateTime.now + assert d != Date.today + assert Time.local(2008, 10, 10, 0, 0, 0) != Time.now + assert DateTime.new(2008, 10, 10, 0, 0, 0, local_offset) != DateTime.now end def test_freeze_with_integer_instance_works_as_expected @@ -202,9 +210,9 @@ assert_equal Date.new(2008, 10, 10), Date.today end end - assert_not_equal t, Time.now - assert_not_equal DateTime.new(2008, 10, 10, 10, 10, 10), DateTime.now - assert_not_equal Date.new(2008, 10, 10), Date.today + assert t != Time.now + assert DateTime.new(2008, 10, 10, 10, 10, 10) != DateTime.now + assert Date.new(2008, 10, 10) != Date.today end def test_exception_thrown_in_freeze_block_properly_resets_time @@ -215,11 +223,19 @@ raise "blah exception" end rescue - assert_not_equal t, Time.now + assert t != Time.now assert_nil Time.send(:mock_time) end end + def test_exception_thrown_in_return_block_restores_previous_time + t = Time.local(2008, 10, 10, 10, 10, 10) + Timecop.freeze(t) do + Timecop.return { raise 'foobar' } rescue nil + assert_equal t, Time.now + end + end + def test_freeze_freezes_time t = Time.local(2008, 10, 10, 10, 10, 10) now = Time.now @@ -241,7 +257,7 @@ new_now = Time.now assert_times_effectively_equal(new_now, t, 1, "Looks like we failed to actually travel time") sleep(0.25) - assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving" + assert_times_effectively_not_equal new_now, Time.now, 0.24, "Looks like time is not moving" end end @@ -252,7 +268,7 @@ if ENV'TZ' == 'UTC' assert_equal(local_offset, 0, "Local offset not be zero for #{ENV'TZ'}") else - assert_not_equal(local_offset, 0, "Local offset should not be zero for #{ENV'TZ'}") + assert(local_offset, 0 != "Local offset should not be zero for #{ENV'TZ'}") end assert_equal local_offset, DateTime.now.offset, "Failed for timezone: #{ENV'TZ'}" end @@ -273,6 +289,17 @@ t = Time.local(2008, 10, 10, 10, 10, 10) assert_times_effectively_equal t, Timecop.scale(4, t) end + + def test_scaling_returns_now_if_nil_supplied + assert_times_effectively_equal Time.now, Timecop.scale(nil) + end + + def test_scaling_raises_when_empty_string_supplied + err = assert_raises(TypeError) do + Timecop.scale("") + end + assert_match /String can't be coerced into Float/, err.message + end def test_freeze_with_utc_time each_timezone do @@ -284,6 +311,19 @@ end end + def test_freeze_without_arguments_instance_works_as_expected + t = Time.local(2008, 10, 10, 10, 10, 10) + Timecop.freeze(t) do + assert_equal t, Time.now + Timecop.freeze do + assert_equal t, Time.now + assert_equal Time.local(2008, 10, 10, 10, 10, 10), Time.now + assert_equal Date.new(2008, 10, 10), Date.today + end + end + assert t != Time.now + end + def test_destructive_methods_on_frozen_time # Use any time zone other than UTC. ENV'TZ' = 'EST' @@ -314,13 +354,13 @@ end def test_recursive_travel_yields_correct_time - Timecop.travel(2008, 10, 10, 10, 10, 10) do + Timecop.travel(2008, 10, 10, 10, 10, 10) do Timecop.travel(2008, 9, 9, 9, 9, 9) do |inner_freeze| assert_times_effectively_equal inner_freeze, Time.now, 1, "Failed to yield current time back to block" end end end - + def test_recursive_travel_then_freeze t = Time.local(2008, 10, 10, 10, 10, 10) Timecop.travel(2008, 10, 10, 10, 10, 10) do @@ -365,6 +405,10 @@ end assert_times_effectively_equal(time_after_travel, Time.now) end + + def test_travel_returns_now_if_nil_supplied + assert_times_effectively_equal Time.now, Timecop.travel(nil) + end def test_travel_time_with_block_returns_the_value_of_the_block t_future = Time.local(2030, 10, 10, 10, 10, 10) @@ -373,6 +417,13 @@
View file
_service:tar_scm:timecop-0.7.1.gem/data/test/timecop_without_date_but_with_time_test.rb -> _service:tar_scm:timecop-0.9.5.gem/data/test/timecop_without_date_but_with_time_test.rb
Changed
@@ -1,12 +1,8 @@ -require "test/unit" - -class TestTimecopWithoutDateButWithTime < Test::Unit::TestCase - TIMECOP_LIB = File.join(File.dirname(__FILE__), '..', 'lib', 'timecop') +require_relative "test_helper" +require "time" +class TestTimecopWithoutDateButWithTime < Minitest::Test def test_loads_properly_when_time_is_required_instead_of_date - assert_nothing_raised do - require "time" - require TIMECOP_LIB - end + require 'timecop' end end
View file
_service:tar_scm:timecop-0.7.1.gem/data/test/timecop_without_date_test.rb -> _service:tar_scm:timecop-0.9.5.gem/data/test/timecop_without_date_test.rb
Changed
@@ -1,34 +1,33 @@ +require_relative "test_helper" +require 'timecop' -require File.join(File.dirname(__FILE__), "test_helper") -require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop') +class TestTimecopWithoutDate < Minitest::Test -class TestTimecopWithoutDate < Test::Unit::TestCase - def setup Object.send(:remove_const, :Date) if Object.const_defined?(:Date) Object.send(:remove_const, :DateTime) if Object.const_defined?(:DateTime) end - + # just in case...let's really make sure that Timecop is disabled between tests... def teardown Timecop.return end - + def test_freeze_changes_and_resets_time # depending on how we're invoked (individually or via the rake test suite) assert !Time.respond_to?(:zone) || Time.zone.nil? - + t = Time.local(2008, 10, 10, 10, 10, 10) - assert_not_equal t, Time.now + assert t != Time.now Timecop.freeze(2008, 10, 10, 10, 10, 10) do assert_equal t, Time.now end - assert_not_equal t, Time.now + assert t != Time.now end - + def test_recursive_freeze t = Time.local(2008, 10, 10, 10, 10, 10) - Timecop.freeze(2008, 10, 10, 10, 10, 10) do + Timecop.freeze(2008, 10, 10, 10, 10, 10) do assert_equal t, Time.now t2 = Time.local(2008, 9, 9, 9, 9, 9) Timecop.freeze(2008, 9, 9, 9, 9, 9) do @@ -38,7 +37,7 @@ end assert_nil Time.send(:mock_time) end - + def test_exception_thrown_in_freeze_block_properly_resets_time t = Time.local(2008, 10, 10, 10, 10, 10) begin @@ -47,11 +46,11 @@ raise "blah exception" end rescue - assert_not_equal t, Time.now + assert t != Time.now assert_nil Time.send(:mock_time) end end - + def test_freeze_freezes_time t = Time.local(2008, 10, 10, 10, 10, 10) now = Time.now @@ -63,7 +62,7 @@ assert_equal new_t, Time.now end end - + def test_travel_keeps_time_moving t = Time.local(2008, 10, 10, 10, 10, 10) now = Time.now @@ -71,13 +70,13 @@ new_now = Time.now assert_times_effectively_equal new_now, t, 1, "Looks like we failed to actually travel time" # 0.1 seconds sleep(0.25) - assert_times_effectively_not_equal new_now, Time.now, 0.25, "Looks like time is not moving" + assert_times_effectively_not_equal new_now, Time.now, 0.24, "Looks like time is not moving" end end - + def test_recursive_travel_maintains_each_context t = Time.local(2008, 10, 10, 10, 10, 10) - Timecop.travel(2008, 10, 10, 10, 10, 10) do + Timecop.travel(2008, 10, 10, 10, 10, 10) do assert((t - Time.now).abs < 50, "Failed to travel time.") t2 = Time.local(2008, 9, 9, 9, 9, 9) Timecop.travel(2008, 9, 9, 9, 9, 9) do @@ -88,10 +87,10 @@ end assert_nil Time.send(:mock_time) end - + def test_recursive_travel_then_freeze t = Time.local(2008, 10, 10, 10, 10, 10) - Timecop.travel(2008, 10, 10, 10, 10, 10) do + Timecop.travel(2008, 10, 10, 10, 10, 10) do assert((t - Time.now).abs < 50, "Failed to travel time.") t2 = Time.local(2008, 9, 9, 9, 9, 9) Timecop.freeze(2008, 9, 9, 9, 9, 9) do @@ -101,10 +100,10 @@ end assert_nil Time.send(:mock_time) end - + def test_recursive_freeze_then_travel t = Time.local(2008, 10, 10, 10, 10, 10) - Timecop.freeze(t) do + Timecop.freeze(t) do assert_equal t, Time.now t2 = Time.local(2008, 9, 9, 9, 9, 9) Timecop.travel(t2) do @@ -113,7 +112,7 @@ end assert_equal t, Time.now end - assert_nil Time.send(:mock_time) + assert_nil Time.send(:mock_time) end end
View file
_service:tar_scm:timecop-0.7.1.gem/metadata.gz -> _service:tar_scm:timecop-0.9.5.gem/metadata.gz
Changed
@@ -1,16 +1,15 @@ --- !ruby/object:Gem::Specification name: timecop version: !ruby/object:Gem::Version - version: 0.7.1 - prerelease: + version: 0.9.5 platform: ruby authors: - Travis Jeffery - John Trupiano -autorequire: +autorequire: bindir: bin cert_chain: -date: 2013-12-30 00:00:00.000000000 Z +date: 2022-03-07 00:00:00.000000000 Z dependencies: description: A gem providing "time travel" and "time freezing" capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock @@ -28,38 +27,35 @@ - lib/timecop.rb - lib/timecop/time_extensions.rb - lib/timecop/time_stack_item.rb -- lib/timecop/version.rb - lib/timecop/timecop.rb -- test/run_tests.sh +- lib/timecop/version.rb - test/test_helper.rb - test/time_stack_item_test.rb - test/timecop_test.rb -- test/timecop_without_date_test.rb - test/timecop_without_date_but_with_time_test.rb +- test/timecop_without_date_test.rb homepage: https://github.com/travisjeffery/timecop licenses: - MIT -post_install_message: +metadata: {} +post_install_message: rdoc_options: -- --charset=UTF-8 +- "--charset=UTF-8" require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - ">=" - !ruby/object:Gem::Version version: 1.9.2 required_rubygems_version: !ruby/object:Gem::Requirement - none: false requirements: - - - ! '>=' + - - ">=" - !ruby/object:Gem::Version version: '0' requirements: -rubyforge_project: timecop -rubygems_version: 1.8.23 -signing_key: +rubygems_version: 3.2.22 +signing_key: specification_version: 3 summary: A gem providing "time travel" and "time freezing" capabilities, making it dead simple to test time-dependent code. It provides a unified method to mock Time.now,
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.
浙ICP备2022010568号-2