<commit_before>module RSpec::Puppet
  module FunctionMatchers
    extend RSpec::Matchers::DSL

    matcher :run do
      match do |func_obj|
        if @params
          @func = lambda { func_obj.call(@params) }
        else
          @func = lambda { func_obj.call }
        end

        unless @expected_error.nil?
          result = false
          begin
            @func.call
          rescue Exception => e
            @actual_error = e.class
            if @actual_error == @expected_error
              result = true
            end
          end
          result
        else
          unless @expected_return.nil?
            @actual_return = @func.call
            @actual_return == @expected_return
          else
            begin
              @func.call
            rescue
              false
            end
            true
          end
        end
      end

      chain :with_params do |*params|
        @params = params
      end

      chain :and_return do |value|
        @expected_return = value
      end

      # XXX support error string and regexp
      chain :and_raise_error do |value|
        @expected_error = value
      end

      failure_message_for_should do |func_obj|
        func_name = func_obj.name.to_s.gsub(/^function_/, '')
        func_params = @params.inspect[1..-2]

        if @expected_return
          "expected #{func_name}(#{func_params}) to have returned #{@expected_return.inspect} instead of #{@actual_return.inspect}"
        elsif @expected_error
          "expected #{func_name}(#{func_params}) to have raised #{@expected_error.inspect}"
        else
          "expected #{func_name}(#{func_params}) to have run successfully"
        end
      end

      failure_message_for_should_not do |func_obj|
        func_name = func_obj.name.gsub(/^function_/, '')
        func_params = @params.inspect[1..-2]

        if @expected_return
          "expected #{func_name}(#{func_params}) to not have returned #{@expected_return.inspect}"
        elsif @expected_error
          "expected #{func_name}(#{func_params}) to not have raised #{@expected_error.inspect}"
        else
          "expected #{func_name}(#{func_params}) to not have run successfully"
        end
      end
    end
  end
end<commit_msg>DRY up failure messages for run matcher<commit_after>module RSpec::Puppet
  module FunctionMatchers
    extend RSpec::Matchers::DSL

    matcher :run do
      match do |func_obj|
        if @params
          @func = lambda { func_obj.call(@params) }
        else
          @func = lambda { func_obj.call }
        end

        unless @expected_error.nil?
          result = false
          begin
            @func.call
          rescue Exception => e
            @actual_error = e.class
            if @actual_error == @expected_error
              result = true
            end
          end
          result
        else
          unless @expected_return.nil?
            @actual_return = @func.call
            @actual_return == @expected_return
          else
            begin
              @func.call
            rescue
              false
            end
            true
          end
        end
      end

      chain :with_params do |*params|
        @params = params
      end

      chain :and_return do |value|
        @expected_return = value
      end

      # XXX support error string and regexp
      chain :and_raise_error do |value|
        @expected_error = value
      end

      failure_message_for_should do |func_obj|
        failure_message_generic(:should, func_obj)
      end

      failure_message_for_should_not do |func_obj|
        failure_message_generic(:should_not, func_obj)
      end

      def failure_message_generic(type, func_obj)
        func_name = func_obj.name.gsub(/^function_/, '')
        func_params = @params.inspect[1..-2]

        message = "expected #{func_name}(#{func_params}) to "
        message << "not " if type == :should_not

        if @expected_return
          message << "have returned #{@expected_return.inspect}"
          if type == :should
            message << " instead of #{@actual_return.inspect}"
          end
        elsif @expected_error
          message << "have raised #{@expected_error.inspect}"
        else
          message << "have run successfully"
        end
        message
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08373590982286634, 'fraction_numerical': 0.0013802622498274672, 'mean_word_length': 1.7124142233312538, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2625193', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1193, 'n_words': 307}