timecop 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ class Timecop
4
4
  # movements on a simple stack.
5
5
  class TimeStackItem #:nodoc:
6
6
  attr_reader :mock_type
7
-
7
+
8
8
  def initialize(mock_type, *args)
9
9
  raise "Unknown mock_type #{mock_type}" unless [:freeze, :travel, :scale].include?(mock_type)
10
10
  @scaling_factor = args.shift if mock_type == :scale
@@ -14,35 +14,35 @@ class Timecop
14
14
  @travel_offset = compute_travel_offset
15
15
  @dst_adjustment = compute_dst_adjustment(@time)
16
16
  end
17
-
17
+
18
18
  def year
19
19
  time.year
20
20
  end
21
-
21
+
22
22
  def month
23
23
  time.month
24
24
  end
25
-
25
+
26
26
  def day
27
27
  time.day
28
28
  end
29
-
29
+
30
30
  def hour
31
31
  time.hour
32
32
  end
33
-
33
+
34
34
  def min
35
35
  time.min
36
36
  end
37
-
37
+
38
38
  def sec
39
39
  time.sec
40
40
  end
41
-
41
+
42
42
  def utc_offset
43
43
  time.utc_offset
44
44
  end
45
-
45
+
46
46
  def travel_offset
47
47
  @travel_offset
48
48
  end
@@ -50,12 +50,12 @@ class Timecop
50
50
  def scaling_factor
51
51
  @scaling_factor
52
52
  end
53
-
53
+
54
54
  def time(time_klass = Time) #:nodoc:
55
55
  if travel_offset.nil?
56
- time_klass.at(@time.to_f)
56
+ time_klass.at(@time)
57
57
  elsif scaling_factor.nil?
58
- time_klass.at((Time.now_without_mock_time + travel_offset).to_f)
58
+ time_klass.at(Time.now_without_mock_time + travel_offset)
59
59
  else
60
60
  time_klass.at(scaled_time)
61
61
  end
@@ -64,11 +64,11 @@ class Timecop
64
64
  def scaled_time
65
65
  (@time + (Time.now_without_mock_time - @time_was) * scaling_factor).to_f
66
66
  end
67
-
67
+
68
68
  def date(date_klass = Date)
69
69
  date_klass.jd(time.__send__(:to_date).jd)
70
70
  end
71
-
71
+
72
72
  def datetime(datetime_klass = DateTime)
73
73
  our_offset = utc_offset + dst_adjustment
74
74
 
@@ -80,24 +80,24 @@ class Timecop
80
80
  datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
81
81
  end
82
82
  end
83
-
83
+
84
84
  def dst_adjustment
85
85
  @dst_adjustment
86
86
  end
87
-
87
+
88
88
  private
89
89
  def rational_to_utc_offset(rational)
90
90
  ((24.0 / rational.denominator) * rational.numerator) * (60 * 60)
91
91
  end
92
-
92
+
93
93
  def utc_offset_to_rational(utc_offset)
94
94
  Rational(utc_offset, 24 * 60 * 60)
95
95
  end
96
-
96
+
97
97
  def parse_time(*args)
98
98
  time_klass = Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
99
99
  arg = args.shift
100
- if arg.is_a?(Time)
100
+ if arg.is_a?(Time)
101
101
  if Timecop.active_support != false && arg.respond_to?(:in_time_zone)
102
102
  arg.in_time_zone
103
103
  else
@@ -128,13 +128,13 @@ class Timecop
128
128
  end
129
129
  end
130
130
  end
131
-
131
+
132
132
  def compute_dst_adjustment(time)
133
133
  return 0 if !(time.dst? ^ Time.now.dst?)
134
134
  return -1 * 60 * 60 if time.dst?
135
135
  return 60 * 60
136
136
  end
137
-
137
+
138
138
  def compute_travel_offset
139
139
  return nil if mock_type == :freeze
140
140
  time - Time.now_without_mock_time
@@ -1,3 +1,3 @@
1
1
  class Timecop
2
- VERSION = "0.5.5"
2
+ VERSION = "0.5.6"
3
3
  end
@@ -7,7 +7,7 @@ class TestTimeStackItem < Test::Unit::TestCase
7
7
  Timecop.active_support = nil
8
8
  Timecop.return
9
9
  end
10
-
10
+
11
11
  def test_new_with_time
12
12
  t = Time.now
13
13
  y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
@@ -33,7 +33,7 @@ class TestTimeStackItem < Test::Unit::TestCase
33
33
  assert_equal min, stack_item.min
34
34
  assert_equal s, stack_item.sec
35
35
  end
36
-
36
+
37
37
  def test_new_with_datetime_now
38
38
  t = DateTime.now
39
39
  y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.min, t.sec
@@ -46,7 +46,7 @@ class TestTimeStackItem < Test::Unit::TestCase
46
46
  assert_equal min, stack_item.min
47
47
  assert_equal s, stack_item.sec
48
48
  end
49
-
49
+
50
50
  def test_new_with_datetime_in_different_timezone
51
51
  each_timezone do
52
52
  t = DateTime.parse("2009-10-11 00:38:00 +0200")
@@ -55,7 +55,7 @@ class TestTimeStackItem < Test::Unit::TestCase
55
55
  assert_date_times_equal(t, stack_item.datetime)
56
56
  end
57
57
  end
58
-
58
+
59
59
  def test_new_with_date
60
60
  date = Date.today
61
61
  y, m, d, h, min, s = date.year, date.month, date.day, 0, 0, 0
