mutest icon indicating copy to clipboard operation
mutest copied to clipboard

Prepend / append `\A`, `^`, `$`, and `\z` anchors to regular expressions.

Open dgollahon opened this issue 8 years ago • 2 comments

We currently upgrade ^ and $ to their full-string counterparts, but we don't seem to add anchors in general.

Note: we may get a "loop" of mutations by adding these and then generically deleting them if we don't special case them. On the one hand a "loop" would force you to prove you really mean the end of line, but it would also be punishing in the case where you're just being defensive / conservative (which is probably ok).

dgollahon avatar Apr 12 '17 21:04 dgollahon

My interest in this mutation was to help detect cases where code testing a certain regexp only provides test cases where the input string contains exactly what the regular expression is matching. For example,

require 'rspec'

def weekday?(input)
  input.match(/(?:Mon|Tue|Wed|Thu|Fri)/)
end

RSpec.describe '#weekday?' do
  it 'matches monday' do
    expect(weekday?('Mon')).to be_truthy
  end

  it 'does not match sunday' do
    expect(weekday?('Sun')).to be_falsey
  end
end

should either become

def weekday?(input)
  input.match(/\A(?:Mon|Tue|Wed|Thu|Fri)\z/)
end

or a test should be added like

it 'matches a day of the week in the middle of a sentence' do
  expect(weekday?('I cannot wait until Friday')).to be_truthy
end

backus avatar Apr 12 '17 21:04 backus

Right. That's what i meant about

where you're just being defensive / conservative (which is probably ok).

which is an argument for special casing it. OTOH if we wanted to be really strict and allow the "loop", then you'd always have to provide two test cases--one with the anchor and one without and show that you do or do not want to match both. I think it would probably be too frustrating to kill in a lot of cases though so allowing an "unchecked" \A or \z is probably not a bad thing (it is "stricter").

dgollahon avatar Apr 12 '17 22:04 dgollahon