require 'rails_helper'

RSpec.describe Dunlop::FileSed do

  context ".egrep" do

    let(:file) { Tempfile.new('file_sed', Rails.application.config.working_path) }
    let(:filename) { file.path }

    after do
      file.unlink
    end

    it "replaces single infile" do
      File.open(filename,'w') { |file| file << %Q{hello world\nhere is some " for you} }
      subject.in_place(filename, %Q{s/"//g})
      expect(File.read(filename).strip).to eq %Q{hello world\nhere is some  for you}
    end

    it "replaces multiple infile" do
      File.open(filename,'w') { |file| file << %Q{hello world\nhere is some " for you} }
      subject.in_place(filename, [%Q{s/"//g},%Q{s/u$/a/}])
      expect(File.read(filename).strip).to eq %Q{hello world\nhere is some  for yoa}
    end

    it "handles CFLR, CR, LF => LF with the right combination" do
      File.open(filename,'w') { |file| file << %Q{hello world\r\nthis is CRLF\r\nmixed with CR only\rand some LF only\nwierd eh?} }
      subject.fix_line_endings(filename)
      expect(File.read(filename)).to eq %Q{hello world\nthis is CRLF\nmixed with CR only\nand some LF only\nwierd eh?}
    end

  end

end
