File: spec_helper.rb

package info (click to toggle)
ruby-fugit 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 300 kB
  • sloc: ruby: 3,363; makefile: 40
file content (115 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#
# Specifying fugit
#
# Sun Jan  1 12:09:21 JST 2017  Ishinomaki
#

require 'pp'
require 'ostruct'

require 'chronic'
::Khronic = ::Chronic
Object.send(:remove_const, :Chronic)

require 'fugit'


module Helpers

  def jruby?; !! RUBY_PLATFORM.match(/java/); end
  def windows?; Gem.win_platform?; end


  def in_zone(zone_name, &block)

    prev_tz = ENV['TZ']
    ENV['TZ'] = zone_name if zone_name

    block.call

  ensure

    ENV['TZ'] = prev_tz
  end

  def in_active_support_zone(zone_name, &block)

    prev_tz = ENV['TZ']
    ENV['TZ'] = nil # else it takes over

    Time._zone = zone_name
    Time.module_eval do
      class << self
        def zone; @zone; end
      end
    end

    block.call

  ensure

    Time._zone = nil
    Time.module_eval do
      class << self
        undef_method :zone
      end
    end

    ENV['TZ'] = prev_tz
  end

  def require_chronic

    Object.const_set(:Chronic, Khronic)
  end

  def unrequire_chronic

    Object.send(:remove_const, :Chronic)
  end
end # Helpers

RSpec.configure do |c|

  c.alias_example_to(:they)
  c.alias_example_to(:so)
  c.include(Helpers)
end


  # A _bad_inc that doesn't progress, to test #next_time and
  # #previous_time loop breakers...
  #
class Fugit::Cron::TimeCursor
  def _bad_inc(i)
    @t = @t + 0
    self
  end
  alias _original_inc inc
end


  # Simulating ActiveSupport Time.zone
  #
class Time

  class << self

    # .zone itself is defined/undefined in the #in_active_support_zone
    # spec helper defined above

    attr_reader :_zone

    def _zone=(name)

      @zone =
        if name
          OpenStruct.new(tzinfo: ::TZInfo::Timezone.get(name))
        else
          nil
        end
    end
  end
end