File: allow_value.rb

package info (click to toggle)
ruby-rspec-puppet 2.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,416 kB
  • sloc: ruby: 6,661; makefile: 6
file content (53 lines) | stat: -rw-r--r-- 1,269 bytes parent folder | download | duplicates (2)
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
module RSpec::Puppet
  module TypeAliasMatchers
    class AllowValue
      def initialize(values)
        @values = values
        @error_msgs = []
      end

      def matches?(catalogue)
        matches = @values.map do |test_value|
          begin
            catalogue.call(test_value)
            true
          rescue Puppet::Error => e
            @error_msgs << e.message
            false
          end
        end
        matches.all?
      end

      def description
        if @values.length == 1
          "match value #{@values.first.inspect}"
        else
          "match values #{@values.map(&:inspect).join(', ')}"
        end
      end

      def failure_message
        "expected that the type alias would " + description + " but it raised the #{@error_msgs.length == 1 ? 'error' : 'errors'} #{@error_msgs.join(', ')}"
      end

      def failure_message_when_negated
        "expected that the type alias would not " + description + " but it does"
      end

      def supports_block_expectations
        true
      end

      def supports_value_expectations
        true
      end
    end

    def allow_value(*values)
      RSpec::Puppet::TypeAliasMatchers::AllowValue.new(values)
    end

    alias_method :allow_values, :allow_value
  end
end