aboutsummaryrefslogtreecommitdiffstats
path: root/spec/mspec/lib/mspec/guards/version.rb
blob: cb08fdac73b6d964db958ca3fb454ddc4933ca08 (plain)
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
require 'mspec/utils/deprecate'
require 'mspec/utils/version'
require 'mspec/guards/guard'

class VersionGuard < SpecGuard
  FULL_RUBY_VERSION = SpecVersion.new SpecGuard.ruby_version(:full)

  def initialize(version)
    case version
    when String
      @version = SpecVersion.new version
    when Range
      MSpec.deprecate "an empty version range end", 'a specific version' if version.end.empty?
      a = SpecVersion.new version.begin
      b = SpecVersion.new version.end
      unless version.exclude_end?
        MSpec.deprecate "ruby_version_is with an inclusive range", 'an exclusive range ("2.1"..."2.3")'
      end
      @version = version.exclude_end? ? a...b : a..b
    else
      raise "version must be a String or Range but was a #{version.class}"
    end
    @parameters = [version]
  end

  def match?
    if Range === @version
      @version.include? FULL_RUBY_VERSION
    else
      FULL_RUBY_VERSION >= @version
    end
  end
end

def ruby_version_is(*args, &block)
  VersionGuard.new(*args).run_if(:ruby_version_is, &block)
end