summaryrefslogtreecommitdiff
path: root/spec/support/matchers.rb
blob: ab2e74e1db835d7c26432bc7c54891105ecd731a (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
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
module Spec
  module Matchers
    def have_dep(*args)
      simple_matcher "have dependency" do |given, matcher|
        dep = Bundler::Dependency.new(*args)

        # given.length == args.length / 2
        given.length == 1 && given.all? { |d| d == dep }
      end
    end

    def have_gem(*args)
      simple_matcher "have gem" do |given, matcher|
        given.length == args.length && given.all? { |g| args.include?(g.full_name) }
      end
    end

    def have_rubyopts(*args)
      args = args.flatten
      args = args.first.split(/\s+/) if args.size == 1

      simple_matcher "have options #{args.join(' ')}" do |actual|
        actual = actual.split(/\s+/) if actual.is_a?(String)
        args.all? {|arg| actual.include?(arg) } && actual.uniq.size == actual.size
      end
    end

    def should_be_installed(*names)
      opts = names.last.is_a?(Hash) ? names.pop : {}
      groups = Array(opts[:groups])
      groups << opts
      names.each do |name|
        name, version, platform = name.split(/\s+/)
        version_const = name == 'bundler' ? 'Bundler::VERSION' : Spec::Builders.constantize(name)
        run "require '#{name}.rb'; puts #{version_const}", *groups
        actual_version, actual_platform = out.split(/\s+/)
        Gem::Version.new(actual_version).should == Gem::Version.new(version)
        actual_platform.should == platform
      end
    end

    alias should_be_available should_be_installed

    def should_not_be_installed(*names)
      opts = names.last.is_a?(Hash) ? names.pop : {}
      groups = opts[:groups] || []
      names.each do |name|
        name, version = name.split(/\s+/)
        run <<-R, *groups
          begin
            require '#{name}'
            puts #{Spec::Builders.constantize(name)}
          rescue LoadError, NameError
            puts "WIN"
          end
        R
        if version.nil? || out == "WIN"
          out.should == "WIN"
        else
          Gem::Version.new(out).should_not == Gem::Version.new(version)
        end
      end
    end

    def should_be_locked
      bundled_app("Gemfile.lock").should exist
    end
  end
end