Sha256: 96d1e1f31d38b9539282ba43e3e3dd1b27eac09dd0fe0615e4313d5997aa08e5

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

module CustomMatchers
  class BeWellFormed
    def matches?(files)
      @errors = files.map {|filename|
                  [
                    check_for_tabs(filename),
                    excessive_spacing(filename),
                    newline_precedes_eof(filename)
                  ]
                }.flatten.compact

      @errors.empty?
    end

    def failure_message_for_should
      @errors.join("\n")
    end

  private
    def check_for_tabs(filename)
      bad_lines = File.readlines(filename).each_with_index.map do |line, line_no|
                    line_no + 1 if line["\t"] and line !~ /^\s+#.*\s+\n$/
                  end.flatten.compact

      "#{filename} has tab characters on lines #{bad_lines.join(', ')}" if bad_lines.any?
    end

    def excessive_spacing(filename)
      bad_lines = File.readlines(filename).each_with_index.map do |line, line_no|
                    line_no + 1 if line =~ /\s+\n$/ and line !~ /^\s+#.*\s+\n$/
                  end.flatten.compact

      "#{filename} has spaces on the EOL on lines #{bad_lines.join(', ')}" if bad_lines.any?
    end

    def newline_precedes_eof(filename)
      "#{filename} does not have a newline (\\n) before EOF" if File.read(filename) !~ /\n$/
    end
  end

  def be_well_formed
    BeWellFormed.new
  end
end

Version data entries

10 entries across 10 versions & 5 rubygems

Version Path
haproxyctl-1.1.0 rhapr/spec/support/custom_matchers.rb
haproxyctl-1.0.0 rhapr/spec/support/custom_matchers.rb
haproxyctl-0.2.0 rhapr/spec/support/custom_matchers.rb
haproxyctl-0.0.1 rhapr/spec/support/custom_matchers.rb
rhapr-0.1.0 spec/support/custom_matchers.rb
rhapr-0.0.1 spec/support/custom_matchers.rb
stubber-0.1.1 spec/support/custom_matchers.rb
string_stubber-0.0.5 spec/support/custom_matchers.rb
string_stubber-0.0.3 spec/support/custom_matchers.rb
rsx-0.0.1 spec/support/custom_matchers.rb