free_mutant/spec/shared/mutator_behavior.rb

105 lines
2.1 KiB
Ruby
Raw Normal View History

# encoding: utf-8
class Subject
include Equalizer.new(:source)
Undefined = Object.new.freeze
attr_reader :source
def self.coerce(input)
case input
when Parser::AST::Node
new(input)
when String
new(Parser::CurrentRuby.parse(input))
else
raise
end
end
def to_s
"#{@node.inspect}\n#{@source}"
end
def initialize(node)
source = Unparser.unparse(node)
@node, @source = node, source
end
def assert_transitive!
generated = Unparser.generate(@node)
parsed = Parser::CurrentRuby.parse(generated)
again = Unparser.generate(parsed)
unless generated == again
# mostly an unparser bug!
fail sprintf("Untransitive:\n%s\n---\n%s", generated, again)
end
self
end
end
2012-08-01 08:58:24 -04:00
shared_examples_for 'a mutator' do
subject { object.each(node) { |item| yields << item } }
let(:yields) { [] }
let(:object) { described_class }
unless instance_methods.map(&:to_s).include?('node')
let(:node) { parse(source) }
end
it_should_behave_like 'a command method'
context 'with no block' do
subject { object.each(node) }
it { should be_instance_of(to_enum.class) }
let(:expected_mutations) do
2013-07-28 20:45:40 -04:00
mutations.map(&Subject.method(:coerce))
end
let(:generated_mutations) do
end
it 'generates the expected mutations' do
generated = subject.map { |node| Subject.new(node) }
missing = expected_mutations - generated
unexpected = generated - expected_mutations
message = []
2012-12-06 06:32:16 -05:00
if missing.any?
message << sprintf('Missing mutations (%i):', missing.length)
message.concat(missing)
end
if unexpected.any?
message << sprintf('Unexpected mutations (%i):', unexpected.length)
message.concat(unexpected)
end
if message.any?
message = sprintf(
"Original:\n%s\n-----\n%s",
generate(node),
message.join("\n-----\n")
)
fail message
end
end
end
end
2012-08-01 08:58:24 -04:00
shared_examples_for 'a noop mutator' do
let(:mutations) { [] }
2012-08-01 08:58:24 -04:00
it_should_behave_like 'a mutator'
end