2013-07-28 19:03:06 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2013-06-20 05:19:35 -04:00
|
|
|
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!
|
2013-07-28 17:52:05 -04:00
|
|
|
fail sprintf("Untransitive:\n%s\n---\n%s", generated, again)
|
2013-06-20 05:19:35 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-01 08:58:24 -04:00
|
|
|
shared_examples_for 'a mutator' do
|
2012-08-01 07:27:35 -04:00
|
|
|
subject { object.each(node) { |item| yields << item } }
|
|
|
|
|
2013-06-04 13:26:53 -04:00
|
|
|
let(:yields) { [] }
|
|
|
|
let(:object) { described_class }
|
2012-08-13 14:40:00 -04:00
|
|
|
|
|
|
|
unless instance_methods.map(&:to_s).include?('node')
|
2013-06-04 13:26:53 -04:00
|
|
|
let(:node) { parse(source) }
|
2012-08-13 14:40:00 -04:00
|
|
|
end
|
2012-08-01 07:27:35 -04:00
|
|
|
|
|
|
|
it_should_behave_like 'a command method'
|
|
|
|
|
|
|
|
context 'with no block' do
|
|
|
|
subject { object.each(node) }
|
|
|
|
|
|
|
|
it { should be_instance_of(to_enum.class) }
|
|
|
|
|
2013-06-20 05:19:35 -04:00
|
|
|
let(:expected_mutations) do
|
2013-07-28 20:45:40 -04:00
|
|
|
mutations.map(&Subject.method(:coerce))
|
2012-12-10 18:17:19 -05:00
|
|
|
end
|
|
|
|
|
2013-06-20 05:19:35 -04:00
|
|
|
let(:generated_mutations) do
|
2012-08-01 07:27:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'generates the expected mutations' do
|
2013-06-23 15:57:12 -04:00
|
|
|
|
2013-06-20 05:19:35 -04:00
|
|
|
generated = subject.map { |node| Subject.new(node) }
|
|
|
|
|
|
|
|
missing = expected_mutations - generated
|
|
|
|
unexpected = generated - expected_mutations
|
2012-08-01 07:27:35 -04:00
|
|
|
|
2013-06-20 05:19:35 -04:00
|
|
|
message = []
|
2012-12-06 06:32:16 -05:00
|
|
|
|
2013-06-21 13:10:16 -04:00
|
|
|
if missing.any?
|
2013-07-28 17:52:05 -04:00
|
|
|
message << sprintf('Missing mutations (%i):', missing.length)
|
2013-06-20 05:19:35 -04:00
|
|
|
message.concat(missing)
|
2012-08-01 07:27:35 -04:00
|
|
|
end
|
2013-06-20 05:19:35 -04:00
|
|
|
|
2013-06-21 13:10:16 -04:00
|
|
|
if unexpected.any?
|
2013-07-28 17:52:05 -04:00
|
|
|
message << sprintf('Unexpected mutations (%i):', unexpected.length)
|
2013-06-20 05:19:35 -04:00
|
|
|
message.concat(unexpected)
|
|
|
|
end
|
|
|
|
|
2013-07-28 17:52:05 -04:00
|
|
|
if message.any?
|
|
|
|
|
|
|
|
message = sprintf(
|
2013-09-05 19:43:17 -04:00
|
|
|
"Original:\n%s\n%s\n-----\n%s",
|
2013-07-28 17:52:05 -04:00
|
|
|
generate(node),
|
2013-09-05 19:43:17 -04:00
|
|
|
node.inspect,
|
2013-07-28 17:52:05 -04:00
|
|
|
message.join("\n-----\n")
|
|
|
|
)
|
|
|
|
|
|
|
|
fail message
|
|
|
|
end
|
2012-08-01 07:27:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-01 08:58:24 -04:00
|
|
|
shared_examples_for 'a noop mutator' do
|
2012-08-01 07:53:28 -04:00
|
|
|
let(:mutations) { [] }
|
2012-08-01 07:27:35 -04:00
|
|
|
|
2012-08-01 08:58:24 -04:00
|
|
|
it_should_behave_like 'a mutator'
|
2012-08-01 07:53:28 -04:00
|
|
|
end
|