Skip to content

Commit

Permalink
issue guard#510: move if/else constant testing outside now method
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Dec 31, 2020
1 parent 519c056 commit 20f0585
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/listen/monotonic_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
module Listen
module MonotonicTime
class << self
def now
if defined?(Process::CLOCK_MONOTONIC)
if defined?(Process::CLOCK_MONOTONIC)

def now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
elsif defined?(Process::CLOCK_MONOTONIC_RAW)
end

elsif defined?(Process::CLOCK_MONOTONIC_RAW)

def now
Process.clock_gettime(Process::CLOCK_MONOTONIC_RAW)
else
end

else

def now
Time.now.to_f
end

end
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/listen/monotonic_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
require 'listen/monotonic_time'

RSpec.describe Listen::MonotonicTime do
after(:all) do
# load once more with constants unstubbed/unhidden
load './lib/listen/monotonic_time.rb'
end

context 'module methods' do
describe '.now' do
subject { described_class.now }
Expand All @@ -11,6 +16,7 @@
context 'when CLOCK_MONOTONIC defined' do
before do
stub_const('Process::CLOCK_MONOTONIC', 10)
load './lib/listen/monotonic_time.rb'
end

it 'returns the CLOCK_MONOTONIC tick count' do
Expand All @@ -23,6 +29,7 @@
before do
hide_const('Process::CLOCK_MONOTONIC')
stub_const('Process::CLOCK_MONOTONIC_RAW', 11)
load './lib/listen/monotonic_time.rb'
end

it 'returns the floating point Time.now' do
Expand All @@ -37,6 +44,7 @@
before do
hide_const('Process::CLOCK_MONOTONIC')
hide_const('Process::CLOCK_MONOTONIC_RAW')
load './lib/listen/monotonic_time.rb'
end

it 'returns the floating point Time.now' do
Expand Down

0 comments on commit 20f0585

Please sign in to comment.