hyper-spec 0.1.2 → 0.99.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,190 +0,0 @@
1
- if RUBY_ENGINE == 'opal'
2
- require 'lolex'
3
- require 'time'
4
-
5
- # Wrap the Lolex js package
6
- class Lolex
7
- class << self
8
- def stack
9
- @stack ||= []
10
- end
11
-
12
- def push(time, scale = 1, resolution = 10)
13
- puts "Lolex.push(#{time}, #{scale}, #{resolution})"
14
- time = Time.parse(time) if time.is_a? String
15
- stack << [Time.now, @scale, @resolution]
16
- update_lolex(time, scale, resolution)
17
- end
18
-
19
- def pop
20
- update_lolex(*stack.pop) unless stack.empty?
21
- end
22
-
23
- def unmock(time, resolution)
24
- push(time, 1, resolution)
25
- @backup_stack = stack
26
- @stack = []
27
- end
28
-
29
- def restore
30
- @stack = @backup_stack
31
- pop
32
- end
33
-
34
- def tick
35
- real_clock = `(new #{@lolex}['_Date']).getTime()`
36
- mock_clock = Time.now.to_f * 1000
37
- real_elapsed_time = real_clock - @real_start_time
38
- mock_elapsed_time = mock_clock - @mock_start_time
39
-
40
- ticks = real_elapsed_time * @scale - mock_elapsed_time
41
-
42
- `#{@lolex}.tick(#{ticks.to_i})`
43
- nil
44
- end
45
-
46
- def create_ticker
47
- return unless @scale && @scale > 0
48
- ticker = %x{
49
- #{@lolex}['_setInterval'].call(
50
- window,
51
- function() { #{tick} },
52
- #{@resolution}
53
- )
54
- }
55
- ticker
56
- end
57
-
58
- def update_lolex(time, scale, resolution)
59
- `#{@lolex}.uninstall()` && return if scale.nil?
60
- @mock_start_time = time.to_f * 1000
61
-
62
- if @lolex
63
- `#{@lolex}['_clearInterval'].call(window, #{@ticker})` if @ticker
64
- @real_start_time = `(new #{@lolex}['_Date']).getTime()`
65
- `#{@lolex}.tick(#{@mock_start_time - Time.now.to_f * 1000})`
66
- else
67
- @real_start_time = Time.now.to_f * 1000
68
- @lolex = `lolex.install(window, #{@mock_start_time})`
69
- end
70
-
71
- @scale = scale
72
- @resolution = resolution
73
- @ticker = create_ticker
74
- nil # must return nil otherwise we try to return a timer to server!
75
- end
76
- end
77
- end
78
-
79
- else
80
- require 'timecop'
81
-
82
- # Interface to the Lolex package running on the client side
83
- # Below we will monkey patch Timecop to call these methods
84
- class Lolex
85
- class << self
86
- def init(page, client_time_zone, resolution)
87
- @capybara_page = page
88
- @resolution = resolution || 10
89
- @client_time_zone = client_time_zone
90
- run_pending_evaluations
91
- @initialized = true
92
- end
93
-
94
- def initialized?
95
- @initialized
96
- end
97
-
98
- def push(mock_type, *args)
99
- scale = if mock_type == :freeze
100
- 0
101
- elsif mock_type == :scale
102
- args[0]
103
- else
104
- 1
105
- end
106
- evaluate_ruby do
107
- "Lolex.push('#{time_string_in_zone}', #{scale}, #{@resolution})"
108
- end
109
- end
110
-
111
- def pop
112
- evaluate_ruby { 'Lolex.pop' }
113
- end
114
-
115
- def unmock
116
- evaluate_ruby { "Lolex.unmock('#{time_string_in_zone}', #{@resolution})" }
117
- end
118
-
119
- def restore
120
- evaluate_ruby { 'Lolex.restore' }
121
- end
122
-
123
- private
124
-
125
- def time_string_in_zone
126
- Time.now.in_time_zone(@client_time_zone).strftime('%Y/%m/%d %H:%M:%S %z')
127
- end
128
-
129
- def pending_evaluations
130
- @pending_evaluations ||= []
131
- end
132
-
133
- def evaluate_ruby(&block)
134
- if @capybara_page
135
- @capybara_page.evaluate_ruby(yield)
136
- else
137
- pending_evaluations << block
138
- end
139
- end
140
-
141
- def run_pending_evaluations
142
- return if pending_evaluations.empty?
143
- @capybara_page.evaluate_ruby(pending_evaluations.collect(&:call).join("\n"))
144
- @pending_evaluations ||= []
145
- end
146
- end
147
- end
148
-
149
- # Monkey patches to call our Lolex interface
150
- class Timecop
151
- private
152
-
153
- def travel(mock_type, *args, &block)
154
- raise SafeModeException if Timecop.safe_mode? && !block_given?
155
-
156
- stack_item = TimeStackItem.new(mock_type, *args)
157
-
158
- stack_backup = @_stack.dup
159
- @_stack << stack_item
160
-
161
- Lolex.push(mock_type, *args)
162
-
163
- if block_given?
164
- begin
165
- yield stack_item.time
166
- ensure
167
- Lolex.pop
168
- @_stack.replace stack_backup
169
- end
170
- end
171
- end
172
-
173
- def return(&block)
174
- current_stack = @_stack
175
- current_baseline = @baseline
176
- unmock!
177
- yield
178
- ensure
179
- Lolex.restore
180
- @_stack = current_stack
181
- @baseline = current_baseline
182
- end
183
-
184
- def unmock! #:nodoc:
185
- @baseline = nil
186
- @_stack = []
187
- Lolex.unmock
188
- end
189
- end
190
- end