vendredi 31 juillet 2015

Regex works on Rubular, but appears to not in RSpec test

I am currently working on these tests. Right now the first 2 will pass but the 3rd one will not.

context "Navigation Functions" do
  let(:nav) {Navigator.new("8801507-001")}
  it "#nav_to_start_folder should navigate to job folder based on stored Work Order number" do
    nav.nav_to_start_folder
    expect(File.basename(Dir.pwd)).to eq("8801507-001 Test Dealer 1 of 4")
  end

  it "#input_csv should return the input CSV when in a folder" do
    expect(nav.input_csv).to eq("test.csv")
  end

  it "#nav_to_next_folder should increment the work order number and job title and create the next folder" do
    nav.nav_to_next_folder
    expect(File.basename(Dir.pwd)).to eq("8801507-002 Test Dealer 2 of 4")
  end
end

The class I am testing is as follows:

class Navigator
def initialize(starting_work_order)
  @workorder = starting_work_order
end

def nav_to_start_folder
  Dir.chdir @workorder[0, 7]
  Dir.chdir Dir.glob("#{@workorder}*").at(0)
end

def input_csv
  arr = Dir.glob('*.csv')
  arr.delete_if { |e| /880\d\d\d\d-\d\d\d ?(for import)?/ =~ e }
  arr.at(0)
end

def nav_to_next_folder
  @title = job_title(File.basename(Dir.pwd))
  Dir.chdir '..'
  next_work_order_number
  next_job_title
  Dir.mkdir @workorder + " " + @title
  Dir.chdir @workorder + " " + @title
end

private

# Info Parser
def next_work_order_number
  @workorder[-3, 3] = next_work_order_seq
end
# Info Parser
def next_work_order_seq
  start_number = @workorder[-3, 3].to_i
  format('%03d', (start_number + 1))
end

# Info Parser
def ipd_style?(folder_name)
  if folder_name[3] == '-'
    false
  else
    true
  end
end

# Info Parser
def job_title(folder_name)
  if ipd_style?(folder_name)
    folder_name[12, (folder_name.length - 11)]
  else
    folder_name[11, (folder_name.length - 10)]
  end
end

# Info Parser
def next_job_title
  /\A(\d*-\d{3}) (?<mainTitle>.+) (?<current>\d+) of (?<total>\d+)\z/ =~ @title
  @title = "#{mainTitle} #{(current.to_i + 1).to_s} of #{total}"
end

end

What appears to be happening is whenever I run next_job_title the regex match groups all come up as nil. I have tested the expression against a few test strings and they all appear to work, but for some reason in this test it won't. Am I missing something subtle (or obvious) about match groups that is causing this to not work?

Also the RSpec failure message is:

 IPDUtils::Navigator Navigation Functions #nav_to_next_folder should increment the work order number and job title and create the next folder
 Failure/Error: expect(File.basename(Dir.pwd)).to eq("8801507-002 Test Dealer 2 of 4")

   expected: "8801507-002 Test Dealer 2 of 4"
        got: "8801507-002  1 of"

Aucun commentaire:

Enregistrer un commentaire