@@ -68,7 +68,7 @@ class TestTimeStackItem < Test::Unit::TestCase
68
68
  assert_equal min, stack_item.min
69
69
  assert_equal s, stack_item.sec
70
70
  end
71
-
71
+
72
72
  # Due to the nature of this test (calling Time.now once in this test and
73
73
  # once in #new), this test may fail when two subsequent calls
74
74
  # to Time.now return a different second.
@@ -84,7 +84,7 @@ class TestTimeStackItem < Test::Unit::TestCase
84
84
  assert_equal min, stack_item.min
85
85
  assert_equal s, stack_item.sec
86
86
  end
87
-
87
+
88
88
  def test_new_with_individual_arguments
89
89
  y, m, d, h, min, s = 2008, 10, 10, 10, 10, 10
90
90
  stack_item = Timecop::TimeStackItem.new(:freeze, y, m, d, h, min, s)
@@ -96,14 +96,14 @@ class TestTimeStackItem < Test::Unit::TestCase
96
96
  assert_equal min, stack_item.min
97
97
  assert_equal s, stack_item.sec
98
98
  end
99
-
99
+
100
100
  def test_rational_to_utc_offset
101
101
  assert_equal -14400, a_time_stack_item.send(:rational_to_utc_offset, Rational(-1, 6))
102
102
  assert_equal -18000, a_time_stack_item.send(:rational_to_utc_offset, Rational(-5, 24))
103
103
  assert_equal 0, a_time_stack_item.send(:rational_to_utc_offset, Rational(0, 1))
104
104
  assert_equal 3600, a_time_stack_item.send(:rational_to_utc_offset, Rational(1, 24))
105
105
  end
106
-
106
+
107
107
  def test_utc_offset_to_rational
108
108
  assert_equal Rational(-1, 6), a_time_stack_item.send(:utc_offset_to_rational, -14400)
109
109
  assert_equal Rational(-5, 24), a_time_stack_item.send(:utc_offset_to_rational, -18000)
@@ -119,16 +119,16 @@ class TestTimeStackItem < Test::Unit::TestCase
119
119
 
120
120
  assert_equal 0, tsi.send(:dst_adjustment)
121
121
  end
122
-
122
+
123
123
  def test_compute_dst_adjustment_for_non_dst_to_non_dst
124
124
  Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0400"))
125
125
  t = DateTime.parse("2009-12-11 00:00:00 -0400")
126
126
  tsi = Timecop::TimeStackItem.new(:freeze, t)
127
127
  return if Time.now.dst? || tsi.time.dst?
128
128
 
129
- assert_equal 0, tsi.send(:dst_adjustment)
129
+ assert_equal 0, tsi.send(:dst_adjustment)
130
130
  end
131
-
131
+
132
132
  def test_compute_dst_adjustment_for_dst_to_non_dst
133
133
  Timecop.freeze(DateTime.parse("2009-10-1 00:38:00 -0400"))
134
134
  t = DateTime.parse("2009-12-11 00:00:00 -0400")
@@ -137,7 +137,7 @@ class TestTimeStackItem < Test::Unit::TestCase
137
137
 
138
138
  assert_equal 60 * 60, tsi.send(:dst_adjustment)
139
139
  end
140
-
140
+
141
141
  def test_compute_dst_adjustment_for_non_dst_to_dst
142
142
  Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0400"))
143
143
  t = DateTime.parse("2009-10-11 00:00:00 -0400")
@@ -146,7 +146,7 @@ class TestTimeStackItem < Test::Unit::TestCase
146
146
 
147
147
  assert_equal -1 * 60 * 60, tsi.send(:dst_adjustment)
148
148
  end
149
-
149
+
150
150
  # Ensure DateTimes handle changing DST properly
151
151
  def test_datetime_for_dst_to_non_dst
152
152
  Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0500"))
@@ -155,7 +155,7 @@ class TestTimeStackItem < Test::Unit::TestCase
155
155
 
156
156
  assert_date_times_equal t, tsi.datetime
157
157
  end
158
-
158
+
159
159
  def test_datetime_for_non_dst_to_dst
160
160
  Timecop.freeze(DateTime.parse("2009-10-11 00:00:00 -0400"))
161
161
  t = DateTime.parse("2009-11-30 23:38:00 -0500")
@@ -174,7 +174,7 @@ class TestTimeStackItem < Test::Unit::TestCase
174
174
 
175
175
  assert_times_effectively_equal expected_offset, tsi.send(:travel_offset), 1, "Offset not calculated correctly"
176
176
  end
177
-
177
+
178
178
  def test_set_travel_offset_for_freeze
179
179
  Timecop.freeze(2009, 10, 1, 0, 0, 0)
180
180
  t = Time.local(2009, 10, 1, 0, 0, 30)
@@ -228,4 +228,11 @@ class TestTimeStackItem < Test::Unit::TestCase
228
228
  Timecop.freeze(Date.new(2012, 6, 9))
229
229
  end
230
230
  end
231
+
232
+ def test_nsecs_are_set
233
+ time = Time.now
234
+ Timecop.freeze time
235
+ assert_equal time, Time.now
236
+ assert_equal time.nsec, Time.now.nsec
237
+ end
231
238
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timecop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-12-20 00:00:00.000000000 Z
13
+ date: 2012-12-26 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: A gem providing "time travel" and "time freezing" capabilities, making
16
16
  it dead simple to test time-dependent code. It provides a unified method to mock