require 'rails_helper'

RSpec.describe Dunlop::FileGrep do

  let(:filename) { fixture_path.join("file_zip/sip24_kvd_1.txt") }
  let(:destination_filename) { "/tmp/sip24_kvd_1.txt" }

  before do
    FileUtils.rm_f(destination_filename)
  end

  context ".egrep" do

    it "greps the lines matching the regexp" do
      expect(File.exists?(destination_filename)).to be_falsey
      expect(File.exists?(filename)).to be true
      subject.egrep(filename, destination_filename, 'AA|HVD')
      expect(File.exists?(destination_filename)).to be_truthy
      expect(File.read(destination_filename)).to eq <<-DOC.strip_heredoc
        1000_HVD;1000_HVD;;;0;0
        1000_AA;1000_AA;1000;;2;2
        1000_BB;1000_AA;;1000;1;1
        1000_CC;1000_AA;;1000;1;1
        2000_HVD;2000_HVD;;;0;0
        2000_AA;2000_AA;1000;;1;1
        3000_HVD;3000_HVD;;;1;1
      DOC
    end

  end

  context ".egrep_v" do

    it "greps the lines not matching the regexp" do
      expect(File.exists?(destination_filename)).to be_falsey
      subject.egrep_v(filename, destination_filename, 'AA|HVD')
      expect(File.exists?(destination_filename)).to be_truthy
      expect(File.read(destination_filename)).to eq <<-DOC.strip_heredoc
        kvd;primair;lengte;lengte_primair;adressen;totaal
        1000_<cgb>;;;;;2
        2000_<cgb>;;;;;1
        3000_<cgb>;;;;;1
        172500_A;172500_A;1000;;3;3
      DOC
    end

  end

end
