diff --git a/config/flay.yml b/config/flay.yml index 2a5e50be..1f473d54 100644 --- a/config/flay.yml +++ b/config/flay.yml @@ -1,3 +1,3 @@ --- threshold: 18 -total_score: 1020 +total_score: 1039 diff --git a/lib/mutant/expression.rb b/lib/mutant/expression.rb index f6a81d8d..79ccc5b8 100644 --- a/lib/mutant/expression.rb +++ b/lib/mutant/expression.rb @@ -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 diff --git a/spec/unit/mutant/expression_spec.rb b/spec/unit/mutant/expression_spec.rb new file mode 100644 index 00000000..5a69be6a --- /dev/null +++ b/spec/unit/mutant/expression_spec.rb @@ -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