Use dedicated exceptions for expression parsing failure scenarios

This commit is contained in:
Markus Schirp 2014-07-06 02:34:01 +00:00
parent ce9d95f583
commit 829e48a7b5
3 changed files with 61 additions and 3 deletions

View file

@ -1,3 +1,3 @@
---
threshold: 18
total_score: 1020
total_score: 1039

View file

@ -17,6 +17,9 @@ module Mutant
REGISTRY = {}
class InvalidExpressionError < RuntimeError; end
class AmbigousExpressionError < RuntimeError; end
# Initialize expression
#
# @param [MatchData] match
@ -88,7 +91,7 @@ module Mutant
# @api private
#
def self.parse(input)
try_parse(input) or raise "Expression: #{input.inspect} is not valid"
try_parse(input) or fail InvalidExpressionError, "Expression: #{input.inspect} is not valid"
end
# Parse input into expression
@ -110,7 +113,7 @@ module Mutant
when 1
expressions.first
else
fail "Ambigous expression: #{input.inspect}"
fail AmbigousExpressionError, "Ambigous expression: #{input.inspect}"
end
end

View file

@ -0,0 +1,55 @@
require 'spec_helper'
describe Mutant::Expression do
let(:object) { described_class }
describe '.try_parse' do
subject { object.try_parse(input) }
context 'on nonsense' do
let(:input) { 'foo bar' }
it { should be(nil) }
end
context 'on a valid expression' do
let(:input) { 'Foo' }
it { should eql(Mutant::Expression::Namespace::Exact.new('Foo')) }
end
context 'on ambigious expression' do
class ExpressionA < Mutant::Expression
register(/\Atest-syntax\z/)
end
class ExpressionB < Mutant::Expression
register(/^test-syntax$/)
end
let(:input) { 'test-syntax' }
it 'raises an exception' do
expect { subject }.to raise_error(Mutant::Expression::AmbigousExpressionError, 'Ambigous expression: "test-syntax"')
end
end
end
describe '.parse' do
subject { object.parse(input) }
context 'on nonsense' do
let(:input) { 'foo bar' }
it 'raises an exception' do
expect { subject }.to raise_error(Mutant::Expression::InvalidExpressionError, 'Expression: "foo bar" is not valid')
end
end
context 'on a valid expression' do
let(:input) { 'Foo' }
it { should eql(Mutant::Expression::Namespace::Exact.new('Foo')) }
end
end
end