Search code examples
rubydatetimecasestrftime

How can i pull a date's number from a scenario example and use it to move my date x day(s) in adv and then convert it to STRFTIME?


I am having problems understanding how to strip the number '2' from "2 days from now" and then use that to increment my date by two days. so something like date = (Date.today + 2).strftime('%a %-e') aka two days forward.

My 'puts' are just so i can see if the correct date is being outputted. In this situation i am having problems with the third case.

As you can see, I need, verbatim, the date to equal "Thur 26 Tasks"

And(/^I can see the correct task lists details for (.*)$/) do |date|

  sleep 1
  case
  when date == ("today")
    date = Date.today.strftime('%a %-e') + " Tasks"
    puts date
  when date == ("tomorrow")
    date = (Date.today + 1).strftime('%a %-e') + " Tasks"
    puts date
  when date.downcase.include?("days from now")
    date = ((Date.today + [/\d*/]) + " Tasks")
    puts date
  end
  TasksPage.todays_tasks_title.click unless exists {$driver.text(date)}

end


Solution

  • As idea for you

    date = "3 days from now"
    
    date = "#{(Date.today + date.match(/\A\d+(?!days from now\z)/)[0].to_i).strftime('%a %-e')} Tasks" if date.match?(/\A\d+ days from now\z/)
    # => "Fri 29 Tasks"
    

    date.match?(/\A\d+ days from now\z/) returns true or false

    date.match(/\A\d+(?!days from now\z)/)[0] returns substring with days quantity ("3").

    Negative lookahead (?!...) used here.