timecop 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/timecop/time_extensions.rb +4 -2
- data/lib/timecop/time_stack_item.rb +9 -4
- data/test/timecop_test.rb +48 -2
- metadata +42 -27
data/VERSION.yml
CHANGED
@@ -19,10 +19,12 @@ class Time #:nodoc:
|
|
19
19
|
alias_method :now, :now_with_mock_time
|
20
20
|
|
21
21
|
alias_method :new_without_mock_time, :new
|
22
|
+
|
22
23
|
def new_with_mock_time *args
|
23
|
-
|
24
|
+
begin
|
25
|
+
raise ArgumentError.new if args.size <= 0
|
24
26
|
new_without_mock_time *args
|
25
|
-
|
27
|
+
rescue ArgumentError
|
26
28
|
now
|
27
29
|
end
|
28
30
|
end
|
@@ -54,15 +54,20 @@ class Timecop
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def date(date_klass=Date)
|
57
|
-
date_klass.jd(
|
57
|
+
date_klass.jd(time.__send__(:to_date).jd)
|
58
58
|
end
|
59
59
|
|
60
60
|
def datetime(datetime_klass=DateTime)
|
61
61
|
# DateTime doesn't know about DST, so let's remove its presence
|
62
62
|
our_offset = utc_offset + dst_adjustment
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
if Float.method_defined?(:to_r)
|
64
|
+
fractions_of_a_second = time.to_f % 1
|
65
|
+
datetime_klass.new(year, month, day, hour, min, sec + fractions_of_a_second,
|
66
|
+
utc_offset_to_rational(our_offset))
|
67
|
+
else
|
68
|
+
our_offset = utc_offset + dst_adjustment
|
69
|
+
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
|
70
|
+
end
|
66
71
|
end
|
67
72
|
|
68
73
|
def dst_adjustment
|
data/test/timecop_test.rb
CHANGED
@@ -56,8 +56,47 @@ class TestTimecop < Test::Unit::TestCase
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def test_travel_does_not_reduce_precision_of_datetime
|
59
|
-
|
60
|
-
|
59
|
+
# requires to_r on Float (>= 1.9)
|
60
|
+
if Float.method_defined?(:to_r)
|
61
|
+
Timecop.travel(1)
|
62
|
+
assert_not_equal DateTime.now, DateTime.now
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_freeze_in_time_subclass_returns_mocked_subclass
|
67
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
68
|
+
custom_timeklass = Class.new(Time) do
|
69
|
+
def custom_format_method() strftime('%F') end
|
70
|
+
end
|
71
|
+
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
72
|
+
assert custom_timeklass.now.is_a? custom_timeklass
|
73
|
+
assert Time.now.eql? custom_timeklass.now
|
74
|
+
assert custom_timeklass.now.respond_to? :custom_format_method
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_freeze_in_date_subclass_returns_mocked_subclass
|
79
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
80
|
+
custom_dateklass = Class.new(Date) do
|
81
|
+
def custom_format_method() strftime('%F') end
|
82
|
+
end
|
83
|
+
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
84
|
+
assert custom_dateklass.today.is_a? custom_dateklass
|
85
|
+
assert Date.today.eql? custom_dateklass.today
|
86
|
+
assert custom_dateklass.today.respond_to? :custom_format_method
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_freeze_in_datetime_subclass_returns_mocked_subclass
|
91
|
+
t = Time.local(2008, 10, 10, 10, 10, 10)
|
92
|
+
custom_datetimeklass = Class.new(DateTime) do
|
93
|
+
def custom_format_method() strftime('%F') end
|
94
|
+
end
|
95
|
+
Timecop.freeze(2008, 10, 10, 10, 10, 10) do
|
96
|
+
assert custom_datetimeklass.now.is_a? custom_datetimeklass
|
97
|
+
assert DateTime.now.eql? custom_datetimeklass.now
|
98
|
+
assert custom_datetimeklass.now.respond_to? :custom_format_method
|
99
|
+
end
|
61
100
|
end
|
62
101
|
|
63
102
|
def test_recursive_freeze
|
@@ -319,6 +358,13 @@ class TestTimecop < Test::Unit::TestCase
|
|
319
358
|
end
|
320
359
|
end
|
321
360
|
|
361
|
+
def test_freeze_with_new_date
|
362
|
+
date = Date.new(2012, 6, 9)
|
363
|
+
Timecop.freeze(Date.new(2012, 6, 9)) do
|
364
|
+
assert_equal date, Time.now.__send__(:to_date)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
|
322
368
|
def test_return_to_baseline_without_a_baseline_set_returns_to_current_time
|
323
369
|
time_before_travel = Time.now
|
324
370
|
Timecop.travel Time.now - 60
|
metadata
CHANGED
@@ -1,27 +1,35 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: timecop
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
- 6
|
10
|
+
version: 0.4.6
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Travis Jeffery
|
9
14
|
- John Trupiano
|
10
15
|
autorequire:
|
11
16
|
bindir: bin
|
12
17
|
cert_chain: []
|
13
|
-
|
18
|
+
|
19
|
+
date: 2012-09-05 00:00:00 -05:00
|
20
|
+
default_executable:
|
14
21
|
dependencies: []
|
15
|
-
|
16
|
-
|
17
|
-
Time.now, Date.today, and DateTime.now in a single call.
|
22
|
+
|
23
|
+
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 Time.now, Date.today, and DateTime.now in a single call.
|
18
24
|
email: travisjeffery@gmail.com
|
19
25
|
executables: []
|
26
|
+
|
20
27
|
extensions: []
|
21
|
-
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
22
30
|
- LICENSE
|
23
31
|
- README.markdown
|
24
|
-
files:
|
32
|
+
files:
|
25
33
|
- History.rdoc
|
26
34
|
- LICENSE
|
27
35
|
- README.markdown
|
@@ -37,34 +45,41 @@ files:
|
|
37
45
|
- test/timecop_test.rb
|
38
46
|
- test/timecop_without_date_test.rb
|
39
47
|
- test/timecop_without_date_but_with_time_test.rb
|
48
|
+
has_rdoc: true
|
40
49
|
homepage: http://github.com/jtrupiano/timecop
|
41
50
|
licenses: []
|
51
|
+
|
42
52
|
post_install_message:
|
43
|
-
rdoc_options:
|
53
|
+
rdoc_options:
|
44
54
|
- --charset=UTF-8
|
45
|
-
require_paths:
|
55
|
+
require_paths:
|
46
56
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
58
|
none: false
|
49
|
-
requirements:
|
50
|
-
- -
|
51
|
-
- !ruby/object:Gem::Version
|
52
|
-
|
53
|
-
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
67
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
59
75
|
requirements: []
|
76
|
+
|
60
77
|
rubyforge_project: johntrupiano
|
61
|
-
rubygems_version: 1.
|
78
|
+
rubygems_version: 1.6.2
|
62
79
|
signing_key:
|
63
80
|
specification_version: 3
|
64
|
-
summary: A gem providing "time travel" and "time freezing" capabilities, making it
|
65
|
-
|
66
|
-
Date.today, and DateTime.now in a single call.
|
67
|
-
test_files:
|
81
|
+
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, Date.today, and DateTime.now in a single call.
|
82
|
+
test_files:
|
68
83
|
- test/test_helper.rb
|
69
84
|
- test/time_stack_item_test.rb
|
70
85
|
- test/timecop_test.rb
